Skip to content

Commit f9e4465

Browse files
committed
Return undefined/default when requesting key nested under null value.
1 parent 250e18e commit f9e4465

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ export default function dlv(obj, key, def, p) {
22
p = 0;
33
key = key.split ? key.split('.') : key;
44
while (obj && p<key.length) obj = obj[key[p++]];
5-
return obj===undefined ? def : obj;
6-
}
5+
return (obj===undefined || p<key.length) ? def : obj;
6+
}

test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,15 @@ check('a.b', obj.a.b);
4343
check('a.b.three', obj.a.b.three);
4444
check('a.b.c', obj.a.b.c);
4545
check('a.b.c.four', obj.a.b.c.four);
46+
check('n', obj.n);
47+
check('n.badkey', undefined);
4648

4749
//test defaults
4850
console.log("\n> With Defaults");
4951
check('', 'foo', 'foo');
5052
check('undef', 'foo', 'foo');
5153
check('n', null, 'foo');
54+
check('n.badkey', 'foo', 'foo');
5255
check('zero', 0, 'foo');
5356
check('a.badkey', 'foo', 'foo');
5457
check('a.badkey.anotherbadkey', 'foo', 'foo');

0 commit comments

Comments
 (0)