Skip to content

Commit 569955d

Browse files
committed
break: overwrite existing non-object values if needed
1 parent 113244b commit 569955d

File tree

2 files changed

+21
-29
lines changed

2 files changed

+21
-29
lines changed

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ export default function (obj, keys, val) {
44
for (; i < l;) {
55
k = keys[i++];
66
if (k === '__proto__' || k === 'constructor' || k === 'prototype') continue;
7-
t = t[k] = (i === l ? val : ((x=t[k]) != null ? x : (keys[i]*0 !== 0 || !!~keys[i].indexOf('.')) ? {} : []));
7+
t = t[k] = (i === l) ? val : (typeof(x=t[k])===typeof(keys)) ? x : (keys[i]*0 !== 0 || !!~keys[i].indexOf('.')) ? {} : [];
88
}
99
}

test/index.js

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -172,20 +172,22 @@ preserves('should preserve existing object structure', () => {
172172
});
173173
});
174174

175-
preserves('should not convert existing non-object values into object', () => {
175+
preserves('should overwrite existing non-object values as object', () => {
176176
let input = {
177177
a: {
178178
b: 123
179179
}
180180
};
181181

182-
let before = JSON.stringify(input);
183182
dset(input, 'a.b.c', 'hello');
184183

185-
assert.is(
186-
JSON.stringify(input),
187-
before
188-
);
184+
assert.equal(input, {
185+
a: {
186+
b: {
187+
c: 'hello'
188+
}
189+
}
190+
});
189191
});
190192

191193
preserves('should preserve existing object tree w/ array value', () => {
@@ -208,26 +210,6 @@ preserves('should preserve existing object tree w/ array value', () => {
208210
});
209211
});
210212

211-
preserves('should not throw when refusing to convert non-object into object', () => {
212-
try {
213-
let input = { b:123 };
214-
dset(input, 'b.c.d.e', 123);
215-
assert.is(input.b, 123);
216-
} catch (err) {
217-
assert.unreachable('should not have thrown');
218-
}
219-
});
220-
221-
preserves('should not throw when refusing to convert `0` into object', () => {
222-
try {
223-
let input = { b:0 };
224-
dset(input, 'b.a.s.d', 123);
225-
assert.equal(input, { b: 0 });
226-
} catch (err) {
227-
assert.unreachable('should not have thrown');
228-
}
229-
});
230-
231213
preserves.run();
232214

233215
// ---
@@ -274,8 +256,18 @@ pollution('should ignore "prototype" assignment', () => {
274256
dset(input, 'a.prototype.hello', 'world');
275257

276258
assert.is(input.a.prototype, undefined);
277-
assert.is.not(input.a.hello, 'world');
278-
assert.equal(input, { a: 123 });
259+
assert.is(input.a.hello, 'world');
260+
261+
assert.equal(input, {
262+
a: {
263+
hello: 'world'
264+
}
265+
});
266+
267+
assert.is(
268+
JSON.stringify(input),
269+
'{"a":{"hello":"world"}}'
270+
);
279271
});
280272

281273
pollution('should ignore "constructor" assignment', () => {

0 commit comments

Comments
 (0)