-
Notifications
You must be signed in to change notification settings - Fork 39
/
patch.ts
213 lines (195 loc) · 7.44 KB
/
patch.ts
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
import {Pointer} from './pointer'
import {clone} from './util'
import {AddOperation,
RemoveOperation,
ReplaceOperation,
MoveOperation,
CopyOperation,
TestOperation,
Operation,
diffAny} from './diff'
export class MissingError extends Error {
constructor(public path: string) {
super(`Value required at path: ${path}`)
this.name = 'MissingError'
}
}
export class TestError extends Error {
constructor(public actual: any, public expected: any) {
super(`Test failed: ${actual} != ${expected}`)
this.name = 'TestError'
}
}
function _add(object: any, key: string, value: any): void {
if (Array.isArray(object)) {
// `key` must be an index
if (key == '-') {
object.push(value)
}
else {
const index = parseInt(key, 10)
object.splice(index, 0, value)
}
}
else {
object[key] = value
}
}
function _remove(object: any, key: string): void {
if (Array.isArray(object)) {
// '-' syntax doesn't make sense when removing
const index = parseInt(key, 10)
object.splice(index, 1)
}
else {
// not sure what the proper behavior is when path = ''
delete object[key]
}
}
/**
> o If the target location specifies an array index, a new value is
> inserted into the array at the specified index.
> o If the target location specifies an object member that does not
> already exist, a new member is added to the object.
> o If the target location specifies an object member that does exist,
> that member's value is replaced.
*/
export function add(object: any, operation: AddOperation): MissingError | null {
const endpoint = Pointer.fromJSON(operation.path).evaluate(object)
// it's not exactly a "MissingError" in the same way that `remove` is -- more like a MissingParent, or something
if (endpoint.parent === undefined) {
return new MissingError(operation.path)
}
_add(endpoint.parent, endpoint.key, clone(operation.value))
return null
}
/**
> The "remove" operation removes the value at the target location.
> The target location MUST exist for the operation to be successful.
*/
export function remove(object: any, operation: RemoveOperation): MissingError | null {
// endpoint has parent, key, and value properties
const endpoint = Pointer.fromJSON(operation.path).evaluate(object)
if (endpoint.value === undefined) {
return new MissingError(operation.path)
}
// not sure what the proper behavior is when path = ''
_remove(endpoint.parent, endpoint.key)
return null
}
/**
> The "replace" operation replaces the value at the target location
> with a new value. The operation object MUST contain a "value" member
> whose content specifies the replacement value.
> The target location MUST exist for the operation to be successful.
> This operation is functionally identical to a "remove" operation for
> a value, followed immediately by an "add" operation at the same
> location with the replacement value.
Even more simply, it's like the add operation with an existence check.
*/
export function replace(object: any, operation: ReplaceOperation): MissingError | null {
const endpoint = Pointer.fromJSON(operation.path).evaluate(object)
if (endpoint.parent === null) {
return new MissingError(operation.path)
}
// this existence check treats arrays as a special case
if (Array.isArray(endpoint.parent)) {
if (parseInt(endpoint.key, 10) >= endpoint.parent.length) {
return new MissingError(operation.path)
}
}
else if (endpoint.value === undefined) {
return new MissingError(operation.path)
}
endpoint.parent[endpoint.key] = clone(operation.value)
return null
}
/**
> The "move" operation removes the value at a specified location and
> adds it to the target location.
> The operation object MUST contain a "from" member, which is a string
> containing a JSON Pointer value that references the location in the
> target document to move the value from.
> This operation is functionally identical to a "remove" operation on
> the "from" location, followed immediately by an "add" operation at
> the target location with the value that was just removed.
> The "from" location MUST NOT be a proper prefix of the "path"
> location; i.e., a location cannot be moved into one of its children.
TODO: throw if the check described in the previous paragraph fails.
*/
export function move(object: any, operation: MoveOperation): MissingError | null {
const from_endpoint = Pointer.fromJSON(operation.from).evaluate(object)
if (from_endpoint.value === undefined) {
return new MissingError(operation.from)
}
const endpoint = Pointer.fromJSON(operation.path).evaluate(object)
if (endpoint.parent === undefined) {
return new MissingError(operation.path)
}
_remove(from_endpoint.parent, from_endpoint.key)
_add(endpoint.parent, endpoint.key, from_endpoint.value)
return null
}
/**
> The "copy" operation copies the value at a specified location to the
> target location.
> The operation object MUST contain a "from" member, which is a string
> containing a JSON Pointer value that references the location in the
> target document to copy the value from.
> The "from" location MUST exist for the operation to be successful.
> This operation is functionally identical to an "add" operation at the
> target location using the value specified in the "from" member.
Alternatively, it's like 'move' without the 'remove'.
*/
export function copy(object: any, operation: CopyOperation): MissingError | null {
const from_endpoint = Pointer.fromJSON(operation.from).evaluate(object)
if (from_endpoint.value === undefined) {
return new MissingError(operation.from)
}
const endpoint = Pointer.fromJSON(operation.path).evaluate(object)
if (endpoint.parent === undefined) {
return new MissingError(operation.path)
}
_add(endpoint.parent, endpoint.key, clone(from_endpoint.value))
return null
}
/**
> The "test" operation tests that a value at the target location is
> equal to a specified value.
> The operation object MUST contain a "value" member that conveys the
> value to be compared to the target location's value.
> The target location MUST be equal to the "value" value for the
> operation to be considered successful.
*/
export function test(object: any, operation: TestOperation): TestError | null {
const endpoint = Pointer.fromJSON(operation.path).evaluate(object)
// TODO: this diffAny(...).length usage could/should be lazy
if (diffAny(endpoint.value, operation.value, new Pointer()).length) {
return new TestError(endpoint.value, operation.value)
}
return null
}
export class InvalidOperationError extends Error {
constructor(public operation: Operation) {
super(`Invalid operation: ${operation.op}`)
this.name = 'InvalidOperationError'
}
}
/**
Switch on `operation.op`, applying the corresponding patch function for each
case to `object`.
*/
export function apply(object: any, operation: Operation): MissingError | InvalidOperationError | TestError | null {
// not sure why TypeScript can't infer typesafety of:
// {add, remove, replace, move, copy, test}[operation.op](object, operation)
// (seems like a bug)
switch (operation.op) {
case 'add': return add(object, operation)
case 'remove': return remove(object, operation)
case 'replace': return replace(object, operation)
case 'move': return move(object, operation)
case 'copy': return copy(object, operation)
case 'test': return test(object, operation)
}
return new InvalidOperationError(operation)
}