forked from jonschlinkert/set-value
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
204 lines (170 loc) · 5.96 KB
/
Copy pathtest.js
File metadata and controls
204 lines (170 loc) · 5.96 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
/*!
* 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() {
const res = set('foo', 'a.b', 'c');
assert.equal(res, 'foo');
});
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 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 } } });
});
});
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');
});
});