Skip to content

Commit bae058d

Browse files
committed
existsByDot: return true if property exists in object
1 parent f7a61d3 commit bae058d

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

lib/common/exists-by-dot.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11

22
module.exports = function (obj, path) {
3+
if (path in obj) return true;
4+
35
const parts = path.split('.');
46
const nonLeafLen = parts.length - 1;
57

tests/services/exists-by-dot.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ describe('test existsByDot', () => {
4242
assert.equal(existsByDot({}, 'name'), false);
4343
});
4444

45+
it('finds property with dot in key', () => {
46+
const obj = { 'name.first': 'John' };
47+
assert.equal(existsByDot(obj, 'name.first'), true);
48+
assert.equal(existsByDot(obj, 'name'), false);
49+
assert.equal(existsByDot(obj, 'first'), false);
50+
assert.equal(existsByDot(obj, 'namefirst'), false);
51+
assert.equal(existsByDot(obj, 'name.first.foo'), false);
52+
});
53+
4554
it('does not throw if path ends prematurely', () => {
4655
assert.deepEqual(existsByDot(obj, 'x'), false);
4756
assert.deepEqual(existsByDot(obj, 'name.a.c.x'), false);

0 commit comments

Comments
 (0)