Skip to content

Commit

Permalink
do not throw no matter what
Browse files Browse the repository at this point in the history
  • Loading branch information
stevemao committed Dec 4, 2015
1 parent fd0210d commit 1cf2612
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 5 deletions.
27 changes: 22 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand All @@ -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];
Expand Down
47 changes: 47 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ 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);
t.is(dotProp.get({foo: undefined}, 'foo'), undefined);
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() {}
Expand All @@ -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);
});
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 1cf2612

Please sign in to comment.