-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
test.js
289 lines (246 loc) · 7.9 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
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
/*!
* set-value <https://github.com/jonschlinkert/set-value>
*
* Copyright (c) 2014-2018, Jon Schlinkert.
* Released under the MIT License.
*/
'use strict';
require('mocha');
const split = require('split-string');
const assert = require('assert');
const set = require('./');
describe('set', function() {
it('should return non-objects', function() {
var res = set('foo', 'a.b', 'c');
assert.equal(res, 'foo');
res = set(null, 'a.b', 'c');
assert.equal(res, null);
});
it('should create a nested property if it does not already exist', function() {
const o = {};
set(o, 'a.b', 'c');
assert.equal(o.a.b, 'c');
});
it('should create a nested array if it does not already exist', function() {
const o = {};
set(o, 'a.0', 'c');
set(o, 'a.1', 'd');
assert(Array.isArray(o.a));
assert.equal(o.a[0], 'c');
let actual = "";
o.a.map(i=> actual +=i);
assert.equal(actual, "cd");
});
it('should merge an existing value with the given value', function() {
var o = {a: {b: {c: 'd'}}};
set(o, 'a.b', {y: 'z'}, { merge: true });
assert.deepEqual(o.a.b, {c: 'd', y: 'z'});
});
it('should update an object value', function() {
var o = {};
set(o, 'a', {b: 'c'});
set(o, 'a', {c: 'd'}, { merge: true });
assert.deepEqual(o, {a: {b: 'c', c: 'd'}});
set(o, 'a', 'b');
assert.equal(o.a, 'b');
});
it('should extend an array', function() {
const o = {a: []};
set(o, 'a.0', {y: 'z'});
assert(Array.isArray(o.a));
assert.deepEqual(o.a[0], {y: 'z'});
});
it('should extend a function', function() {
function log() {}
const warning = function() {};
const o = {};
set(o, 'helpers.foo', log);
set(o, 'helpers.foo.warning', warning);
assert.equal(typeof o.helpers.foo, 'function');
assert.equal(typeof o.helpers.foo.warning, 'function');
});
it('should extend an object in an array', function() {
var o = {a: [{}, {}, {}]};
set(o, 'a.0.a', {y: 'z'});
set(o, 'a.1.b', {y: 'z'});
set(o, 'a.2.c', {y: 'z'});
assert(Array.isArray(o.a));
assert.deepEqual(o.a[0].a, {y: 'z'});
assert.deepEqual(o.a[1].b, {y: 'z'});
assert.deepEqual(o.a[2].c, {y: 'z'});
});
it('should create a deeply nested property if it does not already exist', function() {
var o = {};
set(o, 'a.b.c.d.e', 'c');
assert.equal(o.a.b.c.d.e, 'c');
});
it('should not create a nested property if it does already exist', function() {
var first = {name: 'Halle'};
var o = {a: first };
set(o, 'a.b', 'c');
assert.equal(o.a.b, 'c');
assert.equal(o.a, first);
assert.equal(o.a.name, 'Halle');
});
it('should support immediate properties', function() {
var o = {};
set(o, 'a', 'b');
assert.equal(o.a, 'b');
});
it('should use property paths to set nested values from the source object.', function() {
var o = {};
set(o, 'a.locals.name', {first: 'Brian'});
set(o, 'b.locals.name', {last: 'Woodward'});
set(o, 'b.locals.name.last', 'Woodward');
assert.deepEqual(o, { a: {locals: {name: { first: 'Brian' }} }, b: {locals: {name: { last: 'Woodward' }} }});
});
it('should add the property even if a value is not defined', function() {
var fixture = {};
assert.deepEqual(set(fixture, 'a.locals.name'), {a: {locals: { name: undefined }}});
assert.deepEqual(set(fixture, 'b.locals.name'), {b: {locals: { name: undefined }}, a: {locals: { name: undefined }}});
});
it('should set the specified property.', function() {
assert.deepEqual(set({a: 'aaa', b: 'b'}, 'a', 'bbb'), {a: 'bbb', b: 'b'});
});
it('should support passing an array as the key', function() {
var actual = set({a: 'a', b: {c: 'd'}}, ['b', 'c', 'd'], 'eee');
assert.deepEqual(actual, {a: 'a', b: {c: {d: 'eee'}}});
});
it('should set a deeply nested value.', function() {
var actual = set({a: 'a', b: {c: 'd'}}, 'b.c.d', 'eee');
assert.deepEqual(actual, {a: 'a', b: {c: {d: 'eee'}}});
});
it('should return the entire object if no property is passed.', function() {
assert.deepEqual(set({a: 'a', b: {c: 'd'}}), {a: 'a', b: {c: 'd'}});
});
it('should set a value only.', function() {
assert.deepEqual(set({a: 'a', b: {c: 'd'}}, 'b.c'), {a: 'a', b: {c: undefined}});
});
it('should set non-plain objects', function(done) {
var o = {};
set(o, 'a.b', new Date());
var firstDate = o.a.b.getTime();
setTimeout(function() {
set(o, 'a.b', new Date());
var secondDate = o.a.b.getTime();
assert.notDeepEqual(firstDate, secondDate);
done();
}, 10);
});
});
describe('escaping', function() {
it('should not split escaped dots', function() {
var o = {};
set(o, 'a\\.b.c.d.e', 'c', { escape: true });
assert.equal(o['a.b'].c.d.e, 'c');
});
it('should work with multiple escaped dots', function() {
var obj1 = {};
set(obj1, 'e\\.f\\.g', 1, { escape: true });
assert.equal(obj1['e.f.g'], 1);
var obj2 = {};
set(obj2, 'e\\.f.g\\.h\\.i.j', 1, { escape: true });
assert.deepEqual(obj2, { 'e.f': { 'g.h.i': { j: 1 } } });
});
it('should work with escaped dots as the last character', function() {
var o = {};
set(o, 'a\\.b.c.d\\.e\\.', 'c', { escape: true });
assert.equal(o['a.b'].c['d.e.'], 'c');
});
});
describe('options', function() {
const options = {
split: function(segment) {
return split(segment, { separator: '.', brackets: true });
}
};
it('should use a custom function to not split inside double quotes', function() {
var o = {};
set(o, 'a."b.c.d".e', 'c', options);
assert.equal(o.a['b.c.d'].e, 'c');
});
it('should use a custom function to not split inside single quotes', function() {
var o = {};
set(o, "a.'b.c.d'.e", 'c', options);
assert.equal(o.a['b.c.d'].e, 'c');
});
it('should use a custom function to not split inside square brackets', function() {
var o = {};
set(o, 'a.[b.c.d].e', 'c', options);
assert.equal(o.a['[b.c.d]'].e, 'c');
});
it('should use a custom function to not split inside parens', function() {
var o = {};
set(o, 'a.(b.c.d).e', 'c', options);
assert.equal(o.a['(b.c.d)'].e, 'c');
});
it('should use a custom function to not split inside angle brackets', function() {
var o = {};
set(o, 'a.<b.c.d>.e', 'c', options);
assert.equal(o.a['<b.c.d>'].e, 'c');
});
it('should use a custom function to not split inside curly braces', function() {
var o = {};
set(o, 'a.{b.c.d}.e', 'c', options);
assert.equal(o.a['{b.c.d}'].e, 'c');
});
});
const Rx = require('rxjs');
const opers = require('rxjs/operators');
var _value = 0;
var o = {a: { b: {} } };
var obs = Rx.from(new Rx.BehaviorSubject()).pipe(
opers.skip(1),
);
Object.defineProperty(o.a.b, 'c', {
configurable: true,
get() { return _value; },
set(value) {
_value = value;
obs.next(value);
}
});
describe('Setter with Observable', function() {
// const expected = 11;
var received = [];
const noop = () => {};
it('should only assign/emit once for each call of set', function(done) {
var subs = obs.subscribe(
data => { received.push(data); },
noop,
() => {
assert.equal(received.length, 1);
done();
}
);
set(o, 'a.b.c', 5);
subs.complete();
});
it('should work assignment via setter', function(done) {
received = null;
var subs = obs.subscribe(
data => { received = data; },
noop,
() => {
assert.equal(received, 10);
done();
}
);
set(o, 'a.b.c', 10);
subs.complete();
});
it('should work with merge of object via setter', function(done) {
received = null;
set(o, 'a.b.c', {foo: 'bar'});
var subs = obs.subscribe(
data => { received = data; },
noop,
() => {
assert.deepEqual(o.a.b.c, { foo: 'bar', bing: 'bong' });
done();
}
);
set(o, 'a.b.c', { bing: 'bong'}, {merge: true});
subs.complete();
});
});