diff --git a/lib/undefsafe.js b/lib/undefsafe.js index f3701de..07b91d9 100644 --- a/lib/undefsafe.js +++ b/lib/undefsafe.js @@ -18,7 +18,10 @@ function undefsafe(obj, path, value) { i++; c = path.substr(i, 1); } - res.push(key); + + if (key) { // the first value could be a string + res.push(key); + } key = ''; continue; } diff --git a/test/undefsafe.test.js b/test/undefsafe.test.js index 9bd973f..c5b6470 100644 --- a/test/undefsafe.test.js +++ b/test/undefsafe.test.js @@ -66,6 +66,13 @@ test('should find properties with periods in them', function (t) { r = undefsafe(value, `a['one.two.and\three'].1`); t.equal(r, true, 'combo: ' + r); + value = { + 'one.two': true + }; + + r = undefsafe(value, `['one.two']`); + t.equal(r, true, 'root: ' + r); + t.end(); });