-
Notifications
You must be signed in to change notification settings - Fork 16
/
condition.js
65 lines (63 loc) · 1.65 KB
/
condition.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
import jsonpath from 'jsonpath';
import jmespath from 'jmespath';
import { JSONPath } from 'jsonpath-plus';
import Nimma from 'nimma';
import objectScan from '../../../src/index.js';
const commentObjectScan = 'Only in code logic';
export default {
_name: 'Conditional Path',
_fixture: 'cond',
objectScanCompiled: {
comment: commentObjectScan,
fn: objectScan(['*[*].y'], {
breakFn: ({ depth, value }) => depth === 2 && value?.x !== 'yes'
}),
result: [['a', 0, 'y']]
},
objectScan: {
comment: commentObjectScan,
fn: (v) => objectScan(['*[*].y'], {
breakFn: ({ depth, value }) => depth === 2 && value?.x !== 'yes'
})(v),
result: [['a', 0, 'y']]
},
jsonpath: {
fn: (v) => jsonpath.paths(v, "$.*[?(@.x == 'yes')].y"),
result: [['$', 'a', 0, 'y']]
},
jsonpathplus: {
fn: (v) => JSONPath({ path: "$.*[?(@.x == 'yes')].y", json: v, resultType: 'path' }),
result: ["$['a'][0]['y']"]
},
jmespath: {
fn: (v) => jmespath.search(v, "*[?(x == 'yes')].y"),
result: [[2], []]
},
nimma: {
fn: (v) => {
const result = [];
new Nimma(["$.*[?(@.x == 'yes')].y"]).query(v, {
"$.*[?(@.x == 'yes')].y": ({ path }) => {
result.push(path.slice(0));
}
});
return result;
},
result: [['a', 0, 'y']]
},
nimmaCompiled: {
fn: (() => {
const n = new Nimma(["$.*[?(@.x == 'yes')].y"]);
return (v) => {
const result = [];
n.query(v, {
"$.*[?(@.x == 'yes')].y": ({ path }) => {
result.push(path.slice(0));
}
});
return result;
};
})(),
result: [['a', 0, 'y']]
}
};