Skip to content

Commit a94ea95

Browse files
manovotnysindresorhus
authored andcommitted
Add ability to return default value if path is undefined (#31)
1 parent 88b6eb6 commit a94ea95

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function getPathSegments(path) {
1919
return parts;
2020
}
2121

22-
module.exports.get = (obj, path) => {
22+
module.exports.get = (obj, path, value) => {
2323
if (!isObj(obj) || typeof path !== 'string') {
2424
return obj;
2525
}
@@ -28,7 +28,7 @@ module.exports.get = (obj, path) => {
2828

2929
for (let i = 0; i < pathArr.length; i++) {
3030
if (!Object.prototype.propertyIsEnumerable.call(obj, pathArr[i])) {
31-
return;
31+
return value;
3232
}
3333

3434
obj = obj[pathArr[i]];
@@ -38,9 +38,9 @@ module.exports.get = (obj, path) => {
3838
// if this is not the last bit of the path, and
3939
// if it did't return `undefined`
4040
// it would return `null` if `obj` is `null`
41-
// but we want `get({foo: null}, 'foo.bar')` to equal `undefined` not `null`
41+
// but we want `get({foo: null}, 'foo.bar')` to equal `undefined`, or the supplied value, not `null`
4242
if (i !== pathArr.length - 1) {
43-
return undefined;
43+
return value;
4444
}
4545

4646
break;

readme.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ dotProp.get({foo: {bar: 'unicorn'}}, 'foo.bar');
2222
dotProp.get({foo: {bar: 'a'}}, 'foo.notDefined.deep');
2323
//=> undefined
2424

25+
dotProp.get({foo: {bar: 'a'}}, 'foo.notDefined.deep', 'default value');
26+
//=> 'default value'
27+
2528
dotProp.get({foo: {'dot.dot': 'unicorn'}}, 'foo.dot\\.dot');
2629
//=> 'unicorn'
2730

@@ -54,7 +57,7 @@ console.log(obj);
5457

5558
## API
5659

57-
### get(obj, path)
60+
### get(obj, path, [value])
5861

5962
### set(obj, path, value)
6063

@@ -80,7 +83,7 @@ Use `\\.` if you have a `.` in the key.
8083

8184
Type: `any`
8285

83-
Value to set at `path`.
86+
Value to set at `path` or optional default value to return from get.
8487

8588

8689
## License

test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ test('get', t => {
1515
t.is(m.get({foo: {bar: {baz: null}}}, 'foo.bar.baz'), null);
1616
t.is(m.get({foo: {bar: 'a'}}, 'foo.fake'), undefined);
1717
t.is(m.get({foo: {bar: 'a'}}, 'foo.fake.fake2'), undefined);
18+
t.is(m.get({foo: {bar: 'a'}}, 'foo.fake.fake2', 'some value'), 'some value');
1819
t.is(m.get({'\\': true}, '\\'), true);
1920
t.is(m.get({'\\foo': true}, '\\foo'), true);
2021
t.is(m.get({'bar\\': true}, 'bar\\'), true);
@@ -40,6 +41,7 @@ test('get', t => {
4041

4142
const f3 = {foo: null};
4243
t.is(m.get(f3, 'foo.bar'), undefined);
44+
t.is(m.get(f3, 'foo.bar', 'some value'), 'some value');
4345

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

0 commit comments

Comments
 (0)