-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathObject.js
More file actions
30 lines (24 loc) · 859 Bytes
/
Object.js
File metadata and controls
30 lines (24 loc) · 859 Bytes
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
function getProp(object, propPath, defaultValue) {
let tokens = propPath.split('.');
for (let i = 0; i < tokens.length - 1; i++) {
let token = tokens[i];
if (typeof object[token] !== 'object' || token === null) {
return defaultValue;
}
object = object[token];
}
let value = object[tokens[tokens.length - 1]];
return (typeof value === 'undefined') ? defaultValue : value;
}
function setProp(object, propPath, value) {
let tokens = propPath.split('.');
for (let i = 0; i < tokens.length - 1; i++) {
let token = tokens[i];
if (typeof object[token] !== 'object' || token === null) {
object[token] = {};
}
object = object[token];
}
object[tokens[tokens.length - 1]] = value;
}
export { getProp, setProp };