1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = function (d, b) {
|
4 | extendStatics = Object.setPrototypeOf ||
|
5 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
6 | function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
7 | return extendStatics(d, b);
|
8 | };
|
9 | return function (d, b) {
|
10 | if (typeof b !== "function" && b !== null)
|
11 | throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
12 | extendStatics(d, b);
|
13 | function __() { this.constructor = d; }
|
14 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
15 | };
|
16 | })();
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.apply = exports.InvalidOperationError = exports.test = exports.copy = exports.move = exports.replace = exports.remove = exports.add = exports.TestError = exports.MissingError = void 0;
|
19 | var pointer_1 = require("./pointer");
|
20 | var util_1 = require("./util");
|
21 | var diff_1 = require("./diff");
|
22 | var MissingError = (function (_super) {
|
23 | __extends(MissingError, _super);
|
24 | function MissingError(path) {
|
25 | var _this = _super.call(this, "Value required at path: " + path) || this;
|
26 | _this.path = path;
|
27 | _this.name = 'MissingError';
|
28 | return _this;
|
29 | }
|
30 | return MissingError;
|
31 | }(Error));
|
32 | exports.MissingError = MissingError;
|
33 | var TestError = (function (_super) {
|
34 | __extends(TestError, _super);
|
35 | function TestError(actual, expected) {
|
36 | var _this = _super.call(this, "Test failed: " + actual + " != " + expected) || this;
|
37 | _this.actual = actual;
|
38 | _this.expected = expected;
|
39 | _this.name = 'TestError';
|
40 | return _this;
|
41 | }
|
42 | return TestError;
|
43 | }(Error));
|
44 | exports.TestError = TestError;
|
45 | function _add(object, key, value) {
|
46 | if (Array.isArray(object)) {
|
47 |
|
48 | if (key == '-') {
|
49 | object.push(value);
|
50 | }
|
51 | else {
|
52 | var index = parseInt(key, 10);
|
53 | object.splice(index, 0, value);
|
54 | }
|
55 | }
|
56 | else {
|
57 | object[key] = value;
|
58 | }
|
59 | }
|
60 | function _remove(object, key) {
|
61 | if (Array.isArray(object)) {
|
62 |
|
63 | var index = parseInt(key, 10);
|
64 | object.splice(index, 1);
|
65 | }
|
66 | else {
|
67 |
|
68 | delete object[key];
|
69 | }
|
70 | }
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | function add(object, operation) {
|
80 | var endpoint = pointer_1.Pointer.fromJSON(operation.path).evaluate(object);
|
81 |
|
82 | if (endpoint.parent === undefined) {
|
83 | return new MissingError(operation.path);
|
84 | }
|
85 | _add(endpoint.parent, endpoint.key, util_1.clone(operation.value));
|
86 | return null;
|
87 | }
|
88 | exports.add = add;
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | function remove(object, operation) {
|
94 |
|
95 | var endpoint = pointer_1.Pointer.fromJSON(operation.path).evaluate(object);
|
96 | if (endpoint.value === undefined) {
|
97 | return new MissingError(operation.path);
|
98 | }
|
99 |
|
100 | _remove(endpoint.parent, endpoint.key);
|
101 | return null;
|
102 | }
|
103 | exports.remove = remove;
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | function replace(object, operation) {
|
117 | var endpoint = pointer_1.Pointer.fromJSON(operation.path).evaluate(object);
|
118 | if (endpoint.parent === null) {
|
119 | return new MissingError(operation.path);
|
120 | }
|
121 |
|
122 | if (Array.isArray(endpoint.parent)) {
|
123 | if (parseInt(endpoint.key, 10) >= endpoint.parent.length) {
|
124 | return new MissingError(operation.path);
|
125 | }
|
126 | }
|
127 | else if (endpoint.value === undefined) {
|
128 | return new MissingError(operation.path);
|
129 | }
|
130 | endpoint.parent[endpoint.key] = operation.value;
|
131 | return null;
|
132 | }
|
133 | exports.replace = replace;
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | function move(object, operation) {
|
150 | var from_endpoint = pointer_1.Pointer.fromJSON(operation.from).evaluate(object);
|
151 | if (from_endpoint.value === undefined) {
|
152 | return new MissingError(operation.from);
|
153 | }
|
154 | var endpoint = pointer_1.Pointer.fromJSON(operation.path).evaluate(object);
|
155 | if (endpoint.parent === undefined) {
|
156 | return new MissingError(operation.path);
|
157 | }
|
158 | _remove(from_endpoint.parent, from_endpoint.key);
|
159 | _add(endpoint.parent, endpoint.key, from_endpoint.value);
|
160 | return null;
|
161 | }
|
162 | exports.move = move;
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 | function copy(object, operation) {
|
177 | var from_endpoint = pointer_1.Pointer.fromJSON(operation.from).evaluate(object);
|
178 | if (from_endpoint.value === undefined) {
|
179 | return new MissingError(operation.from);
|
180 | }
|
181 | var endpoint = pointer_1.Pointer.fromJSON(operation.path).evaluate(object);
|
182 | if (endpoint.parent === undefined) {
|
183 | return new MissingError(operation.path);
|
184 | }
|
185 | _add(endpoint.parent, endpoint.key, util_1.clone(from_endpoint.value));
|
186 | return null;
|
187 | }
|
188 | exports.copy = copy;
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 | function test(object, operation) {
|
198 | var endpoint = pointer_1.Pointer.fromJSON(operation.path).evaluate(object);
|
199 |
|
200 | if (diff_1.diffAny(endpoint.value, operation.value, new pointer_1.Pointer()).length) {
|
201 | return new TestError(endpoint.value, operation.value);
|
202 | }
|
203 | return null;
|
204 | }
|
205 | exports.test = test;
|
206 | var InvalidOperationError = (function (_super) {
|
207 | __extends(InvalidOperationError, _super);
|
208 | function InvalidOperationError(operation) {
|
209 | var _this = _super.call(this, "Invalid operation: " + operation.op) || this;
|
210 | _this.operation = operation;
|
211 | _this.name = 'InvalidOperationError';
|
212 | return _this;
|
213 | }
|
214 | return InvalidOperationError;
|
215 | }(Error));
|
216 | exports.InvalidOperationError = InvalidOperationError;
|
217 |
|
218 |
|
219 |
|
220 |
|
221 | function apply(object, operation) {
|
222 |
|
223 |
|
224 |
|
225 | switch (operation.op) {
|
226 | case 'add': return add(object, operation);
|
227 | case 'remove': return remove(object, operation);
|
228 | case 'replace': return replace(object, operation);
|
229 | case 'move': return move(object, operation);
|
230 | case 'copy': return copy(object, operation);
|
231 | case 'test': return test(object, operation);
|
232 | }
|
233 | return new InvalidOperationError(operation);
|
234 | }
|
235 | exports.apply = apply;
|