-
Notifications
You must be signed in to change notification settings - Fork 761
/
index.js
292 lines (238 loc) · 5.92 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
import jsonPatch from 'fast-json-patch'
import regenerator from 'babel-runtime/regenerator'
import deepExtend from 'deep-extend'
export default {
add,
replace,
remove,
merge,
mergeDeep,
context,
getIn,
applyPatch,
parentPathMatch,
flatten,
fullyNormalizeArray,
normalizeArray,
isPromise,
forEachNew,
forEachNewPrimitive,
isJsonPatch,
isContextPatch,
isPatch,
isMutation,
isAdditiveMutation,
isGenerator,
isFunction,
isObject,
isError
}
function applyPatch(obj, patch, opts) {
opts = opts || {}
patch = Object.assign({}, patch, {
path: patch.path && normalizeJSONPath(patch.path)
})
if (patch.op === 'merge') {
const valPatch = _get(patch.path)
jsonPatch.apply(obj, [valPatch])
Object.assign(valPatch.value, patch.value)
}
else if (patch.op === 'mergeDeep') {
const valPatch = _get(patch.path)
jsonPatch.apply(obj, [valPatch])
deepExtend(valPatch.value, patch.value)
}
else {
jsonPatch.apply(obj, [patch])
// Attach metadata to the resulting value.
if (opts.allowMetaPatches && patch.meta && isAdditiveMutation(patch) &&
(Array.isArray(patch.value) || isObject(patch.value))) {
const valPatch = _get(patch.path)
jsonPatch.apply(obj, [valPatch])
Object.assign(valPatch.value, patch.meta)
}
}
return obj
}
function normalizeJSONPath(path) {
if (Array.isArray(path)) {
if (path.length < 1) {
return ''
}
return '/' + path.map((item) => { // eslint-disable-line prefer-template
return (item + '').replace(/~/g, '~0').replace(/\//g, '~1') // eslint-disable-line prefer-template
}).join('/')
}
return path
}
// =========================
// JSON-Patch Wrappers
// =========================
function add(path, value) {
return {op: 'add', path, value}
}
function _get(path) {
return {op: '_get', path}
}
function replace(path, value, meta) {
return {op: 'replace', path, value, meta}
}
function remove(path, value) {
return {op: 'remove', path}
}
// Custom wrappers
function merge(path, value) {
return {type: 'mutation', op: 'merge', path, value}
}
// Custom wrappers
function mergeDeep(path, value) {
return {type: 'mutation', op: 'mergeDeep', path, value}
}
function context(path, value) {
return {type: 'context', path, value}
}
// =========================
// Iterators
// =========================
function forEachNew(mutations, fn) {
try {
return forEachNewPatch(mutations, forEach, fn)
}
catch (e) {
return e
}
}
function forEachNewPrimitive(mutations, fn) {
try {
return forEachNewPatch(mutations, forEachPrimitive, fn)
}
catch (e) {
return e
}
}
function forEachNewPatch(mutations, fn, callback) {
const res = mutations.filter(isAdditiveMutation).map((mutation) => {
return fn(mutation.value, callback, mutation.path)
}) || []
const flat = flatten(res)
const clean = cleanArray(flat)
return clean
}
function forEachPrimitive(obj, fn, basePath) {
basePath = basePath || []
if (Array.isArray(obj)) {
return obj.map((val, key) => {
return forEachPrimitive(val, fn, basePath.concat(key))
})
}
if (isObject(obj)) {
return Object.keys(obj).map((key) => {
return forEachPrimitive(obj[key], fn, basePath.concat(key))
})
}
return fn(obj, basePath[basePath.length - 1], basePath)
}
function forEach(obj, fn, basePath) {
basePath = basePath || []
let results = []
if (basePath.length > 0) {
const newResults = fn(obj, basePath[basePath.length - 1], basePath)
if (newResults) {
results = results.concat(newResults)
}
}
if (Array.isArray(obj)) {
const arrayResults = obj.map((val, key) => {
return forEach(val, fn, basePath.concat(key))
})
if (arrayResults) {
results = results.concat(arrayResults)
}
}
else if (isObject(obj)) {
const moreResults = Object.keys(obj).map((key) => {
return forEach(obj[key], fn, basePath.concat(key))
})
if (moreResults) {
results = results.concat(moreResults)
}
}
results = flatten(results)
return results
}
// =========================
// Paths
// =========================
function parentPathMatch(path, arr) {
if (!Array.isArray(arr)) {
return false
}
for (let i = 0, len = arr.length; i < len; i++) {
if (arr[i] !== path[i]) {
return false
}
}
return true
}
function getIn(obj, path) {
return path.reduce((val, token) => {
if (typeof token !== 'undefined' && val) {
return val[token]
}
return val
}, obj)
}
// =========================
// Array
// =========================
function fullyNormalizeArray(arr) {
return cleanArray(flatten(normalizeArray(arr)))
}
function normalizeArray(arr) {
return Array.isArray(arr) ? arr : [arr]
}
function flatten(arr) {
return [].concat(...arr.map((val) => {
return Array.isArray(val) ? flatten(val) : val
}))
}
function cleanArray(arr) {
return arr.filter(elm => typeof elm !== 'undefined')
}
// =========================
// Is-Thing.
// =========================
function isObject(val) {
return val && typeof val === 'object'
}
function isPromise(val) {
return isObject(val) && isFunction(val.then)
}
function isFunction(val) {
return val && typeof val === 'function'
}
function isError(patch) {
return patch instanceof Error
}
function isJsonPatch(patch) {
if (isPatch(patch)) {
const op = patch.op
return op === 'add' || op === 'remove' || op === 'replace'
}
return false
}
function isGenerator(thing) {
return regenerator.isGeneratorFunction(thing)
}
function isMutation(patch) {
return isJsonPatch(patch) || (isPatch(patch) && patch.type === 'mutation')
}
function isAdditiveMutation(patch) {
return isMutation(patch) && (patch.op === 'add' || patch.op === 'replace' || patch.op === 'merge' || patch.op === 'mergeDeep')
}
function isContextPatch(patch) {
return isPatch(patch) && patch.type === 'context'
}
function isPatch(patch) {
return patch && typeof patch === 'object'
}