-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
224 lines (191 loc) · 4.96 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
var pkg = require("./index.js");
var jsl = new pkg.JSLProcessor();
var [ eq, and, any, parent] =
[ jsl.eq, jsl.and, jsl.any, jsl.parent];
jsl.match('item', (x) => eq( x, 1), (node) => {
return {
"case": "match by value",
item: node,
"index": jsl.position(),
isLast:jsl.isLast(),
isFirst: jsl.isFirst() };
});
jsl.match('item', (x) => jsl.position() === 2, (node) => {
return { "case": "match by position()", value: node };
})
jsl.match('item', (x) => jsl.parent().parent().node().a === 3, (node) => {
return { "case": "match by value of parent() ", value: node };
})
jsl.match('item', any, (node) => {
return { "case:" : "match any node", value: node, index: jsl.position() }; });
jsl.match(null, (x) => and( eq( x.a, 1), eq(x.b, 2) ), (node) => {
return { "case": "x.a == 1 && x.b == 2 - match stronger condition",
undefValue: jsl.valueOf( ()=>node.undef.property ),
definedVaue: jsl.valueOf( ()=>node.a ),
position: jsl.position(),
items: jsl.apply( 'item', node.items)
};
})
jsl.match( (x) => eq( x.a, 1), function(node){
return { "case": "x.a == 1",
undefValue: jsl.valueOf( ()=>node.undef.property ),
definedVaue: jsl.valueOf( ()=>node.a ),
position: jsl.position() };
});
jsl.match('show-value', any, node => {
return {
'case': 'process object member, , mode=show-value',
'name': jsl.name(),
'position': jsl.position(),
'value': node };
});
jsl.match( (x) => jsl.node().a === 4, function(node){
return { "case" : "match via node(), mode=null", value: node, values: jsl.apply( 'show-value', node ) };
});
jsl.match( (x) => eq( x.a, 2), function(node){
return { "case" : "match via eq() ", position: jsl.position() };
});
jsl.match( (x) => eq( x.undef, 2), function(node){
return { "case" : "x.undef == 2" };
});
jsl.match( (x) => eq( (x)=>x.undef.undef, 2), function(node){
return { "case" : "x.undef.undef == 2" };
});
jsl.match('item-object', any, (node) => {
return { "case": "match any, mode=item-object", any: jsl.node() };
});
jsl.match('item-object', (x) => jsl.parent().parent().node().a === 3, (node) => {
return { parent: 3, node: node, "case":"match by parent()" };
})
jsl.match( (x) => jsl.name() == 'items', node => {
return { "case" : "match by name(), mode=null",
items: jsl.apply( node)
};
});
jsl.match( 'param', node => {
return { "case" : "match by nodeName",
value: jsl.apply( node )
};
});
jsl.match( (node) => (/boolean|number|string/).test(typeof node), function(node){
return node;
});
jsl.match( any, function(node){
return { "case" : "match any node, mode=null",
node: node,
items: jsl.apply( 'item-object', node.items)
};
});
var data = {
items : [
{ a : 1, b :2, items: [ 1, 2, 3 ] },
{ a : 1},
{ a : 2},
{ a : 3, items: [ 4, 5, 6] },
{ a : 4, c: 5, d: "value"},
],
param: 'value'
}
console.log( JSON.stringify( jsl.apply( data ), null, " " )); //the same as previous
/**
Result:
{
"items": {
"case": "match by name(), mode=null",
"items": [
{
"case": "x.a == 1 && x.b == 2 - match stronger condition",
"definedVaue": 1,
"position": 0,
"items": [
{
"case": "match by value",
"item": 1,
"index": 0,
"isLast": false,
"isFirst": true
},
{
"case:": "match any node",
"value": 2,
"index": 1
},
{
"case": "match by position()",
"value": 3
}
]
},
{
"case": "x.a == 1",
"definedVaue": 1,
"position": 1
},
{
"case": "match via eq() ",
"position": 2
},
{
"case": "match any node, mode=null",
"node": {
"a": 3,
"items": [
4,
5,
6
]
},
"items": [
{
"parent": 3,
"node": 4,
"case": "match by parent()"
},
{
"parent": 3,
"node": 5,
"case": "match by parent()"
},
{
"parent": 3,
"node": 6,
"case": "match by parent()"
}
]
},
{
"case": "match via node(), mode=null",
"value": {
"a": 4,
"c": 5,
"d": "value"
},
"values": {
"a": {
"case": "process object member, , mode=show-value",
"name": "a",
"position": 1,
"value": 4
},
"c": {
"case": "process object member, , mode=show-value",
"name": "c",
"position": 2,
"value": 5
},
"d": {
"case": "process object member, , mode=show-value",
"name": "d",
"position": 3,
"value": "value"
}
}
}
]
},
"param": {
"case": "match by nodeName",
"value": "value"
}
}
*/