Skip to content

Commit

Permalink
Return default value when input is not an object (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
radmen authored and sindresorhus committed Feb 18, 2017
1 parent f91d08a commit 7f2bfe4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
5 changes: 5 additions & 0 deletions bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ bench('get', () => {

m.get({'foo.baz': {bar: true}}, 'foo\\.baz.bar');
m.get({'fo.ob.az': {bar: true}}, 'fo\\.ob\\.az.bar');

m.get(null, 'foo.bar', false);
m.get('foo', 'foo.bar', false);
m.get([], 'foo.bar', false);
m.get(undefined, 'foo.bar', false);
});

bench('set', () => {
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function getPathSegments(path) {
module.exports = {
get(obj, path, value) {
if (!isObj(obj) || typeof path !== 'string') {
return obj;
return value === undefined ? obj : value;
}

const pathArr = getPathSegments(path);
Expand Down
5 changes: 5 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ test('get', t => {

t.is(m.get({'foo.baz': {bar: true}}, 'foo\\.baz.bar'), true);
t.is(m.get({'fo.ob.az': {bar: true}}, 'fo\\.ob\\.az.bar'), true);

t.is(m.get(null, 'foo.bar', false), false);
t.is(m.get('foo', 'foo.bar', false), false);
t.is(m.get([], 'foo.bar', false), false);
t.is(m.get(undefined, 'foo.bar', false), false);
});

test('set', t => {
Expand Down

0 comments on commit 7f2bfe4

Please sign in to comment.