forked from Azure/login
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.js
More file actions
359 lines (311 loc) · 16.8 KB
/
Copy pathquery.js
File metadata and controls
359 lines (311 loc) · 16.8 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
var assert = require('assert');
var jp = require('../');
var data = require('./data/store.json');
suite('query', function() {
test('first-level member', function() {
var results = jp.nodes(data, '$.store');
assert.deepEqual(results, [ { path: ['$', 'store'], value: data.store } ]);
});
test('authors of all books in the store', function() {
var results = jp.nodes(data, '$.store.book[*].author');
assert.deepEqual(results, [
{ path: ['$', 'store', 'book', 0, 'author'], value: 'Nigel Rees' },
{ path: ['$', 'store', 'book', 1, 'author'], value: 'Evelyn Waugh' },
{ path: ['$', 'store', 'book', 2, 'author'], value: 'Herman Melville' },
{ path: ['$', 'store', 'book', 3, 'author'], value: 'J. R. R. Tolkien' }
]);
});
test('all authors', function() {
var results = jp.nodes(data, '$..author');
assert.deepEqual(results, [
{ path: ['$', 'store', 'book', 0, 'author'], value: 'Nigel Rees' },
{ path: ['$', 'store', 'book', 1, 'author'], value: 'Evelyn Waugh' },
{ path: ['$', 'store', 'book', 2, 'author'], value: 'Herman Melville' },
{ path: ['$', 'store', 'book', 3, 'author'], value: 'J. R. R. Tolkien' }
]);
});
test('all authors via subscript descendant string literal', function() {
var results = jp.nodes(data, "$..['author']");
assert.deepEqual(results, [
{ path: ['$', 'store', 'book', 0, 'author'], value: 'Nigel Rees' },
{ path: ['$', 'store', 'book', 1, 'author'], value: 'Evelyn Waugh' },
{ path: ['$', 'store', 'book', 2, 'author'], value: 'Herman Melville' },
{ path: ['$', 'store', 'book', 3, 'author'], value: 'J. R. R. Tolkien' }
]);
});
test('all things in store', function() {
var results = jp.nodes(data, '$.store.*');
assert.deepEqual(results, [
{ path: ['$', 'store', 'book'], value: data.store.book },
{ path: ['$', 'store', 'bicycle'], value: data.store.bicycle }
]);
});
test('price of everything in the store', function() {
var results = jp.nodes(data, '$.store..price');
assert.deepEqual(results, [
{ path: ['$', 'store', 'book', 0, 'price'], value: 8.95 },
{ path: ['$', 'store', 'book', 1, 'price'], value: 12.99 },
{ path: ['$', 'store', 'book', 2, 'price'], value: 8.99 },
{ path: ['$', 'store', 'book', 3, 'price'], value: 22.99 },
{ path: ['$', 'store', 'bicycle', 'price'], value: 19.95 }
]);
});
test('last book in order via expression', function() {
var results = jp.nodes(data, '$..book[(@.length-1)]');
assert.deepEqual(results, [ { path: ['$', 'store', 'book', 3], value: data.store.book[3] }]);
});
test('first two books via union', function() {
var results = jp.nodes(data, '$..book[0,1]');
assert.deepEqual(results, [
{ path: ['$', 'store', 'book', 0], value: data.store.book[0] },
{ path: ['$', 'store', 'book', 1], value: data.store.book[1] }
]);
});
test('first two books via slice', function() {
var results = jp.nodes(data, '$..book[0:2]');
assert.deepEqual(results, [
{ path: ['$', 'store', 'book', 0], value: data.store.book[0] },
{ path: ['$', 'store', 'book', 1], value: data.store.book[1] }
]);
});
test('filter all books with isbn number', function() {
var results = jp.nodes(data, '$..book[?(@.isbn)]');
assert.deepEqual(results, [
{ path: ['$', 'store', 'book', 2], value: data.store.book[2] },
{ path: ['$', 'store', 'book', 3], value: data.store.book[3] }
]);
});
test('filter all books with a price less than 10', function() {
var results = jp.nodes(data, '$..book[?(@.price<10)]');
assert.deepEqual(results, [
{ path: ['$', 'store', 'book', 0], value: data.store.book[0] },
{ path: ['$', 'store', 'book', 2], value: data.store.book[2] }
]);
});
test('first ten of all elements', function() {
var results = jp.nodes(data, '$..*', 10);
assert.deepEqual(results, [
{ path: [ '$', 'store' ], value: data.store },
{ path: [ '$', 'store', 'book' ], value: data.store.book },
{ path: [ '$', 'store', 'bicycle' ], value: data.store.bicycle },
{ path: [ '$', 'store', 'book', 0 ], value: data.store.book[0] },
{ path: [ '$', 'store', 'book', 1 ], value: data.store.book[1] },
{ path: [ '$', 'store', 'book', 2 ], value: data.store.book[2] },
{ path: [ '$', 'store', 'book', 3 ], value: data.store.book[3] },
{ path: [ '$', 'store', 'book', 0, 'category' ], value: 'reference' },
{ path: [ '$', 'store', 'book', 0, 'author' ], value: 'Nigel Rees' },
{ path: [ '$', 'store', 'book', 0, 'title' ], value: 'Sayings of the Century' }
])
});
test('all elements', function() {
var results = jp.nodes(data, '$..*');
assert.deepEqual(results, [
{ path: [ '$', 'store' ], value: data.store },
{ path: [ '$', 'store', 'book' ], value: data.store.book },
{ path: [ '$', 'store', 'bicycle' ], value: data.store.bicycle },
{ path: [ '$', 'store', 'book', 0 ], value: data.store.book[0] },
{ path: [ '$', 'store', 'book', 1 ], value: data.store.book[1] },
{ path: [ '$', 'store', 'book', 2 ], value: data.store.book[2] },
{ path: [ '$', 'store', 'book', 3 ], value: data.store.book[3] },
{ path: [ '$', 'store', 'book', 0, 'category' ], value: 'reference' },
{ path: [ '$', 'store', 'book', 0, 'author' ], value: 'Nigel Rees' },
{ path: [ '$', 'store', 'book', 0, 'title' ], value: 'Sayings of the Century' },
{ path: [ '$', 'store', 'book', 0, 'price' ], value: 8.95 },
{ path: [ '$', 'store', 'book', 1, 'category' ], value: 'fiction' },
{ path: [ '$', 'store', 'book', 1, 'author' ], value: 'Evelyn Waugh' },
{ path: [ '$', 'store', 'book', 1, 'title' ], value: 'Sword of Honour' },
{ path: [ '$', 'store', 'book', 1, 'price' ], value: 12.99 },
{ path: [ '$', 'store', 'book', 2, 'category' ], value: 'fiction' },
{ path: [ '$', 'store', 'book', 2, 'author' ], value: 'Herman Melville' },
{ path: [ '$', 'store', 'book', 2, 'title' ], value: 'Moby Dick' },
{ path: [ '$', 'store', 'book', 2, 'isbn' ], value: '0-553-21311-3' },
{ path: [ '$', 'store', 'book', 2, 'price' ], value: 8.99 },
{ path: [ '$', 'store', 'book', 3, 'category' ], value: 'fiction' },
{ path: [ '$', 'store', 'book', 3, 'author' ], value: 'J. R. R. Tolkien' },
{ path: [ '$', 'store', 'book', 3, 'title' ], value: 'The Lord of the Rings' },
{ path: [ '$', 'store', 'book', 3, 'isbn' ], value: '0-395-19395-8' },
{ path: [ '$', 'store', 'book', 3, 'price' ], value: 22.99 },
{ path: [ '$', 'store', 'bicycle', 'color' ], value: 'red' },
{ path: [ '$', 'store', 'bicycle', 'price' ], value: 19.95 }
]);
});
test('all elements via subscript wildcard', function() {
var results = jp.nodes(data, '$..*');
assert.deepEqual(jp.nodes(data, '$..[*]'), jp.nodes(data, '$..*'));
});
test('object subscript wildcard', function() {
var results = jp.query(data, '$.store[*]');
assert.deepEqual(results, [ data.store.book, data.store.bicycle ]);
});
test('no match returns empty array', function() {
var results = jp.nodes(data, '$..bookz');
assert.deepEqual(results, []);
});
test('member numeric literal gets first element', function() {
var results = jp.nodes(data, '$.store.book.0');
assert.deepEqual(results, [ { path: [ '$', 'store', 'book', 0 ], value: data.store.book[0] } ]);
});
test('member numeric literal matches string-numeric key', function() {
var data = { authors: { '1': 'Herman Melville', '2': 'J. R. R. Tolkien' } };
var results = jp.nodes(data, '$.authors.1');
assert.deepEqual(results, [ { path: [ '$', 'authors', 1 ], value: 'Herman Melville' } ]);
});
test('descendant numeric literal gets first element', function() {
var results = jp.nodes(data, '$.store.book..0');
assert.deepEqual(results, [ { path: [ '$', 'store', 'book', 0 ], value: data.store.book[0] } ]);
});
test('root element gets us original obj', function() {
var results = jp.nodes(data, '$');
assert.deepEqual(results, [ { path: ['$'], value: data } ]);
});
test('subscript double-quoted string', function() {
var results = jp.nodes(data, '$["store"]');
assert.deepEqual(results, [ { path: ['$', 'store'], value: data.store} ]);
});
test('subscript single-quoted string', function() {
var results = jp.nodes(data, "$['store']");
assert.deepEqual(results, [ { path: ['$', 'store'], value: data.store} ]);
});
test('leading member component', function() {
var results = jp.nodes(data, "store");
assert.deepEqual(results, [ { path: ['$', 'store'], value: data.store} ]);
});
test('union of three array slices', function() {
var results = jp.query(data, "$.store.book[0:1,1:2,2:3]");
assert.deepEqual(results, data.store.book.slice(0,3));
});
test('slice with step > 1', function() {
var results = jp.query(data, "$.store.book[0:4:2]");
assert.deepEqual(results, [ data.store.book[0], data.store.book[2]]);
});
test('union of subscript string literal keys', function() {
var results = jp.nodes(data, "$.store['book','bicycle']");
assert.deepEqual(results, [
{ path: ['$', 'store', 'book'], value: data.store.book },
{ path: ['$', 'store', 'bicycle'], value: data.store.bicycle },
]);
});
test('union of subscript string literal three keys', function() {
var results = jp.nodes(data, "$.store.book[0]['title','author','price']");
assert.deepEqual(results, [
{ path: ['$', 'store', 'book', 0, 'title'], value: data.store.book[0].title },
{ path: ['$', 'store', 'book', 0, 'author'], value: data.store.book[0].author },
{ path: ['$', 'store', 'book', 0, 'price'], value: data.store.book[0].price }
]);
});
test('union of subscript integer three keys followed by member-child-identifier', function() {
var results = jp.nodes(data, "$.store.book[1,2,3]['title']");
assert.deepEqual(results, [
{ path: ['$', 'store', 'book', 1, 'title'], value: data.store.book[1].title },
{ path: ['$', 'store', 'book', 2, 'title'], value: data.store.book[2].title },
{ path: ['$', 'store', 'book', 3, 'title'], value: data.store.book[3].title }
]);
});
test('union of subscript integer three keys followed by union of subscript string literal three keys', function() {
var results = jp.nodes(data, "$.store.book[0,1,2,3]['title','author','price']");
assert.deepEqual(results, [
{ path: ['$', 'store', 'book', 0, 'title'], value: data.store.book[0].title },
{ path: ['$', 'store', 'book', 0, 'author'], value: data.store.book[0].author },
{ path: ['$', 'store', 'book', 0, 'price'], value: data.store.book[0].price },
{ path: ['$', 'store', 'book', 1, 'title'], value: data.store.book[1].title },
{ path: ['$', 'store', 'book', 1, 'author'], value: data.store.book[1].author },
{ path: ['$', 'store', 'book', 1, 'price'], value: data.store.book[1].price },
{ path: ['$', 'store', 'book', 2, 'title'], value: data.store.book[2].title },
{ path: ['$', 'store', 'book', 2, 'author'], value: data.store.book[2].author },
{ path: ['$', 'store', 'book', 2, 'price'], value: data.store.book[2].price },
{ path: ['$', 'store', 'book', 3, 'title'], value: data.store.book[3].title },
{ path: ['$', 'store', 'book', 3, 'author'], value: data.store.book[3].author },
{ path: ['$', 'store', 'book', 3, 'price'], value: data.store.book[3].price }
]);
});
test('union of subscript integer four keys, including an inexistent one, followed by union of subscript string literal three keys', function() {
var results = jp.nodes(data, "$.store.book[0,1,2,3,151]['title','author','price']");
assert.deepEqual(results, [
{ path: ['$', 'store', 'book', 0, 'title'], value: data.store.book[0].title },
{ path: ['$', 'store', 'book', 0, 'author'], value: data.store.book[0].author },
{ path: ['$', 'store', 'book', 0, 'price'], value: data.store.book[0].price },
{ path: ['$', 'store', 'book', 1, 'title'], value: data.store.book[1].title },
{ path: ['$', 'store', 'book', 1, 'author'], value: data.store.book[1].author },
{ path: ['$', 'store', 'book', 1, 'price'], value: data.store.book[1].price },
{ path: ['$', 'store', 'book', 2, 'title'], value: data.store.book[2].title },
{ path: ['$', 'store', 'book', 2, 'author'], value: data.store.book[2].author },
{ path: ['$', 'store', 'book', 2, 'price'], value: data.store.book[2].price },
{ path: ['$', 'store', 'book', 3, 'title'], value: data.store.book[3].title },
{ path: ['$', 'store', 'book', 3, 'author'], value: data.store.book[3].author },
{ path: ['$', 'store', 'book', 3, 'price'], value: data.store.book[3].price }
]);
});
test('union of subscript integer three keys followed by union of subscript string literal three keys, followed by inexistent literal key', function() {
var results = jp.nodes(data, "$.store.book[0,1,2,3]['title','author','price','fruit']");
assert.deepEqual(results, [
{ path: ['$', 'store', 'book', 0, 'title'], value: data.store.book[0].title },
{ path: ['$', 'store', 'book', 0, 'author'], value: data.store.book[0].author },
{ path: ['$', 'store', 'book', 0, 'price'], value: data.store.book[0].price },
{ path: ['$', 'store', 'book', 1, 'title'], value: data.store.book[1].title },
{ path: ['$', 'store', 'book', 1, 'author'], value: data.store.book[1].author },
{ path: ['$', 'store', 'book', 1, 'price'], value: data.store.book[1].price },
{ path: ['$', 'store', 'book', 2, 'title'], value: data.store.book[2].title },
{ path: ['$', 'store', 'book', 2, 'author'], value: data.store.book[2].author },
{ path: ['$', 'store', 'book', 2, 'price'], value: data.store.book[2].price },
{ path: ['$', 'store', 'book', 3, 'title'], value: data.store.book[3].title },
{ path: ['$', 'store', 'book', 3, 'author'], value: data.store.book[3].author },
{ path: ['$', 'store', 'book', 3, 'price'], value: data.store.book[3].price }
]);
});
test('union of subscript 4 array slices followed by union of subscript string literal three keys', function() {
var results = jp.nodes(data, "$.store.book[0:1,1:2,2:3,3:4]['title','author','price']");
assert.deepEqual(results, [
{ path: ['$', 'store', 'book', 0, 'title'], value: data.store.book[0].title },
{ path: ['$', 'store', 'book', 0, 'author'], value: data.store.book[0].author },
{ path: ['$', 'store', 'book', 0, 'price'], value: data.store.book[0].price },
{ path: ['$', 'store', 'book', 1, 'title'], value: data.store.book[1].title },
{ path: ['$', 'store', 'book', 1, 'author'], value: data.store.book[1].author },
{ path: ['$', 'store', 'book', 1, 'price'], value: data.store.book[1].price },
{ path: ['$', 'store', 'book', 2, 'title'], value: data.store.book[2].title },
{ path: ['$', 'store', 'book', 2, 'author'], value: data.store.book[2].author },
{ path: ['$', 'store', 'book', 2, 'price'], value: data.store.book[2].price },
{ path: ['$', 'store', 'book', 3, 'title'], value: data.store.book[3].title },
{ path: ['$', 'store', 'book', 3, 'author'], value: data.store.book[3].author },
{ path: ['$', 'store', 'book', 3, 'price'], value: data.store.book[3].price }
]);
});
test('nested parentheses eval', function() {
var pathExpression = '$..book[?( @.price && (@.price + 20 || false) )]'
var results = jp.query(data, pathExpression);
assert.deepEqual(results, data.store.book);
});
test('array indexes from 0 to 100', function() {
var data = [];
for (var i = 0; i <= 100; ++i)
data[i] = Math.random();
for (var i = 0; i <= 100; ++i) {
var results = jp.query(data, '$[' + i.toString() + ']');
assert.deepEqual(results, [data[i]]);
}
});
test('descendant subscript numeric literal', function() {
var data = [ 0, [ 1, 2, 3 ], [ 4, 5, 6 ] ];
var results = jp.query(data, '$..[0]');
assert.deepEqual(results, [ 0, 1, 4 ]);
});
test('descendant subscript numeric literal', function() {
var data = [ 0, 1, [ 2, 3, 4 ], [ 5, 6, 7, [ 8, 9 , 10 ] ] ];
var results = jp.query(data, '$..[0,1]');
assert.deepEqual(results, [ 0, 1, 2, 3, 5, 6, 8, 9 ]);
});
test('throws for no input', function() {
assert.throws(function() { jp.query() }, /needs to be an object/);
});
test('throws for bad input', function() {
assert.throws(function() { jp.query("string", "string") }, /needs to be an object/);
});
test('throws for bad input', function() {
assert.throws(function() { jp.query({}, null) }, /we need a path/);
});
test('throws for bad input', function() {
assert.throws(function() { jp.query({}, 42) }, /we need a path/);
});
test('union on objects', function() {
assert.deepEqual(jp.query({a: 1, b: 2, c: null}, '$..["a","b","c","d"]'), [1, 2, null]);
});
});