@@ -4,32 +4,32 @@ import { z } from "zod";
44import { makeRangeFromPositions } from "../../util/nodeSelectors" ;
55import type { MutableQueryCapture } from "./QueryCapture" ;
66import { QueryPredicateOperator } from "./QueryPredicateOperator" ;
7- import { getChildNodesForFieldName } from "./getChildNodesForFieldName " ;
7+ import { isEven } from "./isEven " ;
88import { q } from "./operatorArgumentSchemaTypes" ;
99
1010/**
11- * A predicate operator that returns true if the node matches the desired index parity.
12- * For example, `(#parity ? @foo value 0 )` will accept the match if the `@foo`
13- * capture is at index parity 0 ( even) among its parents value children.
11+ * A predicate operator that returns true if the node is at an even index within
12+ * its parents field. For example, `(#even ? @foo value)` will accept the match
13+ * if the `@foo` capture is at an even index among its parents value children.
1414 */
15- class Parity extends QueryPredicateOperator < Parity > {
16- name = "parity?" as const ;
17- schema = z . tuple ( [ q . node , q . string , q . integer ] ) ;
18- run ( { node } : MutableQueryCapture , fieldName : string , parity : 0 | 1 ) {
19- if ( node . parent == null ) {
20- return false ;
21- }
22-
23- const children = getChildNodesForFieldName ( node . parent , fieldName ) ;
24- const nodeIndex = children . findIndex ( ( { id } ) => id === node . id ) ;
25-
26- if ( nodeIndex === - 1 ) {
27- return false ;
28- }
29-
30- const desiredIndex = Math . floor ( nodeIndex / 2 ) * 2 + parity ;
15+ class Even extends QueryPredicateOperator < Even > {
16+ name = "even?" as const ;
17+ schema = z . tuple ( [ q . node , q . string ] ) ;
18+ run ( { node } : MutableQueryCapture , fieldName : string ) {
19+ return isEven ( node , fieldName ) ;
20+ }
21+ }
3122
32- return nodeIndex === desiredIndex ;
23+ /**
24+ * A predicate operator that returns true if the node is at an odd index within
25+ * its parents field. For example, `(#odd? @foo value)` will accept the match
26+ * if the `@foo` capture is at an odd index among its parents value children.
27+ */
28+ class Odd extends QueryPredicateOperator < Odd > {
29+ name = "odd?" as const ;
30+ schema = z . tuple ( [ q . node , q . string ] ) ;
31+ run ( { node } : MutableQueryCapture , fieldName : string ) {
32+ return ! isEven ( node , fieldName ) ;
3333 }
3434}
3535
@@ -429,7 +429,8 @@ class EmptySingleMultiDelimiter extends QueryPredicateOperator<EmptySingleMultiD
429429
430430export const queryPredicateOperators = [
431431 new Log ( ) ,
432- new Parity ( ) ,
432+ new Even ( ) ,
433+ new Odd ( ) ,
433434 new Text ( ) ,
434435 new Type ( ) ,
435436 new NotType ( ) ,
0 commit comments