-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
53 lines (45 loc) · 1.25 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
'use strict';
var deepval = function (obj, path, value, remove) {
if (!obj) {
return undefined;
}
if (!Array.isArray(path)) {
path = path.split('.');
}
var pl = path.length - 1;
for (var i = 0; i < pl; i++) {
if (value !== undefined && (obj[path[i]] === undefined || obj[path[i]] === null)) {
obj[path[i]] = {};
} else if (!obj.hasOwnProperty(path[i]) || (obj[path[i]] === undefined || obj[path[i]] === null)) {
return undefined;
}
// splice out array elements that are being removed instead of deleting them
if (remove && (i === (pl - 1)) && obj[path[i]] instanceof Array) {
obj[path[i]].splice(obj[path[i + 1]], 1);
return;
}
obj = obj[path[i]];
}
if (value !== undefined) {
if (remove) {
delete obj[path[pl]];
return;
} else {
obj[path[pl]] = value;
}
}
return obj[path[pl]];
};
module.exports = deepval;
module.exports.get = function (obj, path) {
return deepval(obj, path);
};
module.exports.set = function (obj, path, value) {
return deepval(obj, path, value);
};
module.exports.del = function (obj, path) {
return deepval(obj, path, null, true);
};
module.exports.dotpath = function () {
return Array.prototype.slice.call(arguments).join('.');
};