-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyadeepTests.ts
276 lines (239 loc) · 7.78 KB
/
yadeepTests.ts
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
/*
University of Illinois/NCSA Open Source License
Copyright (c) 2018 Terrain Data, Inc. and the authors. All rights reserved.
Developed by: Terrain Data, Inc. and
the individuals who committed the code in this file.
https://github.com/terraindata/yadeep
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
(the "Software"), to deal with the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimers.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimers in the
documentation and/or other materials provided with the distribution.
* Neither the names of Terrain Data, Inc., Terrain, nor the names of its
contributors may be used to endorse or promote products derived from
this Software without specific prior written permission.
This license supersedes any copyright notice, license, or related statement
following this comment block. All files in this repository are provided
under the same license, regardless of whether a corresponding comment block
appears in them. This license also applies retroactively to any previous
state of the repository, including different branches and commits, which
were made public on or after December 8th, 2018.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
THE SOFTWARE.
*/
// Copyright 2018 Terrain Data, Inc.
import * as _ from 'lodash';
import objectify from 'deep-objectify';
import { KeyPath } from 'terrain-keypath';
import * as yadeep from './yadeep';
const doc3: object = {
name: 'Bob',
arr: ['sled', [{ a: 'dog' }, { b: 'doggo', a: 'fren' }]],
hardarr: [['a'], ['b', ['c']]],
};
const objd: object = objectify(doc3);
test('simple top-level get', () =>
{
expect(yadeep.get(objd, KeyPath(['name']))).toBe('Bob');
});
test('primitive get one layer deep', () =>
{
expect(yadeep.get(objd, KeyPath(['arr', '0']))).toBe('sled');
});
test('nested array get', () =>
{
expect(yadeep.get(objd, KeyPath(['hardarr', '1', '1']))).toEqual({ 0: 'c' });
});
test('wildcard nested get', () =>
{
expect(yadeep.get(objd, KeyPath(['arr', '1', -1, 'a']))).toEqual(['dog', 'fren']);
});
test('double wildcard sensible nested get', () =>
{
expect(yadeep.get(objd, KeyPath(['arr', -1, -1, 'a']))).toEqual([undefined, ['dog', 'fren']]);
});
test('simple top-level set', () =>
{
const copy: any = { ...objd };
yadeep.set(copy, KeyPath(['name']), 'jim');
expect(copy['name']).toBe('jim');
});
test('a deep set', () =>
{
const copy: any = { ...objd };
yadeep.set(copy, KeyPath(['arr', '1', '0', 'a']), 'jim');
expect(copy['arr']['1']['0']['a']).toBe('jim');
});
test('a deep set with a wildcard', () =>
{
const copy: any = { ...objd };
yadeep.set(copy, KeyPath(['arr', '1', -1, 'a']), 'jim');
expect(copy['arr']['1']['0']['a']).toBe('jim');
expect(copy['arr']['1']['1']['a']).toBe('jim');
});
test('basic search', () =>
{
const copy: object = _.cloneDeep(doc3);
const singleMatches = yadeep.search(copy, KeyPath(['name']));
expect(singleMatches).toEqual([{
value: 'Bob',
location: KeyPath(['name']),
}]);
const multiMatches = yadeep.search(copy, KeyPath(['arr', -1, -1]));
expect(multiMatches.length).toBe(2);
expect(multiMatches).toContainEqual({
value: { a: 'dog' },
location: KeyPath(['arr', 1, 0]),
});
expect(multiMatches).toContainEqual({
value: { b: 'doggo', a: 'fren' },
location: KeyPath(['arr', 1, 1]),
});
});
test('negative basic search', () =>
{
const copy: object = _.cloneDeep(doc3);
const matches = yadeep.search(copy, KeyPath(['namez']));
expect(matches).toEqual([]);
const deepMatches = yadeep.search(copy, KeyPath(['arr', -1, -1, 'c']));
expect(deepMatches).toEqual([]);
});
test('falsy search', () =>
{
const doc: object = {
nullValue: null,
undefinedValue: undefined,
falseValue: false,
nestedFalsies: [
0,
{
nullAgain: null,
undefinedAgain: undefined,
},
null,
'',
undefined,
],
};
expect(yadeep.search(doc, KeyPath(['nullValue']))).toEqual([
{
value: null,
location: KeyPath(['nullValue']),
},
]);
expect(yadeep.search(doc, KeyPath(['undefinedValue']))).toEqual([
{
value: undefined,
location: KeyPath(['undefinedValue']),
},
]);
expect(yadeep.search(doc, KeyPath(['falseValue']))).toEqual([
{
value: false,
location: KeyPath(['falseValue']),
},
]);
const deepSearch = yadeep.search(doc, KeyPath(['nestedFalsies', -1]));
expect(deepSearch.length).toBe(5);
expect(deepSearch.map((match) => match.value)).toEqual([
0, { nullAgain: null, undefinedAgain: undefined }, null, '', undefined,
]);
});
test('search long keypath', () =>
{
const copy: object = _.cloneDeep(doc3);
expect(yadeep.search(copy, KeyPath(['arr', -1, -1, 'a', 'b']))).toEqual([]);
expect(yadeep.search(copy, KeyPath(['arr', -1, -1, 'a', -1]))).toEqual([]);
expect(yadeep.search(copy, KeyPath(['arr', -1, -1, -1, 'a']))).toEqual([]);
});
test('search empty keypath', () =>
{
const copy: object = _.cloneDeep(doc3);
expect(yadeep.search(copy, KeyPath([]))[0].value).toEqual(doc3);
});
test('array search', () =>
{
const copy: object = _.cloneDeep(doc3);
const matches = yadeep.search(copy, KeyPath(['arr', -1]));
expect(matches).toContainEqual({
value: 'sled',
location: KeyPath(['arr', 0]),
});
expect(matches).toContainEqual({
value: [{ a: 'dog' }, { b: 'doggo', a: 'fren' }],
location: KeyPath(['arr', 1]),
});
});
test('only find existing search', () =>
{
const doc: object = {
items: [
{
foo: 5,
},
{
notFoo: 'hi',
},
{
foo: [1, 2, 3],
},
],
};
const matches = yadeep.search(doc, KeyPath(['items', -1, 'foo']));
const justTheValues = matches.map((match) => match.value);
expect(justTheValues).not.toContain(undefined);
expect(justTheValues).not.toContain(null);
});
test('search specific indexed keypath', () =>
{
const copy: object = _.cloneDeep(doc3);
const matches = yadeep.search(copy, KeyPath(['arr', 1, 0, 'a']));
expect(matches).toEqual([{
value: 'dog',
location: KeyPath(['arr', 1, 0, 'a']),
}]);
});
test('trick numeric index', () =>
{
const doc: object = {
notArr: {
'-1': 100,
'0': 200,
'1': 300,
},
arr: [
10,
20,
30,
],
};
// search on notArr
expect(yadeep.search(doc, KeyPath(['notArr', '0']))).toEqual([{
value: 200,
location: KeyPath(['notArr', '0']),
}]);
expect(yadeep.search(doc, KeyPath(['notArr', '-1']))).toEqual([{
value: 100,
location: KeyPath(['notArr', '-1']),
}]);
expect(yadeep.search(doc, KeyPath(['notArr', 1]))).toEqual([]);
expect(yadeep.search(doc, KeyPath(['notArr', -1]))).toEqual([]);
expect(yadeep.search(doc, KeyPath(['arr', 0]))).toEqual([{
value: 10,
location: KeyPath(['arr', 0]),
}]);
expect(yadeep.search(doc, KeyPath(['arr', '1']))).toEqual([]);
expect(yadeep.search(doc, KeyPath(['arr', '-1']))).toEqual([]);
});