-
-
Notifications
You must be signed in to change notification settings - Fork 842
/
patches.js
167 lines (160 loc) · 5.21 KB
/
patches.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
import {each, has, diffKeys, isNonNegativeInteger} from "./common"
export function generatePatches(
state,
basepath,
patches,
inversePatches,
baseValue,
resultValue
) {
if (patches)
if (Array.isArray(baseValue))
generateArrayPatches(
state,
basepath,
patches,
inversePatches,
baseValue,
resultValue
)
else
generateObjectPatches(
state,
basepath,
patches,
inversePatches,
baseValue,
resultValue
)
}
export function generateArrayPatches(
state,
basepath,
patches,
inversePatches,
baseValue,
resultValue
) {
const shared = Math.min(baseValue.length, resultValue.length)
for (let i = 0; i < shared; i++) {
if (!has(resultValue, i) && has(baseValue, i)) {
var path = basepath.concat(i)
patches.push({op: "remove", path: path})
inversePatches.push({op: "add", path: path, value: baseValue[i]})
continue
}
if (has(resultValue, i) && !has(baseValue, i)) {
var path = basepath.concat(i)
patches.push({op: "add", path: path, value: resultValue[i]})
inversePatches.push({op: "remove", path: path})
continue
}
if (state.assigned[i] && baseValue[i] !== resultValue[i]) {
const path = basepath.concat(i)
patches.push({op: "replace", path, value: resultValue[i]})
inversePatches.push({op: "replace", path, value: baseValue[i]})
}
}
if (shared < resultValue.length) {
// stuff was added
for (let i = shared; i < resultValue.length; i++) {
const path = basepath.concat(i)
patches.push({op: "add", path, value: resultValue[i]})
}
inversePatches.push({
op: "replace",
path: basepath.concat("length"),
value: baseValue.length
})
} else if (shared < baseValue.length) {
// stuff was removed
patches.push({
op: "replace",
path: basepath.concat("length"),
value: resultValue.length
})
for (let i = shared; i < baseValue.length; i++) {
const path = basepath.concat(i)
inversePatches.push({op: "add", path, value: baseValue[i]})
}
}
//handle the non-index keys
let {added, removed} = diffKeys(baseValue, resultValue)
added = added.filter(v => !isNonNegativeInteger(v))
removed = removed.filter(v => !isNonNegativeInteger(v))
each(added, (_, key) => {
const path = basepath.concat(key)
patches.push({op: "add", path: path, value: resultValue[key]})
inversePatches.push({op: "remove", path: path})
})
each(removed, (_, key) => {
const path = basepath.concat(key)
patches.push({op: "remove", path: path})
inversePatches.push({op: "add", path: path, value: baseValue[key]})
})
}
function generateObjectPatches(
state,
basepath,
patches,
inversePatches,
baseValue,
resultValue
) {
each(state.assigned, (key, assignedValue) => {
const origValue = baseValue[key]
const value = resultValue[key]
const op = !assignedValue
? "remove"
: key in baseValue
? "replace"
: "add"
if (origValue === baseValue && op === "replace") return
const path = basepath.concat(key)
patches.push(op === "remove" ? {op, path} : {op, path, value})
inversePatches.push(
op === "add"
? {op: "remove", path}
: op === "remove"
? {op: "add", path, value: origValue}
: {op: "replace", path, value: origValue}
)
})
}
export function applyPatches(draft, patches) {
for (let i = 0; i < patches.length; i++) {
const patch = patches[i]
if (patch.path.length === 0 && patch.op === "replace") {
draft = patch.value
} else {
const path = patch.path.slice()
const key = path.pop()
const base = path.reduce((current, part) => {
if (!current)
throw new Error(
"Cannot apply patch, path doesn't resolve: " +
patch.path.join("/")
)
return current[part]
}, draft)
if (!base)
throw new Error(
"Cannot apply patch, path doesn't resolve: " +
patch.path.join("/")
)
switch (patch.op) {
case "replace":
case "add":
// TODO: add support is not extensive, it does not support insertion or `-` atm!
base[key] = patch.value
break
case "remove":
delete base[key]
break
default:
throw new Error("Unsupported patch operation: " + patch.op)
}
}
}
return draft
}