1- import { isArray , isObject } from "../../types" ;
1+ import { isArray , isObject , isString } from "../../types" ;
22import { JSONPathEnvironment } from "../environment" ;
33import { LogicalExpression } from "../expression" ;
44import { JSONPathNode } from "../node" ;
@@ -11,45 +11,41 @@ export class KeySelector extends JSONPathSelector {
1111 readonly environment : JSONPathEnvironment ,
1212 readonly token : Token ,
1313 readonly key : string ,
14- readonly shorthand : boolean = false ,
1514 ) {
1615 super ( environment , token ) ;
1716 }
1817
19- public resolve ( nodes : JSONPathNode [ ] ) : JSONPathNode [ ] {
18+ public resolve ( node : JSONPathNode ) : JSONPathNode [ ] {
2019 const rv : JSONPathNode [ ] = [ ] ;
21- for ( const node of nodes ) {
22- if ( node . value instanceof String || isArray ( node . value ) ) continue ;
23- if ( isObject ( node . value ) && hasStringKey ( node . value , this . key ) ) {
24- rv . push (
25- new JSONPathNode (
26- this . key ,
27- node . location . concat ( `${ KEY_MARK } ${ this . key } ` ) ,
28- node . root ,
29- ) ,
30- ) ;
31- }
20+ if ( node . value instanceof String || isArray ( node . value ) ) return rv ;
21+ if ( isObject ( node . value ) && hasStringKey ( node . value , this . key ) ) {
22+ rv . push (
23+ new JSONPathNode (
24+ this . key ,
25+ node . location . concat ( `${ KEY_MARK } ${ this . key } ` ) ,
26+ node . root ,
27+ ) ,
28+ ) ;
3229 }
3330 return rv ;
3431 }
3532
36- public * lazyResolve ( nodes : Iterable < JSONPathNode > ) : Generator < JSONPathNode > {
37- for ( const node of nodes ) {
38- if ( node . value instanceof String || isArray ( node . value ) ) continue ;
39- if ( isObject ( node . value ) && hasStringKey ( node . value , this . key ) ) {
40- yield new JSONPathNode (
41- this . key ,
42- node . location . concat ( `${ KEY_MARK } ${ this . key } ` ) ,
43- node . root ,
44- ) ;
45- }
33+ public * lazyResolve ( node : JSONPathNode ) : Generator < JSONPathNode > {
34+ if (
35+ ! isString ( node . value ) &&
36+ isObject ( node . value ) &&
37+ hasStringKey ( node . value , this . key )
38+ ) {
39+ yield new JSONPathNode (
40+ this . key ,
41+ node . location . concat ( `${ KEY_MARK } ${ this . key } ` ) ,
42+ node . root ,
43+ ) ;
4644 }
4745 }
4846
4947 public toString ( ) : string {
50- return this . shorthand
51- ? `[~'${ this . key . replaceAll ( "'" , "\\'" ) } ']`
52- : `~'${ this . key . replaceAll ( "'" , "\\'" ) } '` ;
48+ return `~'${ this . key . replaceAll ( "'" , "\\'" ) } '` ;
5349 }
5450}
5551
@@ -60,47 +56,41 @@ export class KeysSelector extends JSONPathSelector {
6056 constructor (
6157 readonly environment : JSONPathEnvironment ,
6258 readonly token : Token ,
63- readonly shorthand : boolean = false ,
6459 ) {
6560 super ( environment , token ) ;
6661 }
6762
68- public resolve ( nodes : JSONPathNode [ ] ) : JSONPathNode [ ] {
63+ public resolve ( node : JSONPathNode ) : JSONPathNode [ ] {
6964 const rv : JSONPathNode [ ] = [ ] ;
70- for ( const node of nodes ) {
71- if ( node . value instanceof String || isArray ( node . value ) ) continue ;
72- if ( isObject ( node . value ) ) {
73- for ( const [ key , _ ] of this . environment . entries ( node . value ) ) {
74- rv . push (
75- new JSONPathNode (
76- key ,
77- node . location . concat ( `${ KEY_MARK } ${ key } ` ) ,
78- node . root ,
79- ) ,
80- ) ;
81- }
65+ if ( node . value instanceof String || isArray ( node . value ) ) return rv ;
66+ if ( isObject ( node . value ) ) {
67+ for ( const [ key , _ ] of this . environment . entries ( node . value ) ) {
68+ rv . push (
69+ new JSONPathNode (
70+ key ,
71+ node . location . concat ( `${ KEY_MARK } ${ key } ` ) ,
72+ node . root ,
73+ ) ,
74+ ) ;
8275 }
8376 }
8477 return rv ;
8578 }
8679
87- public * lazyResolve ( nodes : Iterable < JSONPathNode > ) : Generator < JSONPathNode > {
88- for ( const node of nodes ) {
89- if ( node . value instanceof String || isArray ( node . value ) ) continue ;
90- if ( isObject ( node . value ) ) {
91- for ( const [ key , _ ] of this . environment . entries ( node . value ) ) {
92- yield new JSONPathNode (
93- key ,
94- node . location . concat ( `${ KEY_MARK } ${ key } ` ) ,
95- node . root ,
96- ) ;
97- }
80+ public * lazyResolve ( node : JSONPathNode ) : Generator < JSONPathNode > {
81+ if ( isObject ( node . value ) && ! isString ( node . value ) && ! isArray ( node . value ) ) {
82+ for ( const [ key , _ ] of this . environment . entries ( node . value ) ) {
83+ yield new JSONPathNode (
84+ key ,
85+ node . location . concat ( `${ KEY_MARK } ${ key } ` ) ,
86+ node . root ,
87+ ) ;
9888 }
9989 }
10090 }
10191
10292 public toString ( ) : string {
103- return this . shorthand ? "[~]" : "~" ;
93+ return "~" ;
10494 }
10595}
10696
@@ -113,52 +103,48 @@ export class KeysFilterSelector extends JSONPathSelector {
113103 super ( environment , token ) ;
114104 }
115105
116- public resolve ( nodes : JSONPathNode [ ] ) : JSONPathNode [ ] {
106+ public resolve ( node : JSONPathNode ) : JSONPathNode [ ] {
117107 const rv : JSONPathNode [ ] = [ ] ;
118- for ( const node of nodes ) {
119- if ( node . value instanceof String || isArray ( node . value ) ) continue ;
120- if ( isObject ( node . value ) ) {
121- for ( const [ key , value ] of this . environment . entries ( node . value ) ) {
122- const filterContext : FilterContext = {
123- environment : this . environment ,
124- currentValue : value ,
125- rootValue : node . root ,
126- currentKey : key ,
127- } ;
128- if ( this . expression . evaluate ( filterContext ) ) {
129- rv . push (
130- new JSONPathNode (
131- key ,
132- node . location . concat ( `${ KEY_MARK } ${ key } ` ) ,
133- node . root ,
134- ) ,
135- ) ;
136- }
108+ if ( node . value instanceof String || isArray ( node . value ) ) return rv ;
109+ if ( isObject ( node . value ) ) {
110+ for ( const [ key , value ] of this . environment . entries ( node . value ) ) {
111+ const filterContext : FilterContext = {
112+ environment : this . environment ,
113+ currentValue : value ,
114+ rootValue : node . root ,
115+ currentKey : key ,
116+ } ;
117+ if ( this . expression . evaluate ( filterContext ) ) {
118+ rv . push (
119+ new JSONPathNode (
120+ key ,
121+ node . location . concat ( `${ KEY_MARK } ${ key } ` ) ,
122+ node . root ,
123+ ) ,
124+ ) ;
137125 }
138126 }
139127 }
140128 return rv ;
141129 }
142130
143- public * lazyResolve ( nodes : Iterable < JSONPathNode > ) : Generator < JSONPathNode > {
144- for ( const node of nodes ) {
145- if ( node . value instanceof String || isArray ( node . value ) ) continue ;
146- if ( isObject ( node . value ) ) {
147- for ( const [ key , value ] of this . environment . entries ( node . value ) ) {
148- const filterContext : FilterContext = {
149- environment : this . environment ,
150- currentValue : value ,
151- rootValue : node . root ,
152- lazy : true ,
153- currentKey : key ,
154- } ;
155- if ( this . expression . evaluate ( filterContext ) ) {
156- yield new JSONPathNode (
157- key ,
158- node . location . concat ( `${ KEY_MARK } ${ key } ` ) ,
159- node . root ,
160- ) ;
161- }
131+ public * lazyResolve ( node : JSONPathNode ) : Generator < JSONPathNode > {
132+ if ( node . value instanceof String || isArray ( node . value ) ) return ;
133+ if ( isObject ( node . value ) ) {
134+ for ( const [ key , value ] of this . environment . entries ( node . value ) ) {
135+ const filterContext : FilterContext = {
136+ environment : this . environment ,
137+ currentValue : value ,
138+ rootValue : node . root ,
139+ lazy : true ,
140+ currentKey : key ,
141+ } ;
142+ if ( this . expression . evaluate ( filterContext ) ) {
143+ yield new JSONPathNode (
144+ key ,
145+ node . location . concat ( `${ KEY_MARK } ${ key } ` ) ,
146+ node . root ,
147+ ) ;
162148 }
163149 }
164150 }
0 commit comments