-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
207 lines (185 loc) · 4.91 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* eslint-disable max-len, sort-keys */
"use strict"
/**
* Set a value by a dot path.
* @param obj The object to evaluate.
* @param prop The path to be set.
* @param val The value to set.
*/
function set(obj, prop, value) {
prop =
typeof prop === "number"
? propToArray(prop.toString())
: typeof prop === "string"
? propToArray(prop)
: prop
var setPropImmutableRec = function(obj, prop, value, i) {
var clone,
head = prop[i]
if (prop.length > i) {
if (Array.isArray(obj)) {
head = getArrayIndex(head, obj)
clone = obj.slice()
} else {
clone = Object.assign({}, obj)
}
clone[head] = setPropImmutableRec(
obj[head] !== undefined ? obj[head] : {},
prop,
value,
i + 1
)
return clone
}
return typeof value === "function" ? value(obj) : value
}
return setPropImmutableRec(obj, prop, value, 0)
}
/**
* Get a value by a dot path.
* @param obj The object to evaluate.
* @param prop The path to value that should be returned.
*/
function get(obj, prop, value) {
prop =
typeof prop === "number"
? propToArray(prop.toString())
: typeof prop === "string"
? propToArray(prop)
: prop
for (var i = 0; i < prop.length; i++) {
if (typeof obj !== "object") {
return value
}
var head = prop[i]
if (Array.isArray(obj) && head === "$end") {
head = obj.length - 1
}
obj = obj[head]
}
if (typeof obj === "undefined") {
return value
}
return obj
}
/**
* Delete a property by a dot path.
* If target container is an object, the property is deleted.
* If target container is an array, the index is deleted.
* If target container is undefined, nothing is deleted.
* @param obj The object to evaluate.
* @param prop The path to the property or index that should be deleted.
*/
function _delete(obj, prop) {
prop =
typeof prop === "number"
? propToArray(prop.toString())
: typeof prop === "string"
? propToArray(prop)
: prop
var deletePropImmutableRec = function(obj, prop, i) {
var clone,
head = prop[i]
if (
typeof obj !== "object" ||
(!Array.isArray(obj) && obj[head] === undefined)
) {
return obj
}
if (prop.length - 1 > i) {
if (Array.isArray(obj)) {
head = getArrayIndex(head, obj)
clone = obj.slice()
} else {
clone = Object.assign({}, obj)
}
clone[head] = deletePropImmutableRec(
obj[head],
prop,
i + 1
)
return clone
}
if (Array.isArray(obj)) {
head = getArrayIndex(head, obj)
clone = [].concat(
obj.slice(0, head),
obj.slice(head + 1)
)
} else {
clone = Object.assign({}, obj)
delete clone[head]
}
return clone
}
return deletePropImmutableRec(obj, prop, 0)
}
/**
* Toggles a value. The target value is evaluated using Boolean(currentValue). The result will always be a JSON boolean.
* Be careful with strings as target value, as "true" and "false" will toggle to false, but "0" will toggle to true.
* Here is what Javascript considers false: 0, -0, null, false, NaN, undefined, and the empty string ("")
* @param obj The object to evaluate.
* @param prop The path to the value.
*/
function toggle(obj, prop) {
var curVal = get(obj, prop)
return set(obj, prop, !curVal)
}
/**
* Merges a value. The target value must be an object, array, null, or undefined.
* If target is an object, Object.assign({}, target, param) is used.
* If target an array, target.concat(param) is used.
* If target is null or undefined, the value is simply set.
* @param obj The object to evaluate.
* @param prop The path to the value.
* @param val The value to merge into the target value.
*/
function merge(obj, prop, val) {
var curVal = get(obj, prop)
if (typeof curVal === "object") {
if (Array.isArray(curVal)) {
return set(obj, prop, curVal.concat(val))
} else if (curVal === null) {
return set(obj, prop, val)
} else {
var merged = Object.assign({}, curVal, val)
return set(obj, prop, merged)
}
} else if (typeof curVal === "undefined") {
return set(obj, prop, val)
} else {
return obj
}
}
function getArrayIndex(head, obj) {
if (head === "$end") {
head = Math.max(obj.length - 1, 0)
}
if (!/^\+?\d+$/.test(head)) {
throw new Error(
"Array index '" + head + "' has to be an integer"
)
}
return parseInt(head)
}
function propToArray(prop) {
return prop
.split(".")
.reduce(function(ret, el, index, list) {
var last = index > 0 && list[index - 1]
if (last && /(?:^|[^\\])\\$/.test(last)) {
ret.pop()
ret.push(last.slice(0, -1) + "." + el)
} else {
ret.push(el)
}
return ret
}, [])
}
module.exports = {
set: set,
get: get,
delete: _delete,
toggle: toggle,
merge: merge,
}