From 1cf2612216d59edea71c6aa304e93c9c7ac331bd Mon Sep 17 00:00:00 2001 From: Steve Mao Date: Thu, 3 Dec 2015 23:51:23 +1100 Subject: [PATCH] do not throw no matter what --- index.js | 27 ++++++++++++++++++++++----- test.js | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 7639c18..6910dd8 100644 --- a/index.js +++ b/index.js @@ -16,7 +16,11 @@ module.exports.get = function (obj, path) { p += pathArr[++i]; } - obj = obj[p]; + try { + obj = obj[p]; + } catch (err) { + return undefined; + } if (obj === undefined) { break; @@ -27,7 +31,7 @@ module.exports.get = function (obj, path) { }; module.exports.set = function (obj, path, value) { - if (!isObj(obj) || typeof path !== 'string') { + if (typeof path !== 'string') { return; } @@ -41,12 +45,25 @@ module.exports.set = function (obj, path, value) { p += pathArr[++i]; } - if (!isObj(obj[p])) { - obj[p] = {}; + var childProp; + try { + childProp = obj[p]; + } catch (err) { + break; + } + + if (!isObj(childProp)) { + try { + obj[p] = {}; + } catch (err) { + break; + } } if (i === pathArr.length - 1) { - obj[p] = value; + try { + obj[p] = value; + } catch (err) {} } obj = obj[p]; diff --git a/test.js b/test.js index 344df6c..69acb02 100644 --- a/test.js +++ b/test.js @@ -4,6 +4,8 @@ import dotProp from './'; test(function getter(t) { let f1 = {foo: {bar: 1}}; t.is(dotProp.get(f1), f1); + f1[''] = 'foo'; + t.is(dotProp.get(f1, ''), f1['']); t.is(dotProp.get(f1, 'foo'), f1.foo); t.is(dotProp.get({foo: 1}, 'foo'), 1); t.is(dotProp.get({foo: null}, 'foo'), null); @@ -11,6 +13,7 @@ test(function getter(t) { t.is(dotProp.get({foo: {bar: true}}, 'foo.bar'), true); t.is(dotProp.get({foo: {bar: {baz: true}}}, 'foo.bar.baz'), true); t.is(dotProp.get({foo: {bar: {baz: null}}}, 'foo.bar.baz'), null); + t.is(dotProp.get({foo: {bar: 'a'}}, 'foo.fake'), undefined); t.is(dotProp.get({foo: {bar: 'a'}}, 'foo.fake.fake2'), undefined); function fn() {} @@ -19,6 +22,16 @@ test(function getter(t) { t.is(dotProp.get(fn, 'foo'), fn.foo); t.is(dotProp.get(fn, 'foo.bar'), 1); + let f2 = {foo: null}; + t.is(dotProp.get(f2, 'foo.bar'), undefined); + + Object.defineProperty(f2, 'bar', { + get() { + throw new Error('Cannot get me'); + } + }); + t.is(dotProp.get(f2, 'bar'), undefined); + t.is(dotProp.get({'foo.baz': {bar: true}}, 'foo\\.baz.bar'), true); t.is(dotProp.get({'fo.ob.az': {bar: true}}, 'fo\\.ob\\.az.bar'), true); }); @@ -66,6 +79,40 @@ test(function setter(t) { dotProp.set(f1, 'fn.bar.baz', 2); t.is(f1.fn.bar.baz, 2); + let f2 = {}; + Object.defineProperty(f2, 'bar', { + configurable: true, + get() {}, + set() { + throw new Error('Cannot set me'); + } + }); + f1.f2 = f2; + dotProp.set(f2, 'bar', 2); + dotProp.set(f2, 'bar.baz', 2); + dotProp.set(f2.bar, '', 2); + dotProp.set(f2.bar, 'baz', 2); + dotProp.set(f1, 'f2.bar', 2); + dotProp.set(f1, 'f2.bar.baz', 2); + + Object.defineProperty(f2, 'bar', { + set() { + throw new Error('Cannot set me'); + }, + get() { + throw new Error('Cannot get me'); + } + }); + f1.f2 = f2; + dotProp.set(f2, 'bar', 2); + dotProp.set(f2, 'bar.baz', 2); + dotProp.set(f1, 'f2.bar', 2); + dotProp.set(f1, 'f2.bar.baz', 2); + + let f3 = {}; + dotProp.set(f3, '', 3); + t.is(f3[''], 3); + dotProp.set(f1, 'foo\\.bar.baz', true); t.is(f1['foo.bar'].baz, true);