-
Notifications
You must be signed in to change notification settings - Fork 84
/
index.js
320 lines (273 loc) · 7.95 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
(function (root, factory) {
'use strict'
/*istanbul ignore next:cant test*/
if (typeof module === 'object' && typeof module.exports === 'object') {
module.exports = factory()
} else if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory)
} else {
// Browser globals
root.objectPath = factory()
}
})(this, function () {
'use strict'
var toStr = Object.prototype.toString
function hasOwnProperty (obj, prop) {
if (obj == null) {
return false
}
//to handle objects with null prototypes (too edge case?)
return Object.prototype.hasOwnProperty.call(obj, prop)
}
function isEmpty (value) {
if (!value) {
return true
}
if (isArray(value) && value.length === 0) {
return true
} else if (typeof value !== 'string') {
for (var i in value) {
if (hasOwnProperty(value, i)) {
return false
}
}
return true
}
return false
}
function toString (type) {
return toStr.call(type)
}
function isObject (obj) {
return typeof obj === 'object' && toString(obj) === '[object Object]'
}
var isArray = Array.isArray || function (obj) {
/*istanbul ignore next:cant test*/
return toStr.call(obj) === '[object Array]'
}
function isBoolean (obj) {
return typeof obj === 'boolean' || toString(obj) === '[object Boolean]'
}
function getKey (key) {
var intKey = parseInt(key)
if (intKey.toString() === key) {
return intKey
}
return key
}
function factory (options) {
options = options || {}
var objectPath = function (obj) {
return Object.keys(objectPath).reduce(function (proxy, prop) {
if (prop === 'create') {
return proxy
}
/*istanbul ignore else*/
if (typeof objectPath[prop] === 'function') {
proxy[prop] = objectPath[prop].bind(objectPath, obj)
}
return proxy
}, {})
}
var hasShallowProperty
if (options.includeInheritedProps) {
hasShallowProperty = function () {
return true
}
} else {
hasShallowProperty = function (obj, prop) {
return (typeof prop === 'number' && Array.isArray(obj)) || hasOwnProperty(obj, prop)
}
}
function getShallowProperty (obj, prop) {
if (hasShallowProperty(obj, prop)) {
return obj[prop]
}
}
var getShallowPropertySafely
if (options.includeInheritedProps) {
getShallowPropertySafely = function (obj, currentPath) {
if (typeof currentPath !== 'string' && typeof currentPath !== 'number') {
currentPath = String(currentPath)
}
var currentValue = getShallowProperty(obj, currentPath)
if (currentPath === '__proto__' || currentPath === 'prototype' ||
(currentPath === 'constructor' && typeof currentValue === 'function')) {
throw new Error('For security reasons, object\'s magic properties cannot be set')
}
return currentValue
}
} else {
getShallowPropertySafely = function (obj, currentPath) {
return getShallowProperty(obj, currentPath)
}
}
function set (obj, path, value, doNotReplace) {
if (typeof path === 'number') {
path = [path]
}
if (!path || path.length === 0) {
return obj
}
if (typeof path === 'string') {
return set(obj, path.split('.').map(getKey), value, doNotReplace)
}
var currentPath = path[0]
var currentValue = getShallowPropertySafely(obj, currentPath)
if (path.length === 1) {
if (currentValue === void 0 || !doNotReplace) {
obj[currentPath] = value
}
return currentValue
}
if (currentValue === void 0) {
//check if we assume an array
if (typeof path[1] === 'number') {
obj[currentPath] = []
} else {
obj[currentPath] = {}
}
}
return set(obj[currentPath], path.slice(1), value, doNotReplace)
}
objectPath.has = function (obj, path) {
if (typeof path === 'number') {
path = [path]
} else if (typeof path === 'string') {
path = path.split('.')
}
if (!path || path.length === 0) {
return !!obj
}
for (var i = 0; i < path.length; i++) {
var j = getKey(path[i])
if ((typeof j === 'number' && isArray(obj) && j < obj.length) ||
(options.includeInheritedProps ? (j in Object(obj)) : hasOwnProperty(obj, j))) {
obj = obj[j]
} else {
return false
}
}
return true
}
objectPath.ensureExists = function (obj, path, value) {
return set(obj, path, value, true)
}
objectPath.set = function (obj, path, value, doNotReplace) {
return set(obj, path, value, doNotReplace)
}
objectPath.insert = function (obj, path, value, at) {
var arr = objectPath.get(obj, path)
at = ~~at
if (!isArray(arr)) {
arr = []
objectPath.set(obj, path, arr)
}
arr.splice(at, 0, value)
}
objectPath.empty = function (obj, path) {
if (isEmpty(path)) {
return void 0
}
if (obj == null) {
return void 0
}
var value, i
if (!(value = objectPath.get(obj, path))) {
return void 0
}
if (typeof value === 'string') {
return objectPath.set(obj, path, '')
} else if (isBoolean(value)) {
return objectPath.set(obj, path, false)
} else if (typeof value === 'number') {
return objectPath.set(obj, path, 0)
} else if (isArray(value)) {
value.length = 0
} else if (isObject(value)) {
for (i in value) {
if (hasShallowProperty(value, i)) {
delete value[i]
}
}
} else {
return objectPath.set(obj, path, null)
}
}
objectPath.push = function (obj, path /*, values */) {
var arr = objectPath.get(obj, path)
if (!isArray(arr)) {
arr = []
objectPath.set(obj, path, arr)
}
arr.push.apply(arr, Array.prototype.slice.call(arguments, 2))
}
objectPath.coalesce = function (obj, paths, defaultValue) {
var value
for (var i = 0, len = paths.length; i < len; i++) {
if ((value = objectPath.get(obj, paths[i])) !== void 0) {
return value
}
}
return defaultValue
}
objectPath.get = function (obj, path, defaultValue) {
if (typeof path === 'number') {
path = [path]
}
if (!path || path.length === 0) {
return obj
}
if (obj == null) {
return defaultValue
}
if (typeof path === 'string') {
return objectPath.get(obj, path.split('.'), defaultValue)
}
var currentPath = getKey(path[0])
var nextObj = getShallowPropertySafely(obj, currentPath)
if (nextObj === void 0) {
return defaultValue
}
if (path.length === 1) {
return nextObj
}
return objectPath.get(obj[currentPath], path.slice(1), defaultValue)
}
objectPath.del = function del (obj, path) {
if (typeof path === 'number') {
path = [path]
}
if (obj == null) {
return obj
}
if (isEmpty(path)) {
return obj
}
if (typeof path === 'string') {
return objectPath.del(obj, path.split('.'))
}
var currentPath = getKey(path[0])
getShallowPropertySafely(obj, currentPath)
if (!hasShallowProperty(obj, currentPath)) {
return obj
}
if (path.length === 1) {
if (isArray(obj)) {
obj.splice(currentPath, 1)
} else {
delete obj[currentPath]
}
} else {
return objectPath.del(obj[currentPath], path.slice(1))
}
return obj
}
return objectPath
}
var mod = factory()
mod.create = factory
mod.withInheritedProps = factory({includeInheritedProps: true})
return mod
})