UNPKG

3.29 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.createTests = exports.createPatch = exports.applyPatch = void 0;
4var pointer_1 = require("./pointer");
5var patch_1 = require("./patch");
6var diff_1 = require("./diff");
7/**
8Apply 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
17This 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*/
23function applyPatch(object, patch) {
24 return patch.map(function (operation) { return patch_1.apply(object, operation); });
25}
26exports.applyPatch = applyPatch;
27function 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/**
36Produce a 'application/json-patch+json'-type patch to get from one object to
37another.
38
39This does not alter `input` or `output` unless they have a property getter with
40side-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`
44to fall back to default behaviour.
45
46Returns list of operations to perform on `input` to produce `output`.
47*/
48function 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}
53exports.createPatch = createPatch;
54/**
55Create a test operation based on `input`'s current evaluation of the JSON
56Pointer `path`; if such a pointer cannot be resolved, returns undefined.
57*/
58function 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/**
65Produce an 'application/json-patch+json'-type list of tests, to verify that
66existing values in an object are identical to the those captured at some
67checkpoint (whenever this function is called).
68
69This does not alter `input` or `output` unless they have a property getter with
70side-effects (which is not a good idea anyway).
71
72Returns list of test operations.
73*/
74function 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}
88exports.createTests = createTests;