From f4299cbd1e30e3b894d5e3f5ec67dd478a9748e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Go=C5=9Bci=C5=84ski?= Date: Sun, 24 Apr 2022 15:06:44 +0200 Subject: [PATCH] fix: handle null in obj, handle empty string in path Closes #35 Closes #36 --- src/index.js | 2 +- src/merge.js | 2 +- test/suites/objects.js | 20 ++++++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 75a01fb..bda308b 100644 --- a/src/index.js +++ b/src/index.js @@ -4,6 +4,6 @@ export function dset(obj, keys, val) { while (i < l) { k = keys[i++]; if (k === '__proto__' || k === 'constructor' || k === 'prototype') break; - t = t[k] = (i === l) ? val : (typeof(x=t[k])===typeof(keys)) ? x : (keys[i]*0 !== 0 || !!~(''+keys[i]).indexOf('.')) ? {} : []; + t = t[k] = (i === l) ? val : ((x=t[k]) && typeof(x)===typeof(keys)) ? x : (k='+'+keys[i])*0 !== 0 || /\./.test(k) ? {} : [] } } diff --git a/src/merge.js b/src/merge.js index d428b14..e3d4c74 100644 --- a/src/merge.js +++ b/src/merge.js @@ -20,6 +20,6 @@ export function dset(obj, keys, val) { while (i < l) { k = keys[i++]; if (k === '__proto__' || k === 'constructor' || k === 'prototype') break; - t = t[k] = (i === l) ? merge(t[k],val) : (typeof(x=t[k])===typeof keys) ? x : (keys[i]*0 !== 0 || !!~(''+keys[i]).indexOf('.')) ? {} : []; + t = t[k] = (i === l) ? merge(t[k],val) : ((x = t[k]) && typeof x === typeof keys) ? x : (k = '+' + keys[i]) * 0 !== 0 || /\./.test(k) ? {} : []; } } diff --git a/test/suites/objects.js b/test/suites/objects.js index b29585d..b2b3292 100644 --- a/test/suites/objects.js +++ b/test/suites/objects.js @@ -91,5 +91,25 @@ export default function (dset, isMerge) { } }); + objects(`should ${verb} empty string property`, () => { + let { input } = prepare({}); + + dset(input, ['hello', ''], 123); + + assert.equal(input, { + hello: { '': 123 }, + }); + }); + + objects(`should ${verb} null values`, () => { + let { input } = prepare({ hello: null }); + + dset(input, ['hello', 'a'], 123); + + assert.equal(input, { + hello: { a: 123 }, + }); + }); + objects.run(); }