Skip to content
This repository has been archived by the owner on Aug 10, 2021. It is now read-only.

Commit

Permalink
Add check if node exists in searchInJson #11
Browse files Browse the repository at this point in the history
  • Loading branch information
danistefanovic committed Jan 24, 2016
1 parent 2bc2b96 commit 0bf05a7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/server/utils/searchInJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export default function searchInJson(json, query) {
return _(query.split('.'))
.drop()
.reduce((prev, key) => {
return prev[key];
if (prev && prev[key]) {
return prev[key];
}
}, json);
}
14 changes: 14 additions & 0 deletions test/server/utils/searchInJson.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,18 @@ describe('searchInJson', () => {
});
});
});

describe('a query that does not match', () => {
it('should return undefined if the leaf does not exist', () => {
const json = { a: 'va1' };
const query = 'json.b';
expect(searchInJson(json, query)).toEqual(undefined);
});

it('should return undefined if an internal node does not exist', () => {
const json = { a: { b: 'vb1' } };
const query = 'json.x.b';
expect(searchInJson(json, query)).toEqual(undefined);
});
});
});

0 comments on commit 0bf05a7

Please sign in to comment.