1
- import o from "gson-conform" ;
2
1
const join = ( a , b ) => `${ a } /${ b } ` ;
3
2
import { VALUE_INDEX , POINTER_INDEX } from "./keys" ;
4
3
const toString = Object . prototype . toString ;
5
4
const rContainer = / O b j e c t | A r r a y / ;
6
- const isContainer = v => rContainer . test ( toString . call ( v ) ) ;
7
- const getTypeOf = v => toString . call ( v ) . match ( / \s ( [ ^ \] ] + ) \] / ) . pop ( ) . toLowerCase ( ) ;
5
+ const isContainer = ( v ) => rContainer . test ( toString . call ( v ) ) ;
6
+ const getTypeOf = ( v ) => toString
7
+ . call ( v )
8
+ . match ( / \s ( [ ^ \] ] + ) \] / )
9
+ . pop ( )
10
+ . toLowerCase ( ) ;
8
11
function nodeAsRegex ( node ) {
9
12
return new RegExp ( node . text . replace ( / ( ^ { | } $ ) / g, "" ) ) ;
10
13
}
14
+ /**
15
+ * Iterates over object or array, passing each key, value and parentObject to the callback
16
+ * @param value - to iterate
17
+ * @param callback - receiving key on given input value
18
+ */
19
+ function forEach ( parent , callback ) {
20
+ if ( Array . isArray ( parent ) ) {
21
+ parent . forEach ( callback ) ;
22
+ }
23
+ else if ( Object . prototype . toString . call ( parent ) === "[object Object]" ) {
24
+ Object . keys ( parent ) . forEach ( function ( key ) {
25
+ callback ( parent [ key ] , key , parent ) ;
26
+ } ) ;
27
+ }
28
+ }
29
+ /**
30
+ * Returns all keys of the given input data
31
+ * @param value
32
+ * @return {Array } containing keys of given value
33
+ */
34
+ function getKeys ( value ) {
35
+ if ( Array . isArray ( value ) ) {
36
+ return value . map ( function ( value , index ) {
37
+ return `${ index } ` ;
38
+ } ) ;
39
+ }
40
+ if ( Object . prototype . toString . call ( value ) === "[object Object]" ) {
41
+ return Object . keys ( value ) ;
42
+ }
43
+ return [ ] ;
44
+ }
11
45
const cache = {
12
46
mem : [ ] ,
13
47
get ( entry , prop ) {
@@ -22,18 +56,23 @@ const cache = {
22
56
} ,
23
57
reset ( ) {
24
58
cache . mem . length = 0 ;
25
- }
59
+ } ,
26
60
} ;
27
61
const expand = {
28
62
any ( node , entry ) {
29
63
const value = entry [ VALUE_INDEX ] ;
30
- return o . keys ( value )
64
+ return ( getKeys ( value )
31
65
// .map(prop => cache.get(entry, prop));
32
- . map ( prop => [ value [ prop ] , prop , value , join ( entry [ POINTER_INDEX ] , prop ) ] ) ;
66
+ . map ( ( prop ) => [
67
+ value [ prop ] ,
68
+ prop ,
69
+ value ,
70
+ join ( entry [ POINTER_INDEX ] , prop ) ,
71
+ ] ) ) ;
33
72
} ,
34
73
all ( node , entry ) {
35
74
const result = [ entry ] ;
36
- o . forEach ( entry [ VALUE_INDEX ] , ( value , prop ) => {
75
+ forEach ( entry [ VALUE_INDEX ] , ( value , prop ) => {
37
76
const childEntry = cache . get ( entry , prop ) ;
38
77
// const childEntry = [value, prop, entry[VALUE_INDEX], join(entry[POINTER_INDEX], prop)];
39
78
childEntry && result . push ( ...expand . all ( node , childEntry ) ) ;
@@ -43,10 +82,15 @@ const expand = {
43
82
regex ( node , entry ) {
44
83
const regex = nodeAsRegex ( node ) ;
45
84
const value = entry [ VALUE_INDEX ] ;
46
- return o . keys ( value )
47
- . filter ( prop => regex . test ( prop ) )
48
- . map ( prop => [ value [ prop ] , prop , value , join ( entry [ POINTER_INDEX ] , prop ) ] ) ;
49
- }
85
+ return getKeys ( value )
86
+ . filter ( ( prop ) => regex . test ( prop ) )
87
+ . map ( ( prop ) => [
88
+ value [ prop ] ,
89
+ prop ,
90
+ value ,
91
+ join ( entry [ POINTER_INDEX ] , prop ) ,
92
+ ] ) ;
93
+ } ,
50
94
} ;
51
95
const select = {
52
96
// alias to property (but escaped)
@@ -58,7 +102,7 @@ const select = {
58
102
entry [ VALUE_INDEX ] [ prop ] ,
59
103
prop ,
60
104
entry [ VALUE_INDEX ] ,
61
- join ( entry [ POINTER_INDEX ] , prop )
105
+ join ( entry [ POINTER_INDEX ] , prop ) ,
62
106
] ;
63
107
}
64
108
} ,
@@ -75,10 +119,10 @@ const select = {
75
119
lookahead : ( node , entry ) => {
76
120
let valid = true ;
77
121
let or = false ;
78
- node . children . forEach ( expr => {
122
+ node . children . forEach ( ( expr ) => {
79
123
if ( expr . type === "expression" ) {
80
124
const isValid = select . expression ( expr , entry ) !== undefined ;
81
- valid = or === true ? ( valid || isValid ) : valid && isValid ;
125
+ valid = or === true ? valid || isValid : valid && isValid ;
82
126
}
83
127
else {
84
128
or = expr . type === "orExpr" ;
@@ -95,14 +139,14 @@ const select = {
95
139
return undefined ;
96
140
}
97
141
return expressionMatches ( value [ prop ] , cmp , test ) ? entry : undefined ;
98
- }
142
+ } ,
99
143
} ;
100
144
function expressionMatches ( value , cmp , test ) {
101
145
if ( cmp === undefined ) {
102
146
return value !== undefined ;
103
147
}
104
148
let valid ;
105
- const valueString = "" + value ;
149
+ const valueString = ` ${ value } ` ;
106
150
if ( test . type === "regex" ) {
107
151
const regex = nodeAsRegex ( test ) ;
108
152
valid = regex . test ( valueString ) ;
0 commit comments