-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
38 lines (29 loc) · 916 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
'use strict';
const keysArray = require('./lib/keysArray');
const isNonNullObject = require('./lib/isNonNullObject');
/**
*
* @param {object} rootObject object to which dotted string keys to be resolved
* @param {string} dotStringKey a string of object dot notation, e.g., "address.city.code"
* @returns {any} value of the resolved key
*
*/
const resolveDotStringKey = (rootObject, dotStringKey) => {
if (!rootObject) {
throw new Error(`rootObject is missing.`)
} else {
if (isNonNullObject(rootObject)) {
let resolvedKeyValue = rootObject;
if (dotStringKey) {
let keyArray = keysArray(dotStringKey);
for (let key of keyArray) {
resolvedKeyValue = resolvedKeyValue[key];
}
}
return resolvedKeyValue;
} else {
throw new Error(`rootObject must be an object or an array.`)
}
}
}
module.exports = resolveDotStringKey;