Skip to content

Commit 12b1f4f

Browse files
committed
added deepGet util from my deeps lib.
1 parent 74b233a commit 12b1f4f

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

lib/utils/index.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// modeled after base64 web-safe chars, but ordered by ASCII
22
const PUSH_CHARS = '-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz';
3-
3+
const hasOwnProperty = Object.hasOwnProperty;
44
const DEFAULT_CHUNK_SIZE = 50;
55

66
// internal promise handler
@@ -12,6 +12,30 @@ const _handler = (resolve, reject, err, resp) => {
1212
});
1313
};
1414

15+
/**
16+
* Deep get a value from an object.
17+
* @website https://github.com/Salakar/deeps
18+
* @param object
19+
* @param path
20+
* @param joiner
21+
* @returns {*}
22+
*/
23+
export function deepGet(object, path, joiner = '/') {
24+
const keys = path.split(joiner);
25+
26+
let i = 0;
27+
let tmp = object;
28+
const len = keys.length;
29+
30+
while (i < len) {
31+
const key = keys[i += 1];
32+
if (!tmp || !hasOwnProperty.call(tmp, key)) return null;
33+
tmp = tmp[key];
34+
}
35+
36+
return tmp;
37+
}
38+
1539
/**
1640
* Simple is object check.
1741
* @param item

0 commit comments

Comments
 (0)