-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (54 loc) · 1.46 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.hasDeepValue = exports.setDeepValue = exports.getDeepValue = void 0;
const getDeepValue = (data, route) => {
if (!route || !data)
return;
let result = data;
if (!result || !result?.hasOwnProperty)
return;
const keys = route.split('.');
for (const key of keys)
if (!!key && result?.hasOwnProperty(key))
result = result[key];
else
return;
return result;
};
exports.getDeepValue = getDeepValue;
const setDeepValue = (data, route, value, createKey = true) => {
if (!route)
return false;
let obj = data;
const keys = route.split('.');
const lastKey = keys.pop();
for (const key of keys) {
if (!obj.hasOwnProperty(key))
if (createKey)
obj[key] = {};
else
return false;
obj = obj[key];
}
obj[lastKey] = value;
data = obj;
return true;
};
exports.setDeepValue = setDeepValue;
const hasDeepValue = (data, route, createKey = false) => {
if (!route)
return false;
let obj = data;
const keys = route.split('.');
for (const key of keys) {
if (!obj.hasOwnProperty(key))
if (createKey)
obj[key] = {};
else
return false;
obj = obj[key];
}
return true;
};
exports.hasDeepValue = hasDeepValue;
//# sourceMappingURL=index.js.map