-
-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathtest.callback.js
More file actions
209 lines (198 loc) · 6.29 KB
/
Copy pathtest.callback.js
File metadata and controls
209 lines (198 loc) · 6.29 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
describe('JSONPath - Callback', function () {
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('Callback', () => {
const expected = ['value', json.store.bicycle, {path: "$['store']['bicycle']", value: json.store.bicycle, parent: json.store, parentProperty: 'bicycle', hasArrExpr: undefined}];
/**
* @type {undefined|(string|object)[]}
*/
let result;
/**
*
* @param {object} data
* @param {string} type
* @param {object} fullData
* @returns {void}
*/
function callback (data, type, fullData) {
if (!result) {
result = [];
}
result.push(type, data, fullData);
}
jsonpath({json, path: '$.store.bicycle', resultType: 'value', wrap: false, callback});
assert.deepEqual(result, expected);
});
it('Callback with `resultType`: "all"', () => {
const expected = [
'value',
{
path: "$['store']['bicycle']",
value: json.store.bicycle,
parent: json.store,
parentProperty: 'bicycle',
pointer: '/store/bicycle',
hasArrExpr: undefined
},
{
path: "$['store']['bicycle']",
value: json.store.bicycle,
parent: json.store,
parentProperty: 'bicycle',
pointer: '/store/bicycle',
hasArrExpr: undefined
}
];
/**
* @type {undefined|(string|object)[]}
*/
let result;
/**
*
* @param {object} data
* @param {string} type
* @param {object} fullData
* @returns {void}
*/
function callback (data, type, fullData) {
if (!result) {
result = [];
}
result.push(type, data, fullData);
}
jsonpath({json, path: '$.store.bicycle', resultType: 'all', wrap: false, callback});
assert.deepEqual(result?.[0], expected[0]);
assert.deepEqual(result, expected);
});
// https://github.com/s3u/JSONPath/issues/126
it('Using callback to set', function () {
const expected = {
age: 30,
email: 'abc@example.com',
'something_deeper': {
abc: 1,
quantity: 11
},
firstName: 'John',
lastName: 'Doe'
};
const givenPerson = {
age: 30,
email: 'abc@example.com',
// let's add firstName, lastName fields
'something_deeper': {
abc: 1
// let's add quantity here
}
};
// defined an object with "json_path":"value" format,
// made sure it is not a deep object.
const obj1 = {
$: {
'firstName': 'John',
'lastName': 'Doe'
},
'$.something_deeper': {
quantity: 11
}
};
Object.entries(obj1).forEach(([path, valuesToSet]) => {
jsonpath({
json: givenPerson,
path,
wrap: false,
callback (obj) {
Object.entries(valuesToSet).forEach(([key, val]) => {
obj[key] = val;
});
}
});
});
const result = givenPerson;
assert.deepEqual(result, expected);
});
// The callback stringifies `retObj.path` in place, so the returned
// results are built from an already-stringified path.
it('returns unmangled paths with `resultType: "path"` and a callback', () => {
/** @type {unknown[]} */
const seen = [];
const result = jsonpath({
json,
path: '$.store.book[*].author',
resultType: 'path',
callback (preferredOutput) {
seen.push(preferredOutput);
}
});
const expected = [
"$['store']['book'][0]['author']",
"$['store']['book'][1]['author']",
"$['store']['book'][2]['author']",
"$['store']['book'][3]['author']"
];
assert.deepEqual(result, expected);
assert.deepEqual(seen, expected);
});
it('returns unmangled pointers with `resultType: "pointer"` and a callback', () => {
/** @type {unknown[]} */
const seen = [];
const result = jsonpath({
json,
path: '$.store.book[*].author',
resultType: 'pointer',
callback (preferredOutput) {
seen.push(preferredOutput);
}
});
const expected = [
'/store/book/0/author',
'/store/book/1/author',
'/store/book/2/author',
'/store/book/3/author'
];
assert.deepEqual(result, expected);
assert.deepEqual(seen, expected);
});
it('returns an unmangled path with `resultType: "path"`, `wrap: false`, and a callback', () => {
const result = jsonpath({
json,
path: '$.store.bicycle.color',
resultType: 'path',
wrap: false,
callback () { /* */ }
});
assert.deepEqual(result, "$['store']['bicycle']['color']");
});
});