From 9c7867e6dc75ee8842b4bbfdfeba9e3ccf3bc616 Mon Sep 17 00:00:00 2001 From: Remy Sharp Date: Wed, 8 Feb 2017 16:52:43 +0000 Subject: [PATCH] fix: when first prop is a string lookup --- lib/undefsafe.js | 5 ++++- test/undefsafe.test.js | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) 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(); });