Skip to content

Commit debd0e3

Browse files
committed
Issue-2-DefaultWhenNotFound use default if value is undefined and key path exists. Don't use default if key exists but value is null
1 parent 15aeaff commit debd0e3

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# `dlv(obj, keypath)` [![NPM](https://img.shields.io/npm/v/dlv.svg)](https://npmjs.com/package/dlv) [![Build](https://travis-ci.org/developit/dlv.svg?branch=master)](https://travis-ci.org/developit/dlv)
22

3-
> Safely get a dot-notated path within a nested object, with ability to return a default if the full key path does not exist
3+
> Safely get a dot-notated path within a nested object, with ability to return a default if the full key path does not exist or the value is undefined
44
55
### Why?
66

@@ -26,7 +26,8 @@ let obj = {
2626
a: {
2727
b: {
2828
c: 1
29-
e: undefined
29+
d: undefined
30+
e: null
3031
}
3132
}
3233
};
@@ -40,14 +41,16 @@ delve(obj, ['a', 'b', 'c']) === 1;
4041
delve(obj, 'a.b') === obj.a.b;
4142

4243
//returns undefined if the full key path does not exist and no default is specified
43-
delve(obj, 'a.b.c.d') === undefined;
44+
delve(obj, 'a.b.c.f') === undefined;
4445

4546
//optional third parameter for default if the full key in path is missing
47+
delve(obj, 'a.b.c.f', 'foo') === 'foo';
48+
49+
//or if the key exists but the value is undefined
4650
delve(obj, 'a.b.c.d', 'foo') === 'foo';
4751

48-
//default is only used if full keypath does not exist.
49-
//Non-truthy values are still returned if they exist at the full keypath
50-
delve(obj, 'a.b.c.e', 'foo') === undefined;
52+
//Non-truthy defined values are still returned if they exist at the full keypath
53+
delve(obj, 'a.b.c.e', 'foo') === null;
5154

5255
//undefined obj or key returns undefined, unless a default is supplied
5356
delve(undefined, 'a.b.c') === undefined;

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ export default function dlv(obj, key, def) {
33
if (key.split) key = key.split('.');
44
var i=0, keyLength=key.length;
55
for (; i<keyLength && obj.hasOwnProperty(key[i]); obj = obj[key[i++]]) ;
6-
return i===keyLength ? obj : def;
6+
return i===keyLength && typeof obj !== 'undefined' ? obj : def;
77
}

test.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var obj = {
55
undef: undefined,
66
zero: 0,
77
one: 1,
8+
n: null,
89
a: {
910
two: 2,
1011
b: {
@@ -20,16 +21,18 @@ var obj = {
2021
//optional third argument is for default when object is not found
2122
function check(path, value, def) {
2223

23-
assert.equal(delve(obj, path, def), value);
24-
console.log(' ✓ delve(obj, "'+path+'")');
24+
assert.strictEqual(delve(obj, path, def), value);
25+
console.log(' ✓ delve(obj, "'+path+'"'+ (def ? ', "'+def+'"' : '') + ')');
2526

2627
if(path) {
2728
var arr = path.split('.');
28-
assert.equal(delve(obj, arr, def), value);
29+
assert.strictEqual(delve(obj, arr, def), value);
30+
console.log(' ✓ delve(obj, ' + JSON.stringify(arr) + (def ? ', "'+def+'"' : '') + ')');
2931
console.log(' ✓ delve(obj, '+JSON.stringify(arr)+')');
3032
}
3133
}
3234

35+
console.log("> No Defaults");
3336
check('', undefined);
3437
check('one', obj.one);
3538
check('one.two', undefined);
@@ -41,8 +44,10 @@ check('a.b.c', obj.a.b.c);
4144
check('a.b.c.four', obj.a.b.c.four);
4245

4346
//test defaults
47+
console.log("\n> With Defaults");
4448
check('', 'foo', 'foo');
45-
check('undef', undefined, 'foo');
49+
check('undef', 'foo', 'foo');
50+
check('n', null, 'foo');
4651
check('zero', 0, 'foo');
4752
check('a.badkey', 'foo', 'foo');
4853
check('a.badkey.anotherbadkey', 'foo', 'foo');

0 commit comments

Comments
 (0)