-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeepHas.js
106 lines (95 loc) · 2.08 KB
/
deepHas.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
var indexFalse,
indexTrue;
function indexer(val) {
return function (obj, i) {
"use strict";
try {
if (obj && i && obj.hasOwnProperty(i)) {
return obj[i];
} else if (obj && i && val) {
obj[i] = {};
return obj[i];
}
return;
} catch (ex) {
console.error(ex);
return;
}
};
}
indexTrue = indexer(true);
indexFalse = indexer(false);
function reduce(obj, str) {
"use strict";
try {
if (typeof str !== "string") {
return;
}
if (typeof obj !== "object") {
return;
}
return str.split('.').reduce(indexFalse, obj);
} catch (ex) {
console.error(ex);
return;
}
}
function add(obj, str, val) {
"use strict";
try {
if (typeof str !== "string") {
return;
}
if (str.indexOf('__proto__') != -1) {
throw "cannot modify prototype property";
}
if (typeof obj !== "object") {
return;
}
if (!val) {
return;
}
var items = str.split('.');
var initial = items.slice(0, items.length - 1);
var last = items.slice(items.length - 1);
var test = initial.reduce(indexTrue, obj);
test[last] = val;
} catch (ex) {
console.error(ex);
return;
}
}
function has(target, path) {
"use strict";
try {
var test = reduce(target, path);
if (typeof test !== "undefined") {
return true;
}
return false;
} catch (ex) {
console.error(ex);
return;
}
}
function get(target, path) {
"use strict";
try {
return reduce(target, path);
} catch (ex) {
console.error(ex);
return;
}
}
function set(target, path, val) {
"use strict";
try {
return add(target, path, val);
} catch (ex) {
console.error(ex);
return;
}
}
exports.has = has;
exports.get = get;
exports.set = set;