-
Notifications
You must be signed in to change notification settings - Fork 666
/
Copy pathtest312.js
354 lines (308 loc) · 9.49 KB
/
test312.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
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
if (typeof exports === 'object') {
var assert = require('assert');
var alasql = require('..');
} else {
__dirname = '.';
}
describe('Test 312 JSON traverse', function () {
/*
### How to search deep nested JSON?
Source: [StackOverflow] (http://stackoverflow.com/questions/30091572/search-deep-nested-json)
<blockquote>
I am working on a solution where I need to search for an element in a deeply nested JSON by its id. I have been advised to use underscore.js which I am pretty new to.
After reading the documentation http://underscorejs.org/#find , I tried to implement the solution using find, filter and findWhere.
The issue I faced is that these functions check the top level of the JSON and not the nested properties thus returning undefined. I tried to use item.catalog && item.catalog.uid == 1; logic as suggested in a similar question Underscore.js - filtering in a nested Json but failed.
How can I find an item by value by searching the whole deeply nested JSON?
</blockquote>
var test = {
"menuInputRequestId": 1,
"catalog":[
{
"uid": 1,
"name": "Pizza",
"desc": "Italian cuisine",
"products": [
{
"uid": 3,
"name": "Devilled chicken",
"desc": "chicken pizza",
},
// ...
]
}
]
};
*/
it('1. How to search deep nested JSON?', function (done) {
var data = {
menuInputRequestId: 1,
catalog: [
{
uid: 1,
name: 'Pizza',
desc: 'Italian cuisine',
products: [
{
uid: 3,
name: 'Devilled chicken',
desc: 'chicken pizza',
prices: [
{
uid: 7,
name: 'regular',
price: '$10',
},
{
uid: 8,
name: 'large',
price: '$12',
},
],
},
],
},
{
uid: 2,
name: 'Pasta',
desc: 'Italian cuisine pasta',
products: [
{
uid: 4,
name: 'Lasagne',
desc: 'chicken lasage',
prices: [
{
uid: 9,
name: 'small',
price: '$10',
},
{
uid: 10,
name: 'large',
price: '$15',
},
],
},
{
uid: 5,
name: 'Pasta',
desc: 'chicken pasta',
prices: [
{
uid: 11,
name: 'small',
price: '$8',
},
{
uid: 12,
name: 'large',
price: '$12',
},
],
},
],
},
],
};
var res = alasql('SEARCH / * WHERE(uid=1) name FROM ?', [data]);
assert.deepEqual(res, ['Pizza']);
done();
});
it('2. How do I traverse a complex JSON doc with javascript and extract named values', function (done) {
/*
Source: http://stackoverflow.com/questions/29966520/how-do-i-traverse-a-complex-json-doc-with-javascript-and-extract-named-values
It has a specific problem to solve, extracting a named value from Json,
I need some javascript to traverse reasonably complex json with nested objects and arrays, and extract values. Example json is
*/
var data = {
query: {
filtered: {
query: {
match_all: {},
},
filter: {
and: {
filters: [
{
terms: {
ACCOUNT_NUMBER: ['37846589', '37846540'],
},
},
],
},
},
},
},
};
var res = alasql('SEARCH /+ACCOUNT_NUMBER/ FROM ?', [data]);
assert.deepEqual(res, ['37846589', '37846540']);
done();
});
it('3. Find all parents elements in a Json file', function (done) {
/*
http://stackoverflow.com/questions/29937203/find-all-parents-elements-in-a-json-file-using-jquery/29937369#29937369
Find all parents elements in a Json file?
I'm currently working on a recursive menu which is built on top of jQuery which looks quite good already.
The structure of the JSon file containing the menu looks as the following:
[
{
"Id": "menuOfficeWebControlsForWebApplication",
"Title": "Office Web Controls",
"Resource": "/Documentation/Data/index.html" },
{
"Id": "menuGettingStarted",
"Title": "Getting Started",
"Resource": "/Documentation/Data/getting-started.html",
"Categories": [{
"Id": "menuCompilingFromSource",
"Title": "Compiling From Source",
"Resource": "/Documentation/Data/Getting-Started/compiling-from-source.html"
},{
"Id": "menuDownloadReleasePackage",
"Title": "Download Release Package",
"Resource": "/Documentation/Data/Getting-Started/downloading-release-package.html"
},{
"Id": "menuBuildingYourFirstApplication",
"Title": "Building your first application",
"Resource": "/Documentation/Data/Getting-Started/building-your-first-application.html"
}]
}
]
Now, I want to retrieve all the elements which are at a higher level and all the items which are directly below that item.
*/
var data = [
{
Id: 'menuOfficeWebControlsForWebApplication',
Title: 'Office Web Controls',
Resource: '/Documentation/Data/index.html',
},
{
Id: 'menuGettingStarted',
Title: 'Getting Started',
Resource: '/Documentation/Data/getting-started.html',
Categories: [
{
Id: 'menuCompilingFromSource',
Title: 'Compiling From Source',
Resource: '/Documentation/Data/Getting-Started/compiling-from-source.html',
},
{
Id: 'menuDownloadReleasePackage',
Title: 'Download Release Package',
Resource: '/Documentation/Data/Getting-Started/downloading-release-package.html',
},
{
Id: 'menuBuildingYourFirstApplication',
Title: 'Building your first application',
Resource: '/Documentation/Data/Getting-Started/building-your-first-application.html',
},
],
},
];
// The answer
var res = alasql('SEARCH /(Categories/)? WHERE(Id) FROM ?', [data]);
// Fro test
var res = alasql('SEARCH /(Categories/)? Id FROM ?', [data]);
assert.deepEqual(res, [
'menuOfficeWebControlsForWebApplication',
'menuGettingStarted',
'menuCompilingFromSource',
'menuDownloadReleasePackage',
'menuBuildingYourFirstApplication',
]);
done();
});
/*
recursive find and replace in multidimensional javascript object
http://stackoverflow.com/questions/29473526/recursive-find-and-replace-in-multidimensional-javascript-object
I need to find and replace values in my object when they match a regular expression (e.g. **myVar**); The object that I need to loop through is user defined and structure varies.
Here is an example object, shortened for simplicity.
var testObject = {
name: "/pricing-setups/{folderId}",
method: "POST",
endpoint: "/pricing-setups/:folderId",
functionName: "create",
Consumes: null,
filename: "apicontracts/pricingsetups/PricingSetupServiceProxy.java",
pathParam: [
{$$hashKey: "06S",
key: "folderId",
value: "**myVar**"}
],
queryParam: [],
request_payload: "{'title':'EnterAname'}",
returnList: []
}
This object is passed into a master function that builds a angularjs resource object using the passed in object.
*/
it('4. Recursive find and replace in multidimensional javascript object', function (done) {
var data = {
name: '/pricing-setups/{folderId}',
method: 'POST',
endpoint: '/pricing-setups/:folderId',
functionName: 'create',
// Consumes: null,
filename: 'apicontracts/pricingsetups/PricingSetupServiceProxy.java',
pathParam: [
{
$$hashKey: '06S',
key: 'folderId',
value: '**myVar**',
},
],
queryParam: [],
request_payload: "{'title':'EnterAname'}",
returnList: [],
};
// Fro test
// var res = alasql('SEARCH / * AS @obj KEYS() WHERE(@obj->(_) LIKE "%myVar%") FROM ?', [data]);
// var res = alasql('SEARCH / * IF(WHERE(_ LIKE "%myVar%") \
// SET(val=val->replace("")) FROM ?', [data]);
// KEYS();
// console.log(res);
// assert.deepEqual(res,[ 'menuOfficeWebControlsForWebApplication',
// 'menuGettingStarted',
// 'menuCompilingFromSource',
// 'menuDownloadReleasePackage',
// 'menuBuildingYourFirstApplication' ]);
done();
});
it('5. Recursive find and replace in multidimensional javascript object', function (done) {
/*
http://stackoverflow.com/questions/23024589/javascript-nested-object-to-multidimensional-array-recursive-function?rq=1
Javascript Nested object to multidimensional array recursive function
I am using this Lucene Query Parser to parse a string/query which produce this kind of data structure:
// Notice that the repetition of 'field3' is on purpose
Sample String: field1:val1 AND field2:val2 OR field3:val3 AND field3:val4
Result:
{ left: { field: "field1", term: "val1" },
operator: "AND"
right: {
left: { field: "field2", term: "val2" },
operator: "OR"
right: {
left: {field: "field3", term: "val3" },
operator: "AND",
right: {
field: "field3",
term: "val4"
}
}
}
I need to iterate on that object to obtain the following:
[ [{ field: "field1", term: "val1"},
{ field: "field2", term: "val2"}
],
[{ field: "field3", term: "val3"},
{ field: "field3", term: "val4"}
]
]
If I try to explain this, the idea is to create an array
of arrays where each child array are separated by "OR",
while each objects inside the child arrays represents the
"AND" separated fields; Although I think the code above explains
it better than me
*/
done();
});
});