forked from JSONPath-Plus/JSONPath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.examples.js
269 lines (240 loc) · 10.7 KB
/
test.examples.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import {checkBuiltInVMAndNodeVM} from '../test-helpers/checkVM.js';
checkBuiltInVMAndNodeVM(function (vmType, setBuiltInState) {
describe(`JSONPath - Examples (${vmType})`, function () {
before(setBuiltInState);
// tests based on examples at http://goessner.net/articles/jsonpath/
const json = {
"store": {
"book": [{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}],
"bicycle": {
"color": "red",
"price": 19.95
}
}
};
it('wildcards (with and without $.)', () => {
const books = json.store.book;
const expected = [books[0].author, books[1].author, books[2].author, books[3].author];
let result = jsonpath({json, path: '$.store.book[*].author'});
assert.deepEqual(result, expected);
result = jsonpath({json, path: 'store.book[*].author'});
assert.deepEqual(result, expected);
});
it('all properties, entire tree', () => {
const books = json.store.book;
const expected = [books[0].author, books[1].author, books[2].author, books[3].author];
const result = jsonpath({json, path: '$..author'});
assert.deepEqual(result, expected);
});
it('all sub properties, single level', () => {
const expected = [json.store.book, json.store.bicycle];
const result = jsonpath({json, path: '$.store.*'});
assert.deepEqual(result, expected);
});
it('all sub properties, entire tree', () => {
const books = json.store.book;
const expected = [books[0].price, books[1].price, books[2].price, books[3].price, json.store.bicycle.price];
const result = jsonpath({json, path: '$.store..price'});
assert.deepEqual(result, expected);
});
it('n property of entire tree', () => {
const books = json.store.book;
const expected = [books[2]];
const result = jsonpath({json, path: '$..book[2]'});
assert.deepEqual(result, expected);
});
it('last property of entire tree', () => {
const books = json.store.book;
const expected = [books[3]];
let result = jsonpath({json, path: '$..book[(@.length-1)]'});
assert.deepEqual(result, expected);
result = jsonpath({json, path: '$..book[-1:]'});
assert.deepEqual(result, expected);
});
it('range of property of entire tree', () => {
const books = json.store.book;
const expected = [books[0], books[1]];
let result = jsonpath({json, path: '$..book[0,1]'});
assert.deepEqual(result, expected);
result = jsonpath({json, path: '$..book[:2]'});
assert.deepEqual(result, expected);
});
it('range of property of entire tree w/ single element result', () => {
const book = json.store.book[0];
const input = {books: [book]};
const expected = [book];
let result = jsonpath({json: input, path: '$.books[0,1]', wrap: false});
assert.deepEqual(result, expected);
result = jsonpath({json: input, path: '$.books[:1]', wrap: false});
assert.deepEqual(result, expected);
});
it('categories and authors of all books', () => {
const expected = ['reference', 'Nigel Rees'];
const result = jsonpath({json, path: '$..book[0][category,author]'});
assert.deepEqual(result, expected);
});
it('filter all properties if sub property exists, of entire tree', () => {
const books = json.store.book;
const expected = [books[2], books[3]];
const result = jsonpath({json, path: '$..book[?(@.isbn)]'});
assert.deepEqual(result, expected);
});
it('filter all properties if sub property exists, of single element array', () => {
const book = json.store.book[3];
const input = {books: [book]};
const expected = [book];
const result = jsonpath({json: input, path: '$.books[?(@.isbn)]', wrap: false});
assert.deepEqual(result, expected);
});
it('filter all properties if sub property greater than of entire tree', () => {
const books = json.store.book;
const expected = [books[0], books[2]];
const result = jsonpath({json, path: '$..book[?(@.price<10)]'});
assert.deepEqual(result, expected);
});
it('@ as a scalar value', () => {
const expected = [json.store.bicycle.price, ...json.store.book.slice(1).map((book) => {
return book.price;
})];
const result = jsonpath({json, path: "$..*[?(@property === 'price' && @ !== 8.95)]", wrap: false});
assert.deepEqual(result, expected);
});
it('all properties of a JSON structure (beneath the root)', () => {
const expected = [
json.store,
json.store.book,
json.store.bicycle
];
json.store.book.forEach((book) => {
expected.push(book);
});
json.store.book.forEach(function (book) {
Object.keys(book).forEach(function (p) {
expected.push(book[p]);
});
});
expected.push(
json.store.bicycle.color,
json.store.bicycle.price
);
const result = jsonpath({json, path: '$..*'});
assert.deepEqual(result, expected);
});
it('all parent components of a JSON structure', () => {
const expected = [
json,
json.store,
json.store.book
];
json.store.book.forEach((book) => {
expected.push(book);
});
expected.push(json.store.bicycle);
const result = jsonpath({json, path: '$..'});
assert.deepEqual(result, expected);
});
it('root', () => {
const expected = json;
const result = jsonpath({json, path: '$', wrap: false});
assert.deepEqual(result, expected);
});
it('Custom operator: parent (caret)', () => {
const expected = [json.store, json.store.book];
const result = jsonpath({json, path: '$..[?(@.price>19)]^'});
assert.deepEqual(result, expected);
});
it('Custom operator: property name (tilde)', () => {
const expected = ['book', 'bicycle'];
const result = jsonpath({json, path: '$.store.*~'});
assert.deepEqual(result, expected);
});
it('Custom property @path', () => {
const expected = json.store.book.slice(1);
const result = jsonpath({json, path: '$.store.book[?(@path !== "$[\'store\'][\'book\'][0]")]'});
assert.deepEqual(result, expected);
});
it('Custom property: @parent', () => {
const expected = ['reference', 'fiction', 'fiction', 'fiction'];
const result = jsonpath({json, path: '$..book[?(@parent.bicycle && @parent.bicycle.color === "red")].category'});
assert.deepEqual(result, expected);
});
it('Custom property: @property', () => {
let expected = json.store.book.reduce(function (arr, book) {
arr.push(book.author, book.title);
if (book.isbn) { arr.push(book.isbn); }
arr.push(book.price);
return arr;
}, []);
let result = jsonpath({json, path: '$..book.*[?(@property !== "category")]'});
assert.deepEqual(result, expected);
expected = json.store.book.slice(1);
result = jsonpath({json, path: '$..book[?(@property !== 0)]'});
assert.deepEqual(result, expected);
});
it('Custom property: @parentProperty', () => {
let expected = [json.store.bicycle.color, json.store.bicycle.price];
let result = jsonpath({json, path: '$.store.*[?(@parentProperty !== "book")]'});
assert.deepEqual(result, expected);
expected = json.store.book.slice(1).reduce(function (rslt, book) {
return [...rslt, ...Object.keys(book).reduce((reslt, prop) => {
reslt.push(book[prop]);
return reslt;
}, [])];
}, []);
result = jsonpath({json, path: '$..book.*[?(@parentProperty !== 0)]'});
assert.deepEqual(result, expected);
});
it('Custom property: @root', () => {
const expected = [json.store.book[2]];
const result = jsonpath({json, path: '$..book[?(@.price === @root.store.book[2].price)]'});
assert.deepEqual(result, expected);
});
it('@number()', () => {
const expected = [8.95, 12.99, 8.99, 22.99];
const result = jsonpath({json, path: '$.store.book..*@number()', flatten: true});
assert.deepEqual(result, expected);
});
it('Regex on value', () => {
const expected = [json.store.book[1].category, json.store.book[2].category, json.store.book[3].category];
const result = jsonpath({
json,
path: '$..book.*[?(@property === "category" && @.match(/TION$/i))]'
});
assert.deepEqual(result, expected);
});
it('Regex on property', () => {
const books = json.store.book;
const expected = [books[2], books[3]];
const result = jsonpath({
json,
path: '$..book.*[?(@property.match(/bn$/i))]^'
});
assert.deepEqual(result, expected);
});
});
});