1 | ;
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.createTests = exports.createPatch = exports.applyPatch = void 0;
|
4 | var pointer_1 = require("./pointer");
|
5 | var patch_1 = require("./patch");
|
6 | var diff_1 = require("./diff");
|
7 | /**
|
8 | Apply a 'application/json-patch+json'-type patch to an object.
|
9 |
|
10 | `patch` *must* be an array of operations.
|
11 |
|
12 | > Operation objects MUST have exactly one "op" member, whose value
|
13 | > indicates the operation to perform. Its value MUST be one of "add",
|
14 | > "remove", "replace", "move", "copy", or "test"; other values are
|
15 | > errors.
|
16 |
|
17 | This method mutates the target object in-place.
|
18 |
|
19 | @returns list of results, one for each operation: `null` indicated success,
|
20 | otherwise, the result will be an instance of one of the Error classes:
|
21 | MissingError, InvalidOperationError, or TestError.
|
22 | */
|
23 | function applyPatch(object, patch) {
|
24 | return patch.map(function (operation) { return patch_1.apply(object, operation); });
|
25 | }
|
26 | exports.applyPatch = applyPatch;
|
27 | function wrapVoidableDiff(diff) {
|
28 | function wrappedDiff(input, output, ptr) {
|
29 | var custom_patch = diff(input, output, ptr);
|
30 | // ensure an array is always returned
|
31 | return Array.isArray(custom_patch) ? custom_patch : diff_1.diffAny(input, output, ptr, wrappedDiff);
|
32 | }
|
33 | return wrappedDiff;
|
34 | }
|
35 | /**
|
36 | Produce a 'application/json-patch+json'-type patch to get from one object to
|
37 | another.
|
38 |
|
39 | This does not alter `input` or `output` unless they have a property getter with
|
40 | side-effects (which is not a good idea anyway).
|
41 |
|
42 | `diff` is called on each pair of comparable non-primitive nodes in the
|
43 | `input`/`output` object trees, producing nested patches. Return `undefined`
|
44 | to fall back to default behaviour.
|
45 |
|
46 | Returns list of operations to perform on `input` to produce `output`.
|
47 | */
|
48 | function createPatch(input, output, diff) {
|
49 | var ptr = new pointer_1.Pointer();
|
50 | // a new Pointer gets a default path of [''] if not specified
|
51 | return (diff ? wrapVoidableDiff(diff) : diff_1.diffAny)(input, output, ptr);
|
52 | }
|
53 | exports.createPatch = createPatch;
|
54 | /**
|
55 | Create a test operation based on `input`'s current evaluation of the JSON
|
56 | Pointer `path`; if such a pointer cannot be resolved, returns undefined.
|
57 | */
|
58 | function createTest(input, path) {
|
59 | var endpoint = pointer_1.Pointer.fromJSON(path).evaluate(input);
|
60 | if (endpoint !== undefined) {
|
61 | return { op: 'test', path: path, value: endpoint.value };
|
62 | }
|
63 | }
|
64 | /**
|
65 | Produce an 'application/json-patch+json'-type list of tests, to verify that
|
66 | existing values in an object are identical to the those captured at some
|
67 | checkpoint (whenever this function is called).
|
68 |
|
69 | This does not alter `input` or `output` unless they have a property getter with
|
70 | side-effects (which is not a good idea anyway).
|
71 |
|
72 | Returns list of test operations.
|
73 | */
|
74 | function createTests(input, patch) {
|
75 | var tests = new Array();
|
76 | patch.filter(diff_1.isDestructive).forEach(function (operation) {
|
77 | var pathTest = createTest(input, operation.path);
|
78 | if (pathTest)
|
79 | tests.push(pathTest);
|
80 | if ('from' in operation) {
|
81 | var fromTest = createTest(input, operation.from);
|
82 | if (fromTest)
|
83 | tests.push(fromTest);
|
84 | }
|
85 | });
|
86 | return tests;
|
87 | }
|
88 | exports.createTests = createTests;
|