Skip to content

Commit 2d0e33c

Browse files
committed
getByDot: return top-level property if key exists in object
1 parent bae058d commit 2d0e33c

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

lib/common/get-by-dot.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
module.exports = function (obj, path) {
33
if (typeof obj !== 'object' || obj === null) return undefined;
44

5-
if (path.indexOf('.') === -1) {
5+
if (path.indexOf('.') === -1 || path in obj) {
66
return obj[path];
77
}
88

tests/services/get-set-by-dot.test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ describe('services byDot', () => {
2222
name: 'John',
2323
address: { line1: '100 5-th Avenue', city: 'New York' }
2424
}
25-
}
25+
},
26+
'foo.bar': {}
2627
};
2728
});
2829

@@ -47,6 +48,10 @@ describe('services byDot', () => {
4748
obj.manager.employee.address.city);
4849
});
4950

51+
it('gets top level property with dot in key', () => {
52+
assert.equal(getByDot(obj, 'foo.bar'), obj['foo.bar']);
53+
});
54+
5055
it('does not throw on missing path, at top', () => {
5156
assert.equal(getByDot(obj, 'xxx'), undefined);
5257
});

0 commit comments

Comments
 (0)