-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.js
218 lines (184 loc) · 10.8 KB
/
test.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
const auger = require('..');
const noop = function () { };
describe('auger.get', function () {
test('should return object when no other parameters provided', function () {
expect(auger.get({ a: 'b' })).toStrictEqual({ a: 'b' });
expect(auger.get({ a: { b: 'c' } })).toStrictEqual({ a: { b: 'c' } });
expect(auger.get(['a', { b: 'c' }])).toStrictEqual(['a', { b: 'c' }]);
});
test('should return object when empty array is provided as path', function () {
expect(auger.get({ a: 'b' }, [])).toStrictEqual({ a: 'b' });
expect(auger.get({ a: { b: 'c' } }, [])).toStrictEqual({ a: { b: 'c' } });
expect(auger.get(['a', { b: 'c' }], [])).toStrictEqual(['a', { b: 'c' }]);
});
test('should return undefined when non-object is provided', function () {
expect(auger.get()).toBeUndefined();
expect(auger.get('str')).toBeUndefined();
expect(auger.get(123)).toBeUndefined();
expect(auger.get(undefined)).toBeUndefined();
expect(auger.get(null)).toBeUndefined();
expect(auger.get(noop)).toBeUndefined();
});
test('should return a value', function () {
expect(auger.get({ a: 'b' }, ['a'])).toEqual('b');
expect(auger.get({ a: 123 }, ['a'])).toEqual(123);
expect(auger.get({ a: { b: 'c' } }, ['a', 'b'])).toEqual('c');
expect(auger.get({ a: { b: false } }, ['a', 'b'])).toEqual(false);
expect(auger.get({ a: { b: null } }, ['a', 'b'])).toEqual(null);
});
test('should return a value from an array', function () {
expect(auger.get({ a: { b: ['c'] } }, ['a', 'b', 0])).toEqual('c');
expect(auger.get({ a: { b: ['c', { d: 1 }] } }, ['a', 'b', 1, 'd'])).toEqual(1);
expect(auger.get({ a: { b: [false] } }, ['a', 'b', 0])).toEqual(false);
});
test('should return an object', function () {
expect(auger.get({ a: { b: { c: 'd' } } }, ['a'])).toStrictEqual({ b: { c: 'd' } });
expect(auger.get({ a: { b: { c: 'd' } } }, ['a', 'b'])).toStrictEqual({ c: 'd' });
expect(auger.get({ a: { b: [{ c: 'd' }] } }, ['a', 'b', 0])).toStrictEqual({ c: 'd' });
});
test('should return an array', function () {
expect(auger.get({ a: ['b', 'c'] }, ['a'])).toStrictEqual(['b', 'c']);
expect(auger.get({ a: { b: ['c', 'd'] } }, ['a', 'b'])).toStrictEqual(['c', 'd']);
expect(auger.get({ a: { b: [['c', 'd']] } }, ['a', 'b'])).toStrictEqual([['c', 'd']]);
expect(auger.get({ a: { b: [['c', 'd']] } }, ['a', 'b', 0])).toStrictEqual(['c', 'd']);
});
test('should work on a property that uses a number as the key', function () {
expect(auger.get({ a: { "1": "b" } }, ['a', '1'])).toEqual('b');
expect(auger.get({ a: { "1": ['c'] } }, ['a', '1'])).toStrictEqual(['c']);
expect(auger.get({ a: { "1": ['c'] } }, ['a', '1', 0])).toEqual('c');
});
test('should get a property that has dots in the key', function () {
expect(auger.get({ 'a.b': 'c' }, ['a.b'])).toEqual('c');
});
test('should return default fallback', function () {
expect(auger.get(Object.create(null), ['a'])).toBeUndefined();
expect(auger.get({}, ['a'])).toBeUndefined();
expect(auger.get([], [1])).toBeUndefined();
expect(auger.get({}, [1])).toBeUndefined();
expect(auger.get([], ['a'])).toBeUndefined();
expect(auger.get({ a: 'b' }, ['a', 'b'])).toBeUndefined();
expect(auger.get({ a: 'b' }, ['a', 'b', 'c'])).toBeUndefined();
expect(auger.get({a: ['c']}, ['a', '0'])).toBeUndefined();
expect(auger.get({a: {'0': 'c'}}, ['a', 0])).toBeUndefined();
});
test('should return provided fallback', function () {
expect(auger.get(Object.create(null), ['a'], 'Oh no!')).toEqual('Oh no!');
expect(auger.get({}, ['a'], 'Oh no!')).toEqual('Oh no!');
expect(auger.get([], [1], 'Oh no!')).toEqual('Oh no!');
expect(auger.get({}, [1], 'Oh no!')).toEqual('Oh no!');
expect(auger.get([], ['a'], 'Oh no!')).toEqual('Oh no!');
expect(auger.get({ a: 'b' }, ['a', 'b'], 'Oh no!')).toEqual('Oh no!');
expect(auger.get({ a: 'b' }, ['a', 'b', 'c'], 'Oh no!')).toEqual('Oh no!');
});
});
describe('auger.has', function () {
test('should return false when non-object is provided', function () {
expect(auger.has()).toBe(false);
expect(auger.has('str')).toBe(false);
expect(auger.has(123)).toBe(false);
expect(auger.has(undefined)).toBe(false);
expect(auger.has(null)).toBe(false);
expect(auger.has(noop)).toBe(false);
});
test('should return false when no other parameters provided', function () {
expect(auger.has({ a: 'b' })).toBe(false);
expect(auger.has({ a: { b: 'c' } })).toBe(false);
});
test('should return false when empty array is provided as path', function () {
expect(auger.has({ a: 'b' }, [])).toBe(false);
expect(auger.has({ a: { b: 'c' } }, [])).toBe(false);
expect(auger.has(['a', { b: 'c' }], [])).toBe(false);
});
test('should return true', function () {
expect(auger.has({ a: 'b' }, ['a'])).toBe(true);
expect(auger.has({ a: 123 }, ['a'])).toBe(true);
expect(auger.has({a: ['c']}, ['a', 0], 'c')).toBe(true);
expect(auger.has({a: {'0': 'c'}}, ['a', '0'], 'c')).toBe(true);
expect(auger.has({ a: { b: 'c' } }, ['a', 'b'])).toBe(true);
expect(auger.has({ a: { b: false } }, ['a', 'b'])).toBe(true);
expect(auger.has({ a: { b: null } }, ['a', 'b'])).toBe(true);
expect(auger.has({ a: { b: ['c'] } }, ['a', 'b', 0])).toBe(true);
expect(auger.has({ a: { b: ['c', { d: 1 }] } }, ['a', 'b', 1, 'd'])).toBe(true);
expect(auger.has({ a: { b: [false] } }, ['a', 'b', 0])).toBe(true);
expect(auger.has({ a: { b: { c: 'd' } } }, ['a'])).toBe(true);
expect(auger.has({ a: { b: { c: 'd' } } }, ['a', 'b'])).toBe(true);
expect(auger.has({ a: { b: [{ c: 'd' }] } }, ['a', 'b', 0])).toBe(true);
expect(auger.has({ a: ['b', 'c'] }, ['a'])).toBe(true);
expect(auger.has({ a: { b: ['c', 'd'] } }, ['a', 'b'])).toBe(true);
expect(auger.has({ a: { b: [['c', 'd']] } }, ['a', 'b'])).toBe(true);
expect(auger.has({ a: { b: [['c', 'd']] } }, ['a', 'b', 0])).toBe(true);
expect(auger.has({ a: { b: [['c', 'd']] } }, ['a', 'b', 0, 1])).toBe(true);
});
test('should return false', function () {
expect(auger.has(Object.create(null), ['a'])).toBe(false);
expect(auger.has({}, ['a'])).toBe(false);
expect(auger.has([], [1])).toBe(false);
expect(auger.has({}, [1])).toBe(false);
expect(auger.has([], ['a'])).toBe(false);
expect(auger.has({ a: 'b' }, ['a', 'b'])).toBe(false);
expect(auger.has({ a: 'b' }, ['a', 'b', 'c'])).toBe(false);
expect(auger.has({a: ['c']}, ['a', '0'])).toBe(false);
expect(auger.has({a: {'0': 'c'}}, ['a', 0])).toBe(false);
});
test('should work on a property that uses a number as the key', function () {
expect(auger.has({ a: { "1": "b" } }, ['a', '1'])).toBe(true);
expect(auger.has({ a: { "1": "b" } }, ['a', 1])).toBe(false);
expect(auger.has({ a: { "1": ["b"] } }, ['a', '1', 0])).toBe(true);
expect(auger.has({ a: { "1": ["b"] } }, ['a', '1', '0'])).toBe(false);
expect(auger.has({ a: [["b"]] }, ['a', 0, 0])).toBe(true);
expect(auger.has({ a: [["b"]] }, ['a', '0', 0])).toBe(false);
expect(auger.has({ a: [["b"]] }, ['a', 0, '0'])).toBe(false);
});
test('should work on a property that has dots in the key', function () {
expect(auger.has({ 'a.b': 'c' }, ['a.b'])).toBe(true);
});
});
describe('auger.set', function () {
test('should return given value when non-object is provided', function () {
expect(auger.set()).toBeUndefined();
expect(auger.set('str')).toEqual('str');
expect(auger.set(123)).toEqual(123);
expect(auger.set(undefined)).toBeUndefined();
expect(auger.set(null)).toEqual(null);
expect(auger.set(noop)).toStrictEqual(noop);
});
test('should return same object when no other parameters provided', function () {
expect(auger.set({ a: 'b' })).toStrictEqual({ a: 'b' });
expect(auger.set({ a: { b: 'c' } })).toStrictEqual({ a: { b: 'c' } });
expect(auger.set(['a', { b: 'c' }])).toStrictEqual(['a', { b: 'c' }]);
});
test('should return same object when empty array is provided as path', function () {
expect(auger.set({ a: 'b' }, [])).toStrictEqual({ a: 'b' });
expect(auger.set({ a: { b: 'c' } }, [])).toStrictEqual({ a: { b: 'c' } });
expect(auger.set(['a', { b: 'c' }], [])).toStrictEqual(['a', { b: 'c' }]);
});
test('should set value', function () {
expect(auger.set({}, ['a'], 'c')).toStrictEqual({ a: 'c' });
expect(auger.set({}, ['a'], noop)).toStrictEqual({ a: noop });
expect(auger.set({ a: 'b' }, ['a'], 'c')).toStrictEqual({ a: 'c' });
expect(auger.set({ a: { b: 'c' } }, ['a', 'b'], 1)).toStrictEqual({ a: { b: 1 } });
expect(auger.set({ a: { b: false } }, ['a', 'b'], true)).toStrictEqual({ a: { b: true } });
expect(auger.set({ a: { b: null } }, ['a', 'b'], 'null')).toStrictEqual({ a: { b: 'null' } });
expect(auger.set({ a: { b: ['c'] } }, ['a', 'b', 1], 'd')).toStrictEqual({ a: { b: ['c', 'd'] } });
expect(auger.set({ a: { b: { c: 'd' } } }, ['a'], { c: 'd' })).toStrictEqual({ a: { c: 'd' } });
expect(auger.set({ a: { b: [{ c: 'd' }] } }, ['a', 'b', 0], 'c')).toStrictEqual({ a: { b: ['c'] } });
});
test('should set default value (undefined)', function () {
expect(auger.set({}, ['a'])).toStrictEqual({ a: undefined });
expect(auger.set({}, ['a', 0])).toStrictEqual({ a: [undefined] });
expect(auger.set({ a: { b: 'c' } }, ['a', 'b'])).toStrictEqual({ a: { b: undefined } });
});
test('should set as object or array depending on if the value is a string or number', function () {
expect(auger.set({}, ['a', 0], 'c')).toStrictEqual({ a: ['c'] });
expect(auger.set({}, ['a', '0'], 'c')).toStrictEqual({ a: { '0': 'c' } });
expect(auger.set({a: ['b']}, ['a', '0'], 'c')).toStrictEqual({ a: { '0': 'c' } });
expect(auger.set({a: ['b']}, ['a', 0], 'c')).toStrictEqual({ a: ['c'] });
});
test('should work on a property that uses a number as the key', function () {
expect(auger.set({ a: { "1": "b" } }, ['a', '1'], 'c')).toStrictEqual({ a: { "1": "c" } });
expect(auger.set({ a: { "1": ['c'] } }, ['a', '1', 1], 'd')).toStrictEqual({ a: { "1": ['c', 'd'] } });
});
test('should work on a property that has dots in the key', function () {
expect(auger.set({ 'a.b': 'c' }, ['a.b'], 'd')).toStrictEqual({ 'a.b': 'd' });
});
});