-
Notifications
You must be signed in to change notification settings - Fork 28
/
icepick.js
372 lines (319 loc) · 9.1 KB
/
icepick.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
/**
* This allows you to work with object hierarchies that have been frozen
* with Object.freeze(). "get" operations can use the normal JS syntax,
* but operations that modify the data will have to return partial copies of
* the structure. The portions of the structure that did not change will
* === their previous values.
*
* Inspired by clojure/mori and Immutable.js
*/
'use strict'
const i = exports
const identity = coll => coll
// we only care about objects or arrays for now
const weCareAbout = val => val !== null &&
(Array.isArray(val) ||
// This will skip objects created with `new Foo()`
// and objects created with `Object.create(proto)`
// The benefit is ignoring DOM elements and event emitters,
// which are often circular.
isObjectLike(val))
const isObjectLike = val => typeof val === 'object' &&
(val.constructor === Object ||
val.constructor == null) &&
(Object.getPrototypeOf(val) === Object.prototype ||
Object.getPrototypeOf(val) === null)
const forKeys = (obj, iter) => {
let idx, keys
if (Array.isArray(obj)) {
idx = obj.length
while (idx--) {
iter(idx)
}
return
}
keys = Object.keys(obj)
idx = keys.length
while (idx--) {
iter(keys[idx])
}
}
const cloneObj = obj => {
const newObj = obj.constructor == null ? Object.create(null) : {}
const keys = Object.keys(obj)
let idx = keys.length
let key
while (idx--) {
key = keys[idx]
newObj[key] = obj[key]
}
return newObj
}
const clone = (coll) => {
if (Array.isArray(coll)) {
return coll.slice()
} else {
return cloneObj(coll)
}
}
const freezeIfNeeded = process.env.NODE_ENV === 'production'
? identity
: coll => {
if (weCareAbout(coll) && !Object.isFrozen(coll)) {
return baseFreeze(coll)
}
return coll
}
const _freeze = process.env.NODE_ENV === 'production'
? identity
: coll => {
if (typeof coll === 'object') {
return Object.freeze(coll)
} else {
return coll
}
}
const prevNodes = []
const baseFreeze = (coll) => {
if (prevNodes.some(val => val === coll)) {
throw new Error('object has a reference cycle')
}
prevNodes.push(coll)
forKeys(coll, key => {
const prop = coll[key]
if (weCareAbout(prop)) {
baseFreeze(prop)
}
})
prevNodes.pop()
Object.freeze(coll)
return coll
}
/**
* recrursively freeze an object and all its child objects
* @param {Object|Array} coll
* @return {Object|Array}
*/
exports.freeze = process.env.NODE_ENV === 'production'
? identity
: baseFreeze
/**
* recursively un-freeze an object, by cloning frozen collections
* @param {[type]} coll [description]
* @return {[type]} [description]
*/
exports.thaw = function thaw (coll) {
if (!weCareAbout(coll) || !Object.isFrozen(coll)) return coll
const newColl = Array.isArray(coll)
? new Array(coll.length)
: {}
forKeys(coll, key => {
newColl[key] = thaw(coll[key])
})
return newColl
}
/**
* set a value on an object or array
* @param {Object|Array} coll
* @param {String|Number} key Key or index
* @param {Object} value
* @return {Object|Array} new object hierarchy with modifications
*/
exports.assoc = function assoc (coll, key, value) {
if (coll[key] === value) {
return _freeze(coll)
}
const newObj = clone(coll)
newObj[key] = freezeIfNeeded(value)
return _freeze(newObj)
}
exports.set = exports.assoc
/**
* un-set a value on an object or array
* @param {Object|Array} coll
* @param {String|Number} key Key or Index
* @return {Object|Array} New object or array
*/
exports.dissoc = function dissoc (coll, key) {
const newObj = clone(coll)
delete newObj[key]
return _freeze(newObj)
}
exports.unset = exports.dissoc
/**
* set a value deep in a hierarchical structure
* @param {Object|Array} coll
* @param {Array} path A list of keys to traverse
* @param {Object} value
* @return {Object|Array} new object hierarchy with modifications
*/
exports.assocIn = function assocIn (coll, path, value) {
const key0 = path[0]
if (path.length === 1) {
// simplest case is a 1-element array. Just a simple assoc.
return i.assoc(coll, key0, value)
} else {
// break the problem down. Assoc this object with the first key
// and the result of assocIn with the rest of the keys
return i.assoc(coll, key0, assocIn(coll[key0] || {}, path.slice(1), value))
}
}
exports.setIn = exports.assocIn
/**
* un-set a value on an object or array
* @param {Object|Array} coll
* @param {Array} path A list of keys to traverse
* @return {Object|Array} New object or array
*/
exports.dissocIn = function dissocIn (coll, path) {
const key0 = path[0]
if (!coll.hasOwnProperty(key0)) {
return coll
}
if (path.length === 1) {
// simplest case is a 1-element array. Just a simple dissoc.
return i.dissoc(coll, key0)
} else {
// break the problem down. Assoc this object with the first key
// and the result of dissocIn with the rest of the keys
return i.assoc(coll, key0, dissocIn(coll[key0], path.slice(1)))
}
}
exports.unsetIn = exports.dissocIn
/**
* get an object from a hierachy based on an array of keys
* @param {Object|Array} coll
* @param {Array} path list of keys
* @return {Object} value, or undefined
*/
function baseGet (coll, path) {
return (path || []).reduce((curr, key) => {
if (!curr) { return }
return curr[key]
}, coll)
}
exports.getIn = baseGet
/**
* Update a value in a hierarchy
* @param {Object|Array} coll
* @param {Array} path list of keys
* @param {Function} callback The existing value with be passed to this.
* Return the new value to set
* @return {Object|Array} new object hierarchy with modifications
*/
exports.updateIn = function updateIn (coll, path, callback) {
const existingVal = baseGet(coll, path)
return i.assocIn(coll, path, callback(existingVal))
};
// generate wrappers for the mutative array methods
['push', 'unshift', 'pop', 'shift', 'reverse', 'sort']
.forEach((methodName) => {
exports[methodName] = function (arr, val) {
const newArr = [...arr]
newArr[methodName](freezeIfNeeded(val))
return _freeze(newArr)
}
exports[methodName].displayName = 'icepick.' + methodName
})
// splice is special because it is variadic
exports.splice = function splice (arr, ..._args) {
const newArr = [...arr]
const args = _args.map(freezeIfNeeded)
newArr.splice.apply(newArr, args)
return _freeze(newArr)
}
// slice is non-mutative
exports.slice = function slice (arr, arg1, arg2) {
const newArr = arr.slice(arg1, arg2)
return _freeze(newArr)
};
['map', 'filter'].forEach((methodName) => {
exports[methodName] = function (fn, arr) {
const newArr = arr[methodName](fn)
return _freeze(newArr)
}
exports[methodName].displayName = 'icepick.' + methodName
})
exports.extend =
exports.assign = function assign (obj, ...objs) {
const newObj = objs.reduce(singleAssign, obj)
return _freeze(newObj)
}
function singleAssign (obj1, obj2) {
return Object.keys(obj2).reduce((obj, key) => {
return i.assoc(obj, key, obj2[key])
}, obj1)
}
exports.merge = merge
function merge (target, source, resolver) {
if (target == null || source == null) {
return target
}
return Object.keys(source).reduce((obj, key) => {
const sourceVal = source[key]
const targetVal = obj[key]
const resolvedSourceVal =
resolver ? resolver(targetVal, sourceVal, key) : sourceVal
if (weCareAbout(sourceVal) && weCareAbout(targetVal)) {
// if they are both frozen and reference equal, assume they are deep equal
if (
resolvedSourceVal === targetVal &&
(
process.env.NODE_ENV === 'production' ||
(
Object.isFrozen(resolvedSourceVal) &&
Object.isFrozen(targetVal)
)
)
) {
return obj
}
if (Array.isArray(sourceVal)) {
return i.assoc(obj, key, resolvedSourceVal)
}
// recursively merge pairs of objects
return assocIfDifferent(obj, key,
merge(targetVal, resolvedSourceVal, resolver))
}
// primitive values, stuff with prototypes
return assocIfDifferent(obj, key, resolvedSourceVal)
}, target)
}
function assocIfDifferent (target, key, value) {
if (target[key] === value) {
return target
}
return i.assoc(target, key, value)
}
const chainProto = {
value: function value () {
return this.val
},
thru: function thru (fn) {
this.val = freezeIfNeeded(fn(this.val))
return this
}
}
Object.keys(exports).forEach((methodName) => {
if (methodName.match(/^(map|filter)$/)) {
chainProto[methodName] = function (fn) {
this.val = exports[methodName](fn, this.val)
return this
}
return
}
chainProto[methodName] = function (...args) {
this.val = exports[methodName](this.val, ...args)
return this
}
})
exports.chain = function chain (val) {
const wrapped = Object.create(chainProto)
wrapped.val = val
return wrapped
}
// for testing
if (process.env.NODE_ENV !== 'development' &&
process.env.NODE_ENV !== 'production') {
exports._weCareAbout = weCareAbout
}