-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
object.js
396 lines (348 loc) · 9.15 KB
/
object.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import { isBigNumber, isObject } from './is.js'
/**
* Clone an object
*
* clone(x)
*
* Can clone any primitive type, array, and object.
* If x has a function clone, this function will be invoked to clone the object.
*
* @param {*} x
* @return {*} clone
*/
export function clone (x) {
const type = typeof x
// immutable primitive types
if (type === 'number' || type === 'string' || type === 'boolean' ||
x === null || x === undefined) {
return x
}
// use clone function of the object when available
if (typeof x.clone === 'function') {
return x.clone()
}
// array
if (Array.isArray(x)) {
return x.map(function (value) {
return clone(value)
})
}
if (x instanceof Date) return new Date(x.valueOf())
if (isBigNumber(x)) return x // bignumbers are immutable
// object
if (isObject(x)) {
return mapObject(x, clone)
}
throw new TypeError(`Cannot clone: unknown type of value (value: ${x})`)
}
/**
* Apply map to all properties of an object
* @param {Object} object
* @param {function} callback
* @return {Object} Returns a copy of the object with mapped properties
*/
export function mapObject (object, callback) {
const clone = {}
for (const key in object) {
if (hasOwnProperty(object, key)) {
clone[key] = callback(object[key])
}
}
return clone
}
/**
* Extend object a with the properties of object b
* @param {Object} a
* @param {Object} b
* @return {Object} a
*/
export function extend (a, b) {
for (const prop in b) {
if (hasOwnProperty(b, prop)) {
a[prop] = b[prop]
}
}
return a
}
/**
* Deep extend an object a with the properties of object b
* @param {Object} a
* @param {Object} b
* @returns {Object}
*/
export function deepExtend (a, b) {
// TODO: add support for Arrays to deepExtend
if (Array.isArray(b)) {
throw new TypeError('Arrays are not supported by deepExtend')
}
for (const prop in b) {
// We check against prop not being in Object.prototype or Function.prototype
// to prevent polluting for example Object.__proto__.
if (hasOwnProperty(b, prop) && !(prop in Object.prototype) && !(prop in Function.prototype)) {
if (b[prop] && b[prop].constructor === Object) {
if (a[prop] === undefined) {
a[prop] = {}
}
if (a[prop] && a[prop].constructor === Object) {
deepExtend(a[prop], b[prop])
} else {
a[prop] = b[prop]
}
} else if (Array.isArray(b[prop])) {
throw new TypeError('Arrays are not supported by deepExtend')
} else {
a[prop] = b[prop]
}
}
}
return a
}
/**
* Deep test equality of all fields in two pairs of arrays or objects.
* Compares values and functions strictly (ie. 2 is not the same as '2').
* @param {Array | Object} a
* @param {Array | Object} b
* @returns {boolean}
*/
export function deepStrictEqual (a, b) {
let prop, i, len
if (Array.isArray(a)) {
if (!Array.isArray(b)) {
return false
}
if (a.length !== b.length) {
return false
}
for (i = 0, len = a.length; i < len; i++) {
if (!deepStrictEqual(a[i], b[i])) {
return false
}
}
return true
} else if (typeof a === 'function') {
return (a === b)
} else if (a instanceof Object) {
if (Array.isArray(b) || !(b instanceof Object)) {
return false
}
for (prop in a) {
// noinspection JSUnfilteredForInLoop
if (!(prop in b) || !deepStrictEqual(a[prop], b[prop])) {
return false
}
}
for (prop in b) {
// noinspection JSUnfilteredForInLoop
if (!(prop in a)) {
return false
}
}
return true
} else {
return (a === b)
}
}
/**
* Recursively flatten a nested object.
* @param {Object} nestedObject
* @return {Object} Returns the flattened object
*/
export function deepFlatten (nestedObject) {
const flattenedObject = {}
_deepFlatten(nestedObject, flattenedObject)
return flattenedObject
}
// helper function used by deepFlatten
function _deepFlatten (nestedObject, flattenedObject) {
for (const prop in nestedObject) {
if (hasOwnProperty(nestedObject, prop)) {
const value = nestedObject[prop]
if (typeof value === 'object' && value !== null) {
_deepFlatten(value, flattenedObject)
} else {
flattenedObject[prop] = value
}
}
}
}
/**
* Test whether the current JavaScript engine supports Object.defineProperty
* @returns {boolean} returns true if supported
*/
export function canDefineProperty () {
// test needed for broken IE8 implementation
try {
if (Object.defineProperty) {
Object.defineProperty({}, 'x', { get: function () { return null } })
return true
}
} catch (e) {}
return false
}
/**
* Attach a lazy loading property to a constant.
* The given function `fn` is called once when the property is first requested.
*
* @param {Object} object Object where to add the property
* @param {string} prop Property name
* @param {Function} valueResolver Function returning the property value. Called
* without arguments.
*/
export function lazy (object, prop, valueResolver) {
let _uninitialized = true
let _value
Object.defineProperty(object, prop, {
get: function () {
if (_uninitialized) {
_value = valueResolver()
_uninitialized = false
}
return _value
},
set: function (value) {
_value = value
_uninitialized = false
},
configurable: true,
enumerable: true
})
}
/**
* Traverse a path into an object.
* When a namespace is missing, it will be created
* @param {Object} object
* @param {string | string[]} path A dot separated string like 'name.space'
* @return {Object} Returns the object at the end of the path
*/
export function traverse (object, path) {
if (path && typeof path === 'string') {
return traverse(object, path.split('.'))
}
let obj = object
if (path) {
for (let i = 0; i < path.length; i++) {
const key = path[i]
if (!(key in obj)) {
obj[key] = {}
}
obj = obj[key]
}
}
return obj
}
/**
* A safe hasOwnProperty
* @param {Object} object
* @param {string} property
*/
export function hasOwnProperty (object, property) {
return object && Object.hasOwnProperty.call(object, property)
}
/**
* Test whether an object is a factory. a factory has fields:
*
* - factory: function (type: Object, config: Object, load: function, typed: function [, math: Object]) (required)
* - name: string (optional)
* - path: string A dot separated path (optional)
* - math: boolean If true (false by default), the math namespace is passed
* as fifth argument of the factory function
*
* @param {*} object
* @returns {boolean}
*/
export function isLegacyFactory (object) {
return object && typeof object.factory === 'function'
}
/**
* Get a nested property from an object
* @param {Object} object
* @param {string | string[]} path
* @returns {Object}
*/
export function get (object, path) {
if (typeof path === 'string') {
if (isPath(path)) {
return get(object, path.split('.'))
} else {
return object[path]
}
}
let child = object
for (let i = 0; i < path.length; i++) {
const key = path[i]
child = child ? child[key] : undefined
}
return child
}
/**
* Set a nested property in an object
* Mutates the object itself
* If the path doesn't exist, it will be created
* @param {Object} object
* @param {string | string[]} path
* @param {*} value
* @returns {Object}
*/
export function set (object, path, value) {
if (typeof path === 'string') {
if (isPath(path)) {
return set(object, path.split('.'), value)
} else {
object[path] = value
return object
}
}
let child = object
for (let i = 0; i < path.length - 1; i++) {
const key = path[i]
if (child[key] === undefined) {
child[key] = {}
}
child = child[key]
}
if (path.length > 0) {
const lastKey = path[path.length - 1]
child[lastKey] = value
}
return object
}
/**
* Create an object composed of the picked object properties
* @param {Object} object
* @param {string[]} properties
* @param {function} [transform] Optional value to transform a value when picking it
* @return {Object}
*/
export function pick (object, properties, transform) {
const copy = {}
for (let i = 0; i < properties.length; i++) {
const key = properties[i]
const value = get(object, key)
if (value !== undefined) {
set(copy, key, transform ? transform(value, key) : value)
}
}
return copy
}
/**
* Shallow version of pick, creating an object composed of the picked object properties
* but not for nested properties
* @param {Object} object
* @param {string[]} properties
* @return {Object}
*/
export function pickShallow (object, properties) {
const copy = {}
for (let i = 0; i < properties.length; i++) {
const key = properties[i]
const value = object[key]
if (value !== undefined) {
copy[key] = value
}
}
return copy
}
// helper function to test whether a string contains a path like 'user.name'
function isPath (str) {
return str.indexOf('.') !== -1
}