@capture.dev/fast-json-patch 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.txt +23 -0
- package/README.md +428 -0
- package/dist/index.cjs +766 -0
- package/dist/index.d.cts +161 -0
- package/dist/index.d.ts +161 -0
- package/dist/index.js +726 -0
- package/package.json +71 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,726 @@
|
|
|
1
|
+
// src/helpers.ts
|
|
2
|
+
var _hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
3
|
+
function hasOwnProperty(obj, key) {
|
|
4
|
+
return _hasOwnProperty.call(obj, key);
|
|
5
|
+
}
|
|
6
|
+
function _objectKeys(obj) {
|
|
7
|
+
if (Array.isArray(obj)) {
|
|
8
|
+
const keys2 = new Array(obj.length);
|
|
9
|
+
for (let k = 0; k < keys2.length; k++) {
|
|
10
|
+
keys2[k] = "" + k;
|
|
11
|
+
}
|
|
12
|
+
return keys2;
|
|
13
|
+
}
|
|
14
|
+
if (Object.keys) {
|
|
15
|
+
return Object.keys(obj);
|
|
16
|
+
}
|
|
17
|
+
let keys = [];
|
|
18
|
+
for (let i in obj) {
|
|
19
|
+
if (hasOwnProperty(obj, i)) {
|
|
20
|
+
keys.push(i);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return keys;
|
|
24
|
+
}
|
|
25
|
+
function _deepClone(obj) {
|
|
26
|
+
switch (typeof obj) {
|
|
27
|
+
case "object":
|
|
28
|
+
return JSON.parse(JSON.stringify(obj));
|
|
29
|
+
//Faster than ES5 clone - http://jsperf.com/deep-cloning-of-objects/5
|
|
30
|
+
case "undefined":
|
|
31
|
+
return null;
|
|
32
|
+
//this is how JSON.stringify behaves for array items
|
|
33
|
+
default:
|
|
34
|
+
return obj;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function isInteger(str) {
|
|
38
|
+
let i = 0;
|
|
39
|
+
const len = str.length;
|
|
40
|
+
let charCode;
|
|
41
|
+
while (i < len) {
|
|
42
|
+
charCode = str.charCodeAt(i);
|
|
43
|
+
if (charCode >= 48 && charCode <= 57) {
|
|
44
|
+
i++;
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
function escapePathComponent(path) {
|
|
52
|
+
if (path.indexOf("/") === -1 && path.indexOf("~") === -1) return path;
|
|
53
|
+
return path.replace(/~/g, "~0").replace(/\//g, "~1");
|
|
54
|
+
}
|
|
55
|
+
function unescapePathComponent(path) {
|
|
56
|
+
return path.replace(/~1/g, "/").replace(/~0/g, "~");
|
|
57
|
+
}
|
|
58
|
+
function hasUndefined(obj) {
|
|
59
|
+
if (obj === void 0) {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
if (obj) {
|
|
63
|
+
if (Array.isArray(obj)) {
|
|
64
|
+
for (let i2 = 0, len = obj.length; i2 < len; i2++) {
|
|
65
|
+
if (hasUndefined(obj[i2])) {
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
} else if (typeof obj === "object") {
|
|
70
|
+
const objKeys = _objectKeys(obj);
|
|
71
|
+
const objKeysLength = objKeys.length;
|
|
72
|
+
for (var i = 0; i < objKeysLength; i++) {
|
|
73
|
+
if (hasUndefined(obj[objKeys[i]])) {
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
function patchErrorMessageFormatter(message, args) {
|
|
82
|
+
const messageParts = [message];
|
|
83
|
+
for (const key in args) {
|
|
84
|
+
const value = typeof args[key] === "object" ? JSON.stringify(args[key], null, 2) : args[key];
|
|
85
|
+
if (typeof value !== "undefined") {
|
|
86
|
+
messageParts.push(`${key}: ${value}`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return messageParts.join("\n");
|
|
90
|
+
}
|
|
91
|
+
var PatchError = class extends Error {
|
|
92
|
+
constructor(message, name, index, operation, tree) {
|
|
93
|
+
super(
|
|
94
|
+
patchErrorMessageFormatter(message, { name, index, operation, tree })
|
|
95
|
+
);
|
|
96
|
+
this.name = name;
|
|
97
|
+
this.index = index;
|
|
98
|
+
this.operation = operation;
|
|
99
|
+
this.tree = tree;
|
|
100
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
101
|
+
this.message = patchErrorMessageFormatter(message, {
|
|
102
|
+
name,
|
|
103
|
+
index,
|
|
104
|
+
operation,
|
|
105
|
+
tree
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
// src/core.ts
|
|
111
|
+
var JsonPatchError = PatchError;
|
|
112
|
+
var objOps = {
|
|
113
|
+
add: function(obj, key, document) {
|
|
114
|
+
obj[key] = this.value;
|
|
115
|
+
return { newDocument: document };
|
|
116
|
+
},
|
|
117
|
+
remove: function(obj, key, document) {
|
|
118
|
+
var removed = obj[key];
|
|
119
|
+
delete obj[key];
|
|
120
|
+
return { newDocument: document, removed };
|
|
121
|
+
},
|
|
122
|
+
replace: function(obj, key, document) {
|
|
123
|
+
var removed = obj[key];
|
|
124
|
+
obj[key] = this.value;
|
|
125
|
+
return { newDocument: document, removed };
|
|
126
|
+
},
|
|
127
|
+
move: function(obj, key, document) {
|
|
128
|
+
let removed = getValueByPointer(document, this.path);
|
|
129
|
+
if (removed) {
|
|
130
|
+
removed = _deepClone(removed);
|
|
131
|
+
}
|
|
132
|
+
const originalValue = applyOperation(document, {
|
|
133
|
+
op: "remove",
|
|
134
|
+
path: this.from
|
|
135
|
+
}).removed;
|
|
136
|
+
applyOperation(document, {
|
|
137
|
+
op: "add",
|
|
138
|
+
path: this.path,
|
|
139
|
+
value: originalValue
|
|
140
|
+
});
|
|
141
|
+
return { newDocument: document, removed };
|
|
142
|
+
},
|
|
143
|
+
copy: function(obj, key, document) {
|
|
144
|
+
const valueToCopy = getValueByPointer(document, this.from);
|
|
145
|
+
applyOperation(document, {
|
|
146
|
+
op: "add",
|
|
147
|
+
path: this.path,
|
|
148
|
+
value: _deepClone(valueToCopy)
|
|
149
|
+
});
|
|
150
|
+
return { newDocument: document };
|
|
151
|
+
},
|
|
152
|
+
test: function(obj, key, document) {
|
|
153
|
+
return { newDocument: document, test: _areEquals(obj[key], this.value) };
|
|
154
|
+
},
|
|
155
|
+
_get: function(obj, key, document) {
|
|
156
|
+
this.value = obj[key];
|
|
157
|
+
return { newDocument: document };
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
var arrOps = {
|
|
161
|
+
add: function(arr, i, document) {
|
|
162
|
+
if (isInteger(i)) {
|
|
163
|
+
arr.splice(i, 0, this.value);
|
|
164
|
+
} else {
|
|
165
|
+
arr[i] = this.value;
|
|
166
|
+
}
|
|
167
|
+
return { newDocument: document, index: i };
|
|
168
|
+
},
|
|
169
|
+
remove: function(arr, i, document) {
|
|
170
|
+
var removedList = arr.splice(i, 1);
|
|
171
|
+
return { newDocument: document, removed: removedList[0] };
|
|
172
|
+
},
|
|
173
|
+
replace: function(arr, i, document) {
|
|
174
|
+
var removed = arr[i];
|
|
175
|
+
arr[i] = this.value;
|
|
176
|
+
return { newDocument: document, removed };
|
|
177
|
+
},
|
|
178
|
+
move: objOps.move,
|
|
179
|
+
copy: objOps.copy,
|
|
180
|
+
test: objOps.test,
|
|
181
|
+
_get: objOps._get
|
|
182
|
+
};
|
|
183
|
+
function getValueByPointer(document, pointer) {
|
|
184
|
+
if (pointer == "") {
|
|
185
|
+
return document;
|
|
186
|
+
}
|
|
187
|
+
var getOriginalDestination = { op: "_get", path: pointer };
|
|
188
|
+
applyOperation(document, getOriginalDestination);
|
|
189
|
+
return getOriginalDestination.value;
|
|
190
|
+
}
|
|
191
|
+
function applyOperation(document, operation, validateOperation = false, mutateDocument = true, banPrototypeModifications = true, index = 0) {
|
|
192
|
+
if (validateOperation) {
|
|
193
|
+
if (typeof validateOperation == "function") {
|
|
194
|
+
validateOperation(operation, 0, document, operation.path);
|
|
195
|
+
} else {
|
|
196
|
+
validator(operation, 0);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
if (operation.path === "") {
|
|
200
|
+
let returnValue = { newDocument: document };
|
|
201
|
+
if (operation.op === "add") {
|
|
202
|
+
returnValue.newDocument = operation.value;
|
|
203
|
+
return returnValue;
|
|
204
|
+
} else if (operation.op === "replace") {
|
|
205
|
+
returnValue.newDocument = operation.value;
|
|
206
|
+
returnValue.removed = document;
|
|
207
|
+
return returnValue;
|
|
208
|
+
} else if (operation.op === "move" || operation.op === "copy") {
|
|
209
|
+
returnValue.newDocument = getValueByPointer(document, operation.from);
|
|
210
|
+
if (operation.op === "move") {
|
|
211
|
+
returnValue.removed = document;
|
|
212
|
+
}
|
|
213
|
+
return returnValue;
|
|
214
|
+
} else if (operation.op === "test") {
|
|
215
|
+
returnValue.test = _areEquals(document, operation.value);
|
|
216
|
+
if (returnValue.test === false) {
|
|
217
|
+
throw new JsonPatchError(
|
|
218
|
+
"Test operation failed",
|
|
219
|
+
"TEST_OPERATION_FAILED",
|
|
220
|
+
index,
|
|
221
|
+
operation,
|
|
222
|
+
document
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
returnValue.newDocument = document;
|
|
226
|
+
return returnValue;
|
|
227
|
+
} else if (operation.op === "remove") {
|
|
228
|
+
returnValue.removed = document;
|
|
229
|
+
returnValue.newDocument = null;
|
|
230
|
+
return returnValue;
|
|
231
|
+
} else if (operation.op === "_get") {
|
|
232
|
+
operation.value = document;
|
|
233
|
+
return returnValue;
|
|
234
|
+
} else {
|
|
235
|
+
if (validateOperation) {
|
|
236
|
+
throw new JsonPatchError(
|
|
237
|
+
"Operation `op` property is not one of operations defined in RFC-6902",
|
|
238
|
+
"OPERATION_OP_INVALID",
|
|
239
|
+
index,
|
|
240
|
+
operation,
|
|
241
|
+
document
|
|
242
|
+
);
|
|
243
|
+
} else {
|
|
244
|
+
return returnValue;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
} else {
|
|
248
|
+
if (!mutateDocument) {
|
|
249
|
+
document = _deepClone(document);
|
|
250
|
+
}
|
|
251
|
+
const path = operation.path || "";
|
|
252
|
+
const keys = path.split("/");
|
|
253
|
+
let obj = document;
|
|
254
|
+
let t = 1;
|
|
255
|
+
let len = keys.length;
|
|
256
|
+
let existingPathFragment = void 0;
|
|
257
|
+
let key;
|
|
258
|
+
let validateFunction;
|
|
259
|
+
if (typeof validateOperation == "function") {
|
|
260
|
+
validateFunction = validateOperation;
|
|
261
|
+
} else {
|
|
262
|
+
validateFunction = validator;
|
|
263
|
+
}
|
|
264
|
+
while (true) {
|
|
265
|
+
key = keys[t];
|
|
266
|
+
if (key && key.indexOf("~") != -1) {
|
|
267
|
+
key = unescapePathComponent(key);
|
|
268
|
+
}
|
|
269
|
+
if (banPrototypeModifications && (key == "__proto__" || key == "prototype" && t > 0 && keys[t - 1] == "constructor")) {
|
|
270
|
+
throw new TypeError(
|
|
271
|
+
"JSON-Patch: modifying `__proto__` or `constructor/prototype` prop is banned for security reasons, if this was on purpose, please set `banPrototypeModifications` flag false and pass it to this function. More info in fast-json-patch README"
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
if (validateOperation) {
|
|
275
|
+
if (existingPathFragment === void 0) {
|
|
276
|
+
if (obj[key] === void 0) {
|
|
277
|
+
existingPathFragment = keys.slice(0, t).join("/");
|
|
278
|
+
} else if (t == len - 1) {
|
|
279
|
+
existingPathFragment = operation.path;
|
|
280
|
+
}
|
|
281
|
+
if (existingPathFragment !== void 0) {
|
|
282
|
+
validateFunction(operation, 0, document, existingPathFragment);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
t++;
|
|
287
|
+
if (Array.isArray(obj)) {
|
|
288
|
+
if (key === "-") {
|
|
289
|
+
key = obj.length;
|
|
290
|
+
} else {
|
|
291
|
+
if (validateOperation && !isInteger(key)) {
|
|
292
|
+
throw new JsonPatchError(
|
|
293
|
+
"Expected an unsigned base-10 integer value, making the new referenced value the array element with the zero-based index",
|
|
294
|
+
"OPERATION_PATH_ILLEGAL_ARRAY_INDEX",
|
|
295
|
+
index,
|
|
296
|
+
operation,
|
|
297
|
+
document
|
|
298
|
+
);
|
|
299
|
+
} else if (isInteger(key)) {
|
|
300
|
+
key = ~~key;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
if (t >= len) {
|
|
304
|
+
if (validateOperation && operation.op === "add" && typeof key === "number" && key > obj.length) {
|
|
305
|
+
throw new JsonPatchError(
|
|
306
|
+
"The specified index MUST NOT be greater than the number of elements in the array",
|
|
307
|
+
"OPERATION_VALUE_OUT_OF_BOUNDS",
|
|
308
|
+
index,
|
|
309
|
+
operation,
|
|
310
|
+
document
|
|
311
|
+
);
|
|
312
|
+
}
|
|
313
|
+
const returnValue = arrOps[operation.op].call(
|
|
314
|
+
operation,
|
|
315
|
+
obj,
|
|
316
|
+
key,
|
|
317
|
+
document
|
|
318
|
+
);
|
|
319
|
+
if (returnValue.test === false) {
|
|
320
|
+
throw new JsonPatchError(
|
|
321
|
+
"Test operation failed",
|
|
322
|
+
"TEST_OPERATION_FAILED",
|
|
323
|
+
index,
|
|
324
|
+
operation,
|
|
325
|
+
document
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
return returnValue;
|
|
329
|
+
}
|
|
330
|
+
} else {
|
|
331
|
+
if (t >= len) {
|
|
332
|
+
const returnValue = objOps[operation.op].call(
|
|
333
|
+
operation,
|
|
334
|
+
obj,
|
|
335
|
+
key,
|
|
336
|
+
document
|
|
337
|
+
);
|
|
338
|
+
if (returnValue.test === false) {
|
|
339
|
+
throw new JsonPatchError(
|
|
340
|
+
"Test operation failed",
|
|
341
|
+
"TEST_OPERATION_FAILED",
|
|
342
|
+
index,
|
|
343
|
+
operation,
|
|
344
|
+
document
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
return returnValue;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
obj = obj[key];
|
|
351
|
+
if (validateOperation && t < len && (!obj || typeof obj !== "object")) {
|
|
352
|
+
throw new JsonPatchError(
|
|
353
|
+
"Cannot perform operation at the desired path",
|
|
354
|
+
"OPERATION_PATH_UNRESOLVABLE",
|
|
355
|
+
index,
|
|
356
|
+
operation,
|
|
357
|
+
document
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
function applyPatch(document, patch, validateOperation, mutateDocument = true, banPrototypeModifications = true) {
|
|
364
|
+
if (validateOperation) {
|
|
365
|
+
if (!Array.isArray(patch)) {
|
|
366
|
+
throw new JsonPatchError(
|
|
367
|
+
"Patch sequence must be an array",
|
|
368
|
+
"SEQUENCE_NOT_AN_ARRAY"
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
if (!mutateDocument) {
|
|
373
|
+
document = _deepClone(document);
|
|
374
|
+
}
|
|
375
|
+
const results = new Array(patch.length);
|
|
376
|
+
for (let i = 0, length = patch.length; i < length; i++) {
|
|
377
|
+
results[i] = applyOperation(
|
|
378
|
+
document,
|
|
379
|
+
patch[i],
|
|
380
|
+
validateOperation,
|
|
381
|
+
true,
|
|
382
|
+
banPrototypeModifications,
|
|
383
|
+
i
|
|
384
|
+
);
|
|
385
|
+
document = results[i].newDocument;
|
|
386
|
+
}
|
|
387
|
+
results.newDocument = document;
|
|
388
|
+
return results;
|
|
389
|
+
}
|
|
390
|
+
function applyReducer(document, operation, index) {
|
|
391
|
+
const operationResult = applyOperation(
|
|
392
|
+
document,
|
|
393
|
+
operation
|
|
394
|
+
);
|
|
395
|
+
if (operationResult.test === false) {
|
|
396
|
+
throw new JsonPatchError(
|
|
397
|
+
"Test operation failed",
|
|
398
|
+
"TEST_OPERATION_FAILED",
|
|
399
|
+
index,
|
|
400
|
+
operation,
|
|
401
|
+
document
|
|
402
|
+
);
|
|
403
|
+
}
|
|
404
|
+
return operationResult.newDocument;
|
|
405
|
+
}
|
|
406
|
+
function validator(operation, index, document, existingPathFragment) {
|
|
407
|
+
if (typeof operation !== "object" || operation === null || Array.isArray(operation)) {
|
|
408
|
+
throw new JsonPatchError(
|
|
409
|
+
"Operation is not an object",
|
|
410
|
+
"OPERATION_NOT_AN_OBJECT",
|
|
411
|
+
index,
|
|
412
|
+
operation,
|
|
413
|
+
document
|
|
414
|
+
);
|
|
415
|
+
} else if (!objOps[operation.op]) {
|
|
416
|
+
throw new JsonPatchError(
|
|
417
|
+
"Operation `op` property is not one of operations defined in RFC-6902",
|
|
418
|
+
"OPERATION_OP_INVALID",
|
|
419
|
+
index,
|
|
420
|
+
operation,
|
|
421
|
+
document
|
|
422
|
+
);
|
|
423
|
+
} else if (typeof operation.path !== "string") {
|
|
424
|
+
throw new JsonPatchError(
|
|
425
|
+
"Operation `path` property is not a string",
|
|
426
|
+
"OPERATION_PATH_INVALID",
|
|
427
|
+
index,
|
|
428
|
+
operation,
|
|
429
|
+
document
|
|
430
|
+
);
|
|
431
|
+
} else if (operation.path.indexOf("/") !== 0 && operation.path.length > 0) {
|
|
432
|
+
throw new JsonPatchError(
|
|
433
|
+
'Operation `path` property must start with "/"',
|
|
434
|
+
"OPERATION_PATH_INVALID",
|
|
435
|
+
index,
|
|
436
|
+
operation,
|
|
437
|
+
document
|
|
438
|
+
);
|
|
439
|
+
} else if ((operation.op === "move" || operation.op === "copy") && typeof operation.from !== "string") {
|
|
440
|
+
throw new JsonPatchError(
|
|
441
|
+
"Operation `from` property is not present (applicable in `move` and `copy` operations)",
|
|
442
|
+
"OPERATION_FROM_REQUIRED",
|
|
443
|
+
index,
|
|
444
|
+
operation,
|
|
445
|
+
document
|
|
446
|
+
);
|
|
447
|
+
} else if ((operation.op === "add" || operation.op === "replace" || operation.op === "test") && operation.value === void 0) {
|
|
448
|
+
throw new JsonPatchError(
|
|
449
|
+
"Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)",
|
|
450
|
+
"OPERATION_VALUE_REQUIRED",
|
|
451
|
+
index,
|
|
452
|
+
operation,
|
|
453
|
+
document
|
|
454
|
+
);
|
|
455
|
+
} else if ((operation.op === "add" || operation.op === "replace" || operation.op === "test") && hasUndefined(operation.value)) {
|
|
456
|
+
throw new JsonPatchError(
|
|
457
|
+
"Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)",
|
|
458
|
+
"OPERATION_VALUE_CANNOT_CONTAIN_UNDEFINED",
|
|
459
|
+
index,
|
|
460
|
+
operation,
|
|
461
|
+
document
|
|
462
|
+
);
|
|
463
|
+
} else if (document) {
|
|
464
|
+
if (operation.op == "add") {
|
|
465
|
+
var pathLen = operation.path.split("/").length;
|
|
466
|
+
var existingPathLen = existingPathFragment.split("/").length;
|
|
467
|
+
if (pathLen !== existingPathLen + 1 && pathLen !== existingPathLen) {
|
|
468
|
+
throw new JsonPatchError(
|
|
469
|
+
"Cannot perform an `add` operation at the desired path",
|
|
470
|
+
"OPERATION_PATH_CANNOT_ADD",
|
|
471
|
+
index,
|
|
472
|
+
operation,
|
|
473
|
+
document
|
|
474
|
+
);
|
|
475
|
+
}
|
|
476
|
+
} else if (operation.op === "replace" || operation.op === "remove" || operation.op === "_get") {
|
|
477
|
+
if (operation.path !== existingPathFragment) {
|
|
478
|
+
throw new JsonPatchError(
|
|
479
|
+
"Cannot perform the operation at a path that does not exist",
|
|
480
|
+
"OPERATION_PATH_UNRESOLVABLE",
|
|
481
|
+
index,
|
|
482
|
+
operation,
|
|
483
|
+
document
|
|
484
|
+
);
|
|
485
|
+
}
|
|
486
|
+
} else if (operation.op === "move" || operation.op === "copy") {
|
|
487
|
+
var existingValue = {
|
|
488
|
+
op: "_get",
|
|
489
|
+
path: operation.from,
|
|
490
|
+
value: void 0
|
|
491
|
+
};
|
|
492
|
+
var error = validate([existingValue], document);
|
|
493
|
+
if (error && error.name === "OPERATION_PATH_UNRESOLVABLE") {
|
|
494
|
+
throw new JsonPatchError(
|
|
495
|
+
"Cannot perform the operation from a path that does not exist",
|
|
496
|
+
"OPERATION_FROM_UNRESOLVABLE",
|
|
497
|
+
index,
|
|
498
|
+
operation,
|
|
499
|
+
document
|
|
500
|
+
);
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
function validate(sequence, document, externalValidator) {
|
|
506
|
+
try {
|
|
507
|
+
if (!Array.isArray(sequence)) {
|
|
508
|
+
throw new JsonPatchError(
|
|
509
|
+
"Patch sequence must be an array",
|
|
510
|
+
"SEQUENCE_NOT_AN_ARRAY"
|
|
511
|
+
);
|
|
512
|
+
}
|
|
513
|
+
if (document) {
|
|
514
|
+
applyPatch(
|
|
515
|
+
_deepClone(document),
|
|
516
|
+
_deepClone(sequence),
|
|
517
|
+
externalValidator || true
|
|
518
|
+
);
|
|
519
|
+
} else {
|
|
520
|
+
externalValidator = externalValidator || validator;
|
|
521
|
+
for (var i = 0; i < sequence.length; i++) {
|
|
522
|
+
externalValidator(sequence[i], i, document, void 0);
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
} catch (e) {
|
|
526
|
+
if (e instanceof JsonPatchError) {
|
|
527
|
+
return e;
|
|
528
|
+
} else {
|
|
529
|
+
throw e;
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
function _areEquals(a, b) {
|
|
534
|
+
if (a === b) return true;
|
|
535
|
+
if (a && b && typeof a == "object" && typeof b == "object") {
|
|
536
|
+
var arrA = Array.isArray(a), arrB = Array.isArray(b), i, length, key;
|
|
537
|
+
if (arrA && arrB) {
|
|
538
|
+
length = a.length;
|
|
539
|
+
if (length != b.length) return false;
|
|
540
|
+
for (i = length; i-- !== 0; ) if (!_areEquals(a[i], b[i])) return false;
|
|
541
|
+
return true;
|
|
542
|
+
}
|
|
543
|
+
if (arrA != arrB) return false;
|
|
544
|
+
var keys = Object.keys(a);
|
|
545
|
+
length = keys.length;
|
|
546
|
+
if (length !== Object.keys(b).length) return false;
|
|
547
|
+
for (i = length; i-- !== 0; ) if (!b.hasOwnProperty(keys[i])) return false;
|
|
548
|
+
for (i = length; i-- !== 0; ) {
|
|
549
|
+
key = keys[i];
|
|
550
|
+
if (!_areEquals(a[key], b[key])) return false;
|
|
551
|
+
}
|
|
552
|
+
return true;
|
|
553
|
+
}
|
|
554
|
+
return a !== a && b !== b;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
// src/duplex.ts
|
|
558
|
+
var beforeDict = /* @__PURE__ */ new WeakMap();
|
|
559
|
+
var Mirror = class {
|
|
560
|
+
constructor(obj) {
|
|
561
|
+
this.observers = /* @__PURE__ */ new Map();
|
|
562
|
+
this.obj = obj;
|
|
563
|
+
}
|
|
564
|
+
};
|
|
565
|
+
var ObserverInfo = class {
|
|
566
|
+
constructor(callback, observer) {
|
|
567
|
+
this.callback = callback;
|
|
568
|
+
this.observer = observer;
|
|
569
|
+
}
|
|
570
|
+
};
|
|
571
|
+
function getMirror(obj) {
|
|
572
|
+
return beforeDict.get(obj);
|
|
573
|
+
}
|
|
574
|
+
function getObserverFromMirror(mirror, callback) {
|
|
575
|
+
return mirror.observers.get(callback);
|
|
576
|
+
}
|
|
577
|
+
function removeObserverFromMirror(mirror, observer) {
|
|
578
|
+
mirror.observers.delete(observer.callback);
|
|
579
|
+
}
|
|
580
|
+
function unobserve(root, observer) {
|
|
581
|
+
observer.unobserve();
|
|
582
|
+
}
|
|
583
|
+
function observe(obj, callback) {
|
|
584
|
+
var patches = [];
|
|
585
|
+
var observer;
|
|
586
|
+
var mirror = getMirror(obj);
|
|
587
|
+
if (!mirror) {
|
|
588
|
+
mirror = new Mirror(obj);
|
|
589
|
+
beforeDict.set(obj, mirror);
|
|
590
|
+
} else {
|
|
591
|
+
const observerInfo = getObserverFromMirror(mirror, callback);
|
|
592
|
+
observer = observerInfo && observerInfo.observer;
|
|
593
|
+
}
|
|
594
|
+
if (observer) {
|
|
595
|
+
return observer;
|
|
596
|
+
}
|
|
597
|
+
observer = {};
|
|
598
|
+
mirror.value = _deepClone(obj);
|
|
599
|
+
if (callback) {
|
|
600
|
+
observer.callback = callback;
|
|
601
|
+
observer.next = null;
|
|
602
|
+
var dirtyCheck = () => {
|
|
603
|
+
generate(observer);
|
|
604
|
+
};
|
|
605
|
+
var fastCheck = () => {
|
|
606
|
+
clearTimeout(observer.next);
|
|
607
|
+
observer.next = setTimeout(dirtyCheck);
|
|
608
|
+
};
|
|
609
|
+
if (typeof window !== "undefined") {
|
|
610
|
+
window.addEventListener("mouseup", fastCheck);
|
|
611
|
+
window.addEventListener("keyup", fastCheck);
|
|
612
|
+
window.addEventListener("mousedown", fastCheck);
|
|
613
|
+
window.addEventListener("keydown", fastCheck);
|
|
614
|
+
window.addEventListener("change", fastCheck);
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
observer.patches = patches;
|
|
618
|
+
observer.object = obj;
|
|
619
|
+
observer.unobserve = () => {
|
|
620
|
+
generate(observer);
|
|
621
|
+
clearTimeout(observer.next);
|
|
622
|
+
removeObserverFromMirror(mirror, observer);
|
|
623
|
+
if (typeof window !== "undefined") {
|
|
624
|
+
window.removeEventListener("mouseup", fastCheck);
|
|
625
|
+
window.removeEventListener("keyup", fastCheck);
|
|
626
|
+
window.removeEventListener("mousedown", fastCheck);
|
|
627
|
+
window.removeEventListener("keydown", fastCheck);
|
|
628
|
+
window.removeEventListener("change", fastCheck);
|
|
629
|
+
}
|
|
630
|
+
};
|
|
631
|
+
mirror.observers.set(callback, new ObserverInfo(callback, observer));
|
|
632
|
+
return observer;
|
|
633
|
+
}
|
|
634
|
+
function generate(observer, invertible = false) {
|
|
635
|
+
var mirror = beforeDict.get(observer.object);
|
|
636
|
+
_generate(mirror.value, observer.object, observer.patches, "", invertible);
|
|
637
|
+
if (observer.patches.length) {
|
|
638
|
+
applyPatch(mirror.value, observer.patches);
|
|
639
|
+
}
|
|
640
|
+
var temp = observer.patches;
|
|
641
|
+
if (temp.length > 0) {
|
|
642
|
+
observer.patches = [];
|
|
643
|
+
if (observer.callback) {
|
|
644
|
+
observer.callback(temp);
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
return temp;
|
|
648
|
+
}
|
|
649
|
+
function _generate(mirror, obj, patches, path, invertible) {
|
|
650
|
+
if (obj === mirror) {
|
|
651
|
+
return;
|
|
652
|
+
}
|
|
653
|
+
if (typeof obj.toJSON === "function") {
|
|
654
|
+
obj = obj.toJSON();
|
|
655
|
+
}
|
|
656
|
+
var newKeys = _objectKeys(obj);
|
|
657
|
+
var oldKeys = _objectKeys(mirror);
|
|
658
|
+
var changed = false;
|
|
659
|
+
var deleted = false;
|
|
660
|
+
for (var t = oldKeys.length - 1; t >= 0; t--) {
|
|
661
|
+
var key = oldKeys[t];
|
|
662
|
+
var oldVal = mirror[key];
|
|
663
|
+
if (hasOwnProperty(obj, key) && !(obj[key] === void 0 && oldVal !== void 0 && Array.isArray(obj) === false)) {
|
|
664
|
+
var newVal = obj[key];
|
|
665
|
+
if (typeof oldVal == "object" && oldVal != null && typeof newVal == "object" && newVal != null && Array.isArray(oldVal) === Array.isArray(newVal)) {
|
|
666
|
+
_generate(oldVal, newVal, patches, path + "/" + escapePathComponent(key), invertible);
|
|
667
|
+
} else {
|
|
668
|
+
if (oldVal !== newVal) {
|
|
669
|
+
changed = true;
|
|
670
|
+
if (invertible) {
|
|
671
|
+
patches.push({ op: "test", path: path + "/" + escapePathComponent(key), value: _deepClone(oldVal) });
|
|
672
|
+
}
|
|
673
|
+
patches.push({ op: "replace", path: path + "/" + escapePathComponent(key), value: _deepClone(newVal) });
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
} else if (Array.isArray(mirror) === Array.isArray(obj)) {
|
|
677
|
+
if (invertible) {
|
|
678
|
+
patches.push({ op: "test", path: path + "/" + escapePathComponent(key), value: _deepClone(oldVal) });
|
|
679
|
+
}
|
|
680
|
+
patches.push({ op: "remove", path: path + "/" + escapePathComponent(key) });
|
|
681
|
+
deleted = true;
|
|
682
|
+
} else {
|
|
683
|
+
if (invertible) {
|
|
684
|
+
patches.push({ op: "test", path, value: mirror });
|
|
685
|
+
}
|
|
686
|
+
patches.push({ op: "replace", path, value: obj });
|
|
687
|
+
changed = true;
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
if (!deleted && newKeys.length == oldKeys.length) {
|
|
691
|
+
return;
|
|
692
|
+
}
|
|
693
|
+
for (var t = 0; t < newKeys.length; t++) {
|
|
694
|
+
var key = newKeys[t];
|
|
695
|
+
if (!hasOwnProperty(mirror, key) && obj[key] !== void 0) {
|
|
696
|
+
patches.push({ op: "add", path: path + "/" + escapePathComponent(key), value: _deepClone(obj[key]) });
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
function compare(tree1, tree2, invertible = false) {
|
|
701
|
+
var patches = [];
|
|
702
|
+
_generate(tree1, tree2, patches, "", invertible);
|
|
703
|
+
return patches;
|
|
704
|
+
}
|
|
705
|
+
export {
|
|
706
|
+
PatchError as JsonPatchError,
|
|
707
|
+
_areEquals,
|
|
708
|
+
applyOperation,
|
|
709
|
+
applyPatch,
|
|
710
|
+
applyReducer,
|
|
711
|
+
compare,
|
|
712
|
+
_deepClone as deepClone,
|
|
713
|
+
escapePathComponent,
|
|
714
|
+
generate,
|
|
715
|
+
getValueByPointer,
|
|
716
|
+
observe,
|
|
717
|
+
unescapePathComponent,
|
|
718
|
+
unobserve,
|
|
719
|
+
validate,
|
|
720
|
+
validator
|
|
721
|
+
};
|
|
722
|
+
/*!
|
|
723
|
+
* https://github.com/Starcounter-Jack/JSON-Patch
|
|
724
|
+
* (c) 2017-2021 Joachim Wester
|
|
725
|
+
* MIT license
|
|
726
|
+
*/
|