-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
307 lines (254 loc) · 6.8 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
'use strict'
var array = require('x-is-array')
var object = require('is-object')
var proto = require('getprototypeof')
var size = require('unist-util-size')
var objectProto = Object.prototype
var ignoredKeys = ['type', 'children', 'value']
module.exports = diff
function diff(left, right) {
var patch = {left: left}
walk(left, right, patch, 0)
return patch
}
function walk(left, right, patch, index) {
var apply = patch[index]
if (left === right) {
return
}
if (!right) {
apply = append(apply, {type: 'remove', left: left, right: null})
} else if (!left) {
apply = append(apply, {type: 'insert', left: null, right: right})
} else if (left.type === right.type) {
apply = diffProperties(apply, left, right)
/* Nodes of the same type must be of the same kind: if `right` is a
* text, so is `left`. */
if (text(right)) {
apply = diffText(apply, left, right)
} else if (parent(right)) {
apply = diffChildren(apply, left, right, patch, index)
}
} else {
apply = append(apply, {type: 'replace', left: left, right: right})
}
if (apply) {
patch[index] = apply
}
}
function diffText(apply, left, right) {
return left.value === right.value
? apply
: append(apply, {type: 'text', left: left, right: right})
}
function diffChildren(apply, left, right, patch, offset) {
var leftChildren = left.children
var ordered = reorder(leftChildren, right.children)
var children = ordered.children
var length = children.length
var index = -1
var leftChild
var rightChild
while (++index < length) {
leftChild = leftChildren[index]
rightChild = children[index]
offset++
if (leftChild) {
walk(leftChild, rightChild, patch, offset)
} else {
/* Excess nodes in `right` need to be added */
apply = append(apply, {type: 'insert', left: null, right: rightChild})
}
offset += size(leftChild)
}
if (ordered.moves) {
/* Reorder nodes last */
apply = append(apply, {type: 'order', left: left, right: ordered.moves})
}
return apply
}
function diffProperties(apply, left, right) {
var diff = diffObjects(left, right)
return diff ? append(apply, {type: 'props', left: left, right: diff}) : apply
}
function diffObjects(left, right) {
var diff
var leftKey
var rightKey
var leftValue
var rightValue
var objectDiff
for (leftKey in left) {
if (ignoredKeys.indexOf(leftKey) !== -1) {
continue
}
if (!(leftKey in right)) {
diff = diff || {}
diff[leftKey] = undefined
}
leftValue = left[leftKey]
rightValue = right[leftKey]
if (leftValue === rightValue) {
continue
}
if (
object(leftValue) &&
object(rightValue) &&
proto(rightValue) === objectProto &&
proto(leftValue) === objectProto
) {
objectDiff = diffObjects(leftValue, rightValue)
if (objectDiff) {
diff = diff || {}
diff[leftKey] = objectDiff
}
} else {
diff = diff || {}
diff[leftKey] = rightValue
}
}
for (rightKey in right) {
if (ignoredKeys.indexOf(rightKey) !== -1) {
continue
}
if (!(rightKey in left)) {
diff = diff || {}
diff[rightKey] = right[rightKey]
}
}
return diff
}
function append(apply, patch) {
if (!apply) {
return patch
}
if (array(apply)) {
apply.push(patch)
return apply
}
return [apply, patch]
}
/* List diff, naive left to right reordering */
function reorder(leftChildren, rightChildren) {
var leftChildIndex = keyIndex(leftChildren)
var leftKeys = leftChildIndex.key
var leftIndex = leftChildIndex.index
var rightChildIndex = keyIndex(rightChildren)
var rightKeys = rightChildIndex.key
var rightIndex = rightChildIndex.index
var leftLength = leftChildren.length
var rightLength = rightChildren.length
var leftOffset = 0
var rightOffset = 0
var rightMoved = []
var leftMoved = []
var removes = []
var inserts = []
var next = []
var index = -1
var leftKey
var rightKey
while (++index < leftLength) {
leftKey = leftIndex[index]
if (leftKey in rightKeys) {
next.push(rightChildren[rightKeys[leftKey]])
} else {
next.push(null)
}
}
index = -1
while (++index < rightLength) {
if (!(rightIndex[index] in leftKeys)) {
next.push(rightChildren[index])
}
}
leftChildren = next
leftChildIndex = keyIndex(leftChildren)
leftKeys = leftChildIndex.key
leftIndex = leftChildIndex.index
leftLength = leftChildren.length
rightKey = rightIndex[rightOffset]
leftKey = leftIndex[leftOffset]
while (leftOffset < leftLength || rightOffset < rightLength) {
/* The left node moved already. */
if (leftMoved.indexOf(leftOffset) !== -1) {
removes.push({left: leftChildren[leftOffset], right: leftOffset})
leftKey = leftIndex[++leftOffset]
/* The right node moved already. */
} else if (rightMoved.indexOf(rightOffset) !== -1) {
removes.push({
left: rightChildren[rightOffset],
right: leftKeys[rightKey]
})
rightKey = rightIndex[++rightOffset]
} else if (!rightKey) {
leftKey = leftIndex[++leftOffset]
} else if (!leftKey) {
rightKey = rightIndex[++rightOffset]
} else if (rightKey === leftKey) {
leftKey = leftIndex[++leftOffset]
rightKey = rightIndex[++rightOffset]
} else if (
leftKeys[rightKey] - leftOffset >=
rightKeys[leftKey] - rightOffset
) {
inserts.push({left: rightChildren[rightOffset], right: rightOffset})
leftMoved.push(leftKeys[rightKey])
rightKey = rightIndex[++rightOffset]
} else {
inserts.push({left: leftChildren[leftOffset], right: rightKeys[leftKey]})
rightMoved.push(rightKeys[leftKey])
leftKey = leftIndex[++leftOffset]
}
}
if (removes.length === 0 && inserts.length === 0) {
return {children: leftChildren, moves: null}
}
return {
children: leftChildren,
moves: {removes: removes, inserts: inserts}
}
}
function keyIndex(children) {
var keys = {}
var indices = []
var length = children.length
var counts = {}
var index = -1
var child
var key
while (++index < length) {
child = children[index]
if (!child) {
continue
}
key = syntheticKey(child)
if (key in counts) {
key = key + ':' + counts[key]++
} else {
counts[key] = 1
}
indices[index] = key
keys[key] = index
}
return {key: keys, index: indices}
}
function syntheticKey(node) {
var props = {}
var key
for (key in node) {
if (ignoredKeys.indexOf(key) === -1) {
props[key] = node[key]
}
}
return node.type + ':' + JSON.stringify(props)
}
function parent(value) {
return node(value) && 'children' in value
}
function text(value) {
return node(value) && 'value' in value
}
function node(value) {
return object(value) && 'type' in value
}