@vielzeug/patchwork 0.0.0-stage → 26.11.14
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 +21 -0
- package/README.md +44 -2
- package/dist/_equality.cjs +2 -0
- package/dist/_equality.cjs.map +1 -0
- package/dist/_equality.d.ts +9 -0
- package/dist/_equality.d.ts.map +1 -0
- package/dist/_equality.js +16 -0
- package/dist/_equality.js.map +1 -0
- package/dist/errors.cjs +2 -0
- package/dist/errors.cjs.map +1 -0
- package/dist/errors.d.ts +20 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +14 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/merge.cjs +2 -0
- package/dist/merge.cjs.map +1 -0
- package/dist/merge.d.ts +37 -0
- package/dist/merge.d.ts.map +1 -0
- package/dist/merge.js +57 -0
- package/dist/merge.js.map +1 -0
- package/dist/patch.cjs +2 -0
- package/dist/patch.cjs.map +1 -0
- package/dist/patch.d.ts +28 -0
- package/dist/patch.d.ts.map +1 -0
- package/dist/patch.js +241 -0
- package/dist/patch.js.map +1 -0
- package/dist/patchwork.cjs +2 -0
- package/dist/patchwork.cjs.map +1 -0
- package/dist/patchwork.iife.js +2 -0
- package/dist/patchwork.iife.js.map +1 -0
- package/dist/patchwork.js +263 -0
- package/dist/patchwork.js.map +1 -0
- package/dist/types.d.ts +59 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +58 -4
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
var PatchError = class extends Error {
|
|
3
|
+
constructor(e, t) {
|
|
4
|
+
super(e, t), this.name = new.target.name, Object.setPrototypeOf(this, new.target.prototype);
|
|
5
|
+
}
|
|
6
|
+
}, PatchPathError = class extends PatchError {
|
|
7
|
+
pointer;
|
|
8
|
+
constructor(e, t) {
|
|
9
|
+
super(e, t), this.pointer = t?.pointer;
|
|
10
|
+
}
|
|
11
|
+
}, PatchTestError = class extends PatchError {};
|
|
12
|
+
// src/_equality.ts
|
|
13
|
+
function isPlainObject(e) {
|
|
14
|
+
return typeof e == "object" && !!e && !Array.isArray(e);
|
|
15
|
+
}
|
|
16
|
+
function deepEqual(e, t) {
|
|
17
|
+
if (e === t) return !0;
|
|
18
|
+
if (Array.isArray(e) && Array.isArray(t)) return e.length === t.length && e.every((e, n) => deepEqual(e, t[n]));
|
|
19
|
+
if (isPlainObject(e) && isPlainObject(t)) {
|
|
20
|
+
let n = Object.keys(e), r = Object.keys(t);
|
|
21
|
+
return n.length === r.length && n.every((n) => Object.hasOwn(t, n) && deepEqual(e[n], t[n]));
|
|
22
|
+
}
|
|
23
|
+
return !1;
|
|
24
|
+
}
|
|
25
|
+
// src/patch.ts
|
|
26
|
+
function parsePointer(e) {
|
|
27
|
+
if (e === "") return [];
|
|
28
|
+
if (!e.startsWith("/")) throw new PatchPathError(`pointer "${e}" must start with "/"`);
|
|
29
|
+
return e.slice(1).split("/").map((e) => e.replaceAll("~1", "/").replaceAll("~0", "~"));
|
|
30
|
+
}
|
|
31
|
+
function formatPointer(e) {
|
|
32
|
+
return e.map((e) => `/${e.replaceAll("~", "~0").replaceAll("/", "~1")}`).join("");
|
|
33
|
+
}
|
|
34
|
+
function isArrayIndex(e) {
|
|
35
|
+
if (/^(0|[1-9]\d*)$/.test(e)) {
|
|
36
|
+
let t = Number(e);
|
|
37
|
+
return t <= 2 ** 53 - 1 ? t : -1;
|
|
38
|
+
}
|
|
39
|
+
return -1;
|
|
40
|
+
}
|
|
41
|
+
function resolve(e, t) {
|
|
42
|
+
let n = e;
|
|
43
|
+
for (let e of t) {
|
|
44
|
+
if (Array.isArray(n)) {
|
|
45
|
+
let t = isArrayIndex(e);
|
|
46
|
+
n = t >= 0 && t < n.length ? n[t] : void 0;
|
|
47
|
+
} else if (isPlainObject(n)) n = Object.hasOwn(n, e) ? n[e] : void 0;
|
|
48
|
+
else return { exists: !1 };
|
|
49
|
+
if (n === void 0) return { exists: !1 };
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
exists: !0,
|
|
53
|
+
value: n
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
function getIn(e, t) {
|
|
57
|
+
return resolve(e, parsePointer(t)).value;
|
|
58
|
+
}
|
|
59
|
+
function descendArray(e, t, n) {
|
|
60
|
+
let r = isArrayIndex(t);
|
|
61
|
+
if (r < 0 || r >= e.length) throw new PatchPathError(`cannot ${n} at "/${t}": index out of bounds`);
|
|
62
|
+
return r;
|
|
63
|
+
}
|
|
64
|
+
function addIn(e, t, n) {
|
|
65
|
+
let [r = "", ...i] = t;
|
|
66
|
+
if (t.length === 0) return n;
|
|
67
|
+
if (Array.isArray(e)) {
|
|
68
|
+
if (r === "-") {
|
|
69
|
+
if (i.length > 0) throw new PatchPathError("cannot add \"/-\": \"-\" must be the final token");
|
|
70
|
+
return [...e, n];
|
|
71
|
+
}
|
|
72
|
+
let t = isArrayIndex(r);
|
|
73
|
+
if (t < 0 || t > e.length) throw new PatchPathError(`cannot add "/${r}": index out of bounds`);
|
|
74
|
+
if (i.length > 0) {
|
|
75
|
+
if (t === e.length) throw new PatchPathError(`cannot add "/${r}": index out of bounds`);
|
|
76
|
+
return [
|
|
77
|
+
...e.slice(0, t),
|
|
78
|
+
addIn(e[t], i, n),
|
|
79
|
+
...e.slice(t + 1)
|
|
80
|
+
];
|
|
81
|
+
}
|
|
82
|
+
return [
|
|
83
|
+
...e.slice(0, t),
|
|
84
|
+
n,
|
|
85
|
+
...e.slice(t)
|
|
86
|
+
];
|
|
87
|
+
}
|
|
88
|
+
if (isPlainObject(e)) {
|
|
89
|
+
if (i.length > 0) {
|
|
90
|
+
if (!Object.hasOwn(e, r)) throw new PatchPathError(`cannot add "/${r}": member does not exist`);
|
|
91
|
+
return {
|
|
92
|
+
...e,
|
|
93
|
+
[r]: addIn(e[r], i, n)
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
return Object.hasOwn(e, r) && deepEqual(e[r], n) ? e : {
|
|
97
|
+
...e,
|
|
98
|
+
[r]: n
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
throw new PatchPathError(`cannot descend into a ${typeof e}`);
|
|
102
|
+
}
|
|
103
|
+
function replaceIn(e, t, n) {
|
|
104
|
+
let [r = "", ...i] = t;
|
|
105
|
+
if (t.length === 0) return n;
|
|
106
|
+
if (Array.isArray(e)) {
|
|
107
|
+
let t = descendArray(e, r, "replace");
|
|
108
|
+
return i.length === 0 ? deepEqual(e[t], n) ? e : [
|
|
109
|
+
...e.slice(0, t),
|
|
110
|
+
n,
|
|
111
|
+
...e.slice(t + 1)
|
|
112
|
+
] : [
|
|
113
|
+
...e.slice(0, t),
|
|
114
|
+
replaceIn(e[t], i, n),
|
|
115
|
+
...e.slice(t + 1)
|
|
116
|
+
];
|
|
117
|
+
}
|
|
118
|
+
if (isPlainObject(e)) {
|
|
119
|
+
if (!Object.hasOwn(e, r)) throw new PatchPathError(`cannot replace "/${r}": member does not exist`);
|
|
120
|
+
return i.length === 0 ? deepEqual(e[r], n) ? e : {
|
|
121
|
+
...e,
|
|
122
|
+
[r]: n
|
|
123
|
+
} : {
|
|
124
|
+
...e,
|
|
125
|
+
[r]: replaceIn(e[r], i, n)
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
throw new PatchPathError(`cannot descend into a ${typeof e}`);
|
|
129
|
+
}
|
|
130
|
+
function removeIn(e, t) {
|
|
131
|
+
let [n = "", ...r] = t;
|
|
132
|
+
if (t.length === 0) return null;
|
|
133
|
+
if (Array.isArray(e)) {
|
|
134
|
+
let t = descendArray(e, n, "remove");
|
|
135
|
+
return r.length === 0 ? [...e.slice(0, t), ...e.slice(t + 1)] : [
|
|
136
|
+
...e.slice(0, t),
|
|
137
|
+
removeIn(e[t], r),
|
|
138
|
+
...e.slice(t + 1)
|
|
139
|
+
];
|
|
140
|
+
}
|
|
141
|
+
if (isPlainObject(e)) {
|
|
142
|
+
if (!Object.hasOwn(e, n)) throw new PatchPathError(`cannot remove "/${n}": member does not exist`);
|
|
143
|
+
if (r.length === 0) {
|
|
144
|
+
let t = { ...e };
|
|
145
|
+
return delete t[n], t;
|
|
146
|
+
}
|
|
147
|
+
return {
|
|
148
|
+
...e,
|
|
149
|
+
[n]: removeIn(e[n], r)
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
throw new PatchPathError(`cannot descend into a ${typeof e}`);
|
|
153
|
+
}
|
|
154
|
+
function requireValue(e, t, n) {
|
|
155
|
+
let r = resolve(e, t);
|
|
156
|
+
if (!r.exists) throw new PatchPathError(`pointer "${n}" does not resolve`, { pointer: n });
|
|
157
|
+
return r.value;
|
|
158
|
+
}
|
|
159
|
+
function at(e, t) {
|
|
160
|
+
try {
|
|
161
|
+
return t();
|
|
162
|
+
} catch (t) {
|
|
163
|
+
throw t instanceof PatchPathError && t.pointer === void 0 ? new PatchPathError(t.message, {
|
|
164
|
+
cause: t,
|
|
165
|
+
pointer: e
|
|
166
|
+
}) : t;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
function applyOperation(e, t) {
|
|
170
|
+
switch (t.op) {
|
|
171
|
+
case "add": return at(t.path, () => addIn(e, parsePointer(t.path), structuredClone(t.value)));
|
|
172
|
+
case "remove": return at(t.path, () => removeIn(e, parsePointer(t.path)));
|
|
173
|
+
case "replace": return at(t.path, () => replaceIn(e, parsePointer(t.path), structuredClone(t.value)));
|
|
174
|
+
case "move": {
|
|
175
|
+
let n = at(t.from, () => requireValue(e, parsePointer(t.from), t.from)), r = at(t.from, () => removeIn(e, parsePointer(t.from)));
|
|
176
|
+
return at(t.path, () => addIn(r, parsePointer(t.path), n));
|
|
177
|
+
}
|
|
178
|
+
case "copy": {
|
|
179
|
+
let n = at(t.from, () => requireValue(e, parsePointer(t.from), t.from));
|
|
180
|
+
return at(t.path, () => addIn(e, parsePointer(t.path), structuredClone(n)));
|
|
181
|
+
}
|
|
182
|
+
case "test": {
|
|
183
|
+
let n = resolve(e, parsePointer(t.path));
|
|
184
|
+
if (!n.exists || !deepEqual(n.value, t.value)) throw new PatchTestError(`test failed at "${t.path}"`);
|
|
185
|
+
return e;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
function apply(e, t) {
|
|
190
|
+
let n = e;
|
|
191
|
+
for (let e of t) n = applyOperation(n, e);
|
|
192
|
+
return n;
|
|
193
|
+
}
|
|
194
|
+
function tryApply(e, t) {
|
|
195
|
+
let n = e;
|
|
196
|
+
for (let e = 0; e < t.length; e += 1) {
|
|
197
|
+
let r = t[e];
|
|
198
|
+
try {
|
|
199
|
+
n = applyOperation(n, r);
|
|
200
|
+
} catch (t) {
|
|
201
|
+
let n = t instanceof PatchTestError ? "test" : "path", i = t instanceof PatchPathError ? t.pointer ?? r.path : r.path;
|
|
202
|
+
return {
|
|
203
|
+
error: {
|
|
204
|
+
index: e,
|
|
205
|
+
message: t.message,
|
|
206
|
+
path: i,
|
|
207
|
+
reason: n
|
|
208
|
+
},
|
|
209
|
+
ok: !1
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return {
|
|
214
|
+
doc: n,
|
|
215
|
+
ok: !0
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
function diffNode(e, t, n, r) {
|
|
219
|
+
if (!deepEqual(t, n)) {
|
|
220
|
+
if (Array.isArray(t) && Array.isArray(n)) {
|
|
221
|
+
let i = Math.min(t.length, n.length), a = 0;
|
|
222
|
+
for (; a < i && deepEqual(t[a], n[a]);) a += 1;
|
|
223
|
+
let o = 0;
|
|
224
|
+
for (; o < i - a && deepEqual(t[t.length - 1 - o], n[n.length - 1 - o]);) o += 1;
|
|
225
|
+
let s = t.length - o, c = n.length - o, l = Math.min(s, c) - a;
|
|
226
|
+
for (let i = a; i < a + l; i += 1) diffNode(`${e}/${i}`, t[i], n[i], r);
|
|
227
|
+
for (let t = s - 1; t >= a + l; --t) r.push({
|
|
228
|
+
op: "remove",
|
|
229
|
+
path: `${e}/${t}`
|
|
230
|
+
});
|
|
231
|
+
let u = o === 0;
|
|
232
|
+
for (let t = a + l; t < c; t += 1) r.push({
|
|
233
|
+
op: "add",
|
|
234
|
+
path: u ? `${e}/-` : `${e}/${t}`,
|
|
235
|
+
value: n[t]
|
|
236
|
+
});
|
|
237
|
+
} else if (isPlainObject(t) && isPlainObject(n)) {
|
|
238
|
+
for (let i of Object.keys(t)) Object.hasOwn(n, i) || r.push({
|
|
239
|
+
op: "remove",
|
|
240
|
+
path: `${e}${formatPointer([i])}`
|
|
241
|
+
});
|
|
242
|
+
for (let i of Object.keys(n)) {
|
|
243
|
+
let a = `${e}${formatPointer([i])}`, o = n[i];
|
|
244
|
+
Object.hasOwn(t, i) ? diffNode(a, t[i], o, r) : r.push({
|
|
245
|
+
op: "add",
|
|
246
|
+
path: a,
|
|
247
|
+
value: o
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
} else r.push({
|
|
251
|
+
op: "replace",
|
|
252
|
+
path: e,
|
|
253
|
+
value: n
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
function diff(e, t) {
|
|
258
|
+
let n = [];
|
|
259
|
+
return diffNode("", e, t, n), n;
|
|
260
|
+
}
|
|
261
|
+
export { PatchError, PatchPathError, PatchTestError, apply, diff, formatPointer, getIn, parsePointer, tryApply };
|
|
262
|
+
|
|
263
|
+
//# sourceMappingURL=patchwork.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"patchwork.js","names":["message","options","value","a","b","item","index","aKeys","bKeys","key","pointer","token","tokens","current","doc","action","rest","next","found","run","error","operation","removed","patch","reason","path","from","to","shared","head","tail","fromMiddle","toMiddle","overlap","ops","i","appends","childPath","toValue"],"sources":["../src/errors.ts","../src/_equality.ts","../src/patch.ts"],"sourcesContent":["import type { JsonPointer } from './types';\n\n/** Base class for all patchwork errors. */\nexport class PatchError extends Error {\n constructor(message: string, options?: ErrorOptions) {\n super(message, options);\n this.name = new.target.name;\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\n/**\n * The document could not be patched because a pointer did not resolve. `pointer` names the\n * offending pointer when the library can attribute one (the failing `path` of a `move` is\n * not its `from`).\n */\nexport class PatchPathError extends PatchError {\n readonly pointer: JsonPointer | undefined;\n\n constructor(message: string, options?: ErrorOptions & { pointer?: JsonPointer }) {\n super(message, options);\n this.pointer = options?.pointer;\n }\n}\n\n/** A `test` operation did not match the document. */\nexport class PatchTestError extends PatchError {}\n","/**\n * Internal JSON-domain helpers shared by the patch engine and the merge module.\n * Never re-exported from the package barrel.\n */\nimport type { JsonObject, JsonValue } from './types';\n\nexport function isPlainObject(value: JsonValue | undefined): value is JsonObject {\n return typeof value === 'object' && value !== null && !Array.isArray(value);\n}\n\n/** Structural equality over the JSON domain: no NaN/undefined/prototype concerns. */\nexport function deepEqual(a: JsonValue, b: JsonValue): boolean {\n if (a === b) return true;\n if (Array.isArray(a) && Array.isArray(b)) {\n return a.length === b.length && a.every((item, index) => deepEqual(item, b[index] as JsonValue));\n }\n if (isPlainObject(a) && isPlainObject(b)) {\n const aKeys = Object.keys(a);\n const bKeys = Object.keys(b);\n return (\n aKeys.length === bKeys.length &&\n aKeys.every((key) => Object.hasOwn(b, key) && deepEqual(a[key] as JsonValue, b[key] as JsonValue))\n );\n }\n return false;\n}\n","import { deepEqual, isPlainObject } from './_equality';\nimport { PatchPathError, PatchTestError } from './errors';\nimport type { ApplyResult, JsonObject, JsonPatch, JsonPointer, JsonValue, PatchOperation } from './types';\n\n// ---------------------------------------------------------------------------\n// RFC 6901 pointers\n// ---------------------------------------------------------------------------\n\n/** Split a JSON Pointer into decoded reference tokens (`~1` → `/`, `~0` → `~`). */\nexport function parsePointer(pointer: JsonPointer): string[] {\n if (pointer === '') return [];\n if (!pointer.startsWith('/')) throw new PatchPathError(`pointer \"${pointer}\" must start with \"/\"`);\n return pointer\n .slice(1)\n .split('/')\n .map((token) => token.replaceAll('~1', '/').replaceAll('~0', '~'));\n}\n\n/** Escape and join reference tokens into a JSON Pointer. */\nexport function formatPointer(tokens: readonly string[]): JsonPointer {\n return tokens.map((token) => `/${token.replaceAll('~', '~0').replaceAll('/', '~1')}`).join('');\n}\n\nfunction isArrayIndex(token: string): number {\n if (/^(0|[1-9]\\d*)$/.test(token)) {\n const index = Number(token);\n return index <= Number.MAX_SAFE_INTEGER ? index : -1;\n }\n return -1;\n}\n\n/** Resolve a pointer, distinguishing \"targets null\" from \"does not exist\". */\nfunction resolve(doc: JsonValue, tokens: readonly string[]): { exists: boolean; value?: JsonValue } {\n let current: JsonValue | undefined = doc;\n for (const token of tokens) {\n if (Array.isArray(current)) {\n const index = isArrayIndex(token);\n current = index >= 0 && index < current.length ? current[index] : undefined;\n } else if (isPlainObject(current)) {\n current = Object.hasOwn(current, token) ? current[token] : undefined;\n } else {\n return { exists: false };\n }\n if (current === undefined) return { exists: false };\n }\n return { exists: true, value: current };\n}\n\n/** Read the value a pointer targets; returns `undefined` when it does not resolve. */\nexport function getIn(doc: JsonValue, pointer: JsonPointer): JsonValue | undefined {\n return resolve(doc, parsePointer(pointer)).value;\n}\n\n// ---------------------------------------------------------------------------\n// Copy-on-write path updates (untouched subtrees keep their identity)\n// ---------------------------------------------------------------------------\n\nfunction descendArray(doc: readonly JsonValue[], token: string, action: string): number {\n const index = isArrayIndex(token);\n if (index < 0 || index >= doc.length) {\n throw new PatchPathError(`cannot ${action} at \"/${token}\": index out of bounds`);\n }\n return index;\n}\n\n/** Insert (arrays) or set (objects); `add` semantics: index may equal the array length. */\nfunction addIn(doc: JsonValue, tokens: readonly string[], value: JsonValue): JsonValue {\n const [token = '', ...rest] = tokens;\n if (tokens.length === 0) return value;\n if (Array.isArray(doc)) {\n if (token === '-') {\n if (rest.length > 0) throw new PatchPathError(`cannot add \"/-\": \"-\" must be the final token`);\n return [...doc, value];\n }\n const index = isArrayIndex(token);\n if (index < 0 || index > doc.length) {\n throw new PatchPathError(`cannot add \"/${token}\": index out of bounds`);\n }\n if (rest.length > 0) {\n if (index === doc.length) throw new PatchPathError(`cannot add \"/${token}\": index out of bounds`);\n return [...doc.slice(0, index), addIn(doc[index] as JsonValue, rest, value), ...doc.slice(index + 1)];\n }\n return [...doc.slice(0, index), value, ...doc.slice(index)];\n }\n if (isPlainObject(doc)) {\n if (rest.length > 0) {\n if (!Object.hasOwn(doc, token)) throw new PatchPathError(`cannot add \"/${token}\": member does not exist`);\n return { ...doc, [token]: addIn(doc[token] as JsonValue, rest, value) };\n }\n if (Object.hasOwn(doc, token) && deepEqual(doc[token] as JsonValue, value)) return doc;\n return { ...doc, [token]: value };\n }\n throw new PatchPathError(`cannot descend into a ${typeof doc}`);\n}\n\n/** Replace in place; the target location must already exist (RFC 6902). */\nfunction replaceIn(doc: JsonValue, tokens: readonly string[], value: JsonValue): JsonValue {\n const [token = '', ...rest] = tokens;\n if (tokens.length === 0) return value;\n if (Array.isArray(doc)) {\n const index = descendArray(doc, token, 'replace');\n if (rest.length === 0) {\n if (deepEqual(doc[index] as JsonValue, value)) return doc;\n return [...doc.slice(0, index), value, ...doc.slice(index + 1)];\n }\n return [...doc.slice(0, index), replaceIn(doc[index] as JsonValue, rest, value), ...doc.slice(index + 1)];\n }\n if (isPlainObject(doc)) {\n if (!Object.hasOwn(doc, token)) throw new PatchPathError(`cannot replace \"/${token}\": member does not exist`);\n if (rest.length === 0) {\n if (deepEqual(doc[token] as JsonValue, value)) return doc;\n return { ...doc, [token]: value };\n }\n return { ...doc, [token]: replaceIn(doc[token] as JsonValue, rest, value) };\n }\n throw new PatchPathError(`cannot descend into a ${typeof doc}`);\n}\n\n/** Remove the member/index the pointer targets; it must exist. */\nfunction removeIn(doc: JsonValue, tokens: readonly string[]): JsonValue {\n const [token = '', ...rest] = tokens;\n if (tokens.length === 0) return null;\n if (Array.isArray(doc)) {\n const index = descendArray(doc, token, 'remove');\n if (rest.length === 0) return [...doc.slice(0, index), ...doc.slice(index + 1)];\n return [...doc.slice(0, index), removeIn(doc[index] as JsonValue, rest), ...doc.slice(index + 1)];\n }\n if (isPlainObject(doc)) {\n if (!Object.hasOwn(doc, token)) throw new PatchPathError(`cannot remove \"/${token}\": member does not exist`);\n if (rest.length === 0) {\n const next: JsonObject = { ...doc };\n delete next[token];\n return next;\n }\n return { ...doc, [token]: removeIn(doc[token] as JsonValue, rest) };\n }\n throw new PatchPathError(`cannot descend into a ${typeof doc}`);\n}\n\nfunction requireValue(doc: JsonValue, tokens: readonly string[], pointer: JsonPointer): JsonValue {\n const found = resolve(doc, tokens);\n if (!found.exists) throw new PatchPathError(`pointer \"${pointer}\" does not resolve`, { pointer });\n return found.value as JsonValue;\n}\n\n/** Attributes path failures thrown inside `run` to `pointer` (the caller's full pointer). */\nfunction at<T>(pointer: JsonPointer, run: () => T): T {\n try {\n return run();\n } catch (error) {\n if (error instanceof PatchPathError && error.pointer === undefined) {\n throw new PatchPathError(error.message, { cause: error, pointer });\n }\n throw error;\n }\n}\n\n// ---------------------------------------------------------------------------\n// RFC 6902 apply / tryApply\n// ---------------------------------------------------------------------------\n\nfunction applyOperation(doc: JsonValue, operation: PatchOperation): JsonValue {\n switch (operation.op) {\n case 'add':\n return at(operation.path, () => addIn(doc, parsePointer(operation.path), structuredClone(operation.value)));\n case 'remove':\n return at(operation.path, () => removeIn(doc, parsePointer(operation.path)));\n case 'replace':\n return at(operation.path, () => replaceIn(doc, parsePointer(operation.path), structuredClone(operation.value)));\n case 'move': {\n const value = at(operation.from, () => requireValue(doc, parsePointer(operation.from), operation.from));\n const removed = at(operation.from, () => removeIn(doc, parsePointer(operation.from)));\n return at(operation.path, () => addIn(removed, parsePointer(operation.path), value));\n }\n case 'copy': {\n const value = at(operation.from, () => requireValue(doc, parsePointer(operation.from), operation.from));\n return at(operation.path, () => addIn(doc, parsePointer(operation.path), structuredClone(value)));\n }\n case 'test': {\n const found = resolve(doc, parsePointer(operation.path));\n if (!found.exists || !deepEqual(found.value as JsonValue, operation.value)) {\n throw new PatchTestError(`test failed at \"${operation.path}\"`);\n }\n return doc;\n }\n }\n}\n\n/**\n * Apply an RFC 6902 patch immutably: untouched subtrees keep their identity,\n * so consumers can diff by reference afterwards. Throws {@link PatchPathError}\n * when a pointer does not resolve and {@link PatchTestError} when a `test`\n * operation does not match — use {@link tryApply} in protocol code where a\n * rejection is an ordinary answer rather than an exception.\n */\nexport function apply<T extends JsonValue>(doc: T, patch: JsonPatch): T {\n let current: JsonValue = doc;\n for (const operation of patch) current = applyOperation(current, operation);\n return current as T;\n}\n\n/** Non-throwing {@link apply}: returns the patched doc or a structured rejection with the failing index. */\nexport function tryApply(doc: JsonValue, patch: JsonPatch): ApplyResult {\n let current: JsonValue = doc;\n for (let index = 0; index < patch.length; index += 1) {\n const operation = patch[index] as PatchOperation;\n try {\n current = applyOperation(current, operation);\n } catch (error) {\n const reason = error instanceof PatchTestError ? 'test' : 'path';\n const path = error instanceof PatchPathError ? (error.pointer ?? operation.path) : operation.path;\n return { error: { index, message: (error as Error).message, path, reason }, ok: false };\n }\n }\n return { doc: current, ok: true };\n}\n\n// ---------------------------------------------------------------------------\n// diff\n// ---------------------------------------------------------------------------\n\nfunction diffNode(path: string, from: JsonValue, to: JsonValue, ops: PatchOperation[]): void {\n if (deepEqual(from, to)) return;\n if (Array.isArray(from) && Array.isArray(to)) {\n // Align on the common prefix and (non-overlapping) common suffix, recurse positionally\n // through the middle, then let surplus removals fall off the middle's tail (descending)\n // and surplus inserts land in ascending index order. True appends keep the `-` token.\n const shared = Math.min(from.length, to.length);\n let head = 0;\n while (head < shared && deepEqual(from[head] as JsonValue, to[head] as JsonValue)) head += 1;\n let tail = 0;\n while (\n tail < shared - head &&\n deepEqual(from[from.length - 1 - tail] as JsonValue, to[to.length - 1 - tail] as JsonValue)\n ) {\n tail += 1;\n }\n const fromMiddle = from.length - tail; // exclusive end of from's middle\n const toMiddle = to.length - tail; // exclusive end of to's middle\n const overlap = Math.min(fromMiddle, toMiddle) - head;\n for (let i = head; i < head + overlap; i += 1) {\n diffNode(`${path}/${i}`, from[i] as JsonValue, to[i] as JsonValue, ops);\n }\n for (let i = fromMiddle - 1; i >= head + overlap; i -= 1) {\n ops.push({ op: 'remove', path: `${path}/${i}` });\n }\n const appends = tail === 0; // surplus only ever lands at the end when no tail is shared\n for (let i = head + overlap; i < toMiddle; i += 1) {\n ops.push({ op: 'add', path: appends ? `${path}/-` : `${path}/${i}`, value: to[i] as JsonValue });\n }\n return;\n }\n if (isPlainObject(from) && isPlainObject(to)) {\n for (const key of Object.keys(from)) {\n if (!Object.hasOwn(to, key)) ops.push({ op: 'remove', path: `${path}${formatPointer([key])}` });\n }\n for (const key of Object.keys(to)) {\n const childPath = `${path}${formatPointer([key])}`;\n const toValue = to[key] as JsonValue;\n if (!Object.hasOwn(from, key)) {\n ops.push({ op: 'add', path: childPath, value: toValue });\n } else {\n diffNode(childPath, from[key] as JsonValue, toValue, ops);\n }\n }\n return;\n }\n ops.push({ op: 'replace', path, value: to });\n}\n\n/**\n * Compute an RFC 6902 patch that transforms `from` into `to`. Object keys\n * diff field-by-field (the payload win sync protocols want); arrays align\n * on their common prefix and suffix and emit ordered removals and indexed\n * inserts around a positionally diffed middle, so appends, prepends, and\n * middle inserts each stay one operation. Not minimal-edit: a reorder\n * diffs positionally. The result always round-trips:\n * `apply(from, diff(from, to))` deep-equals `to`.\n */\nexport function diff(from: JsonValue, to: JsonValue): JsonPatch {\n const ops: PatchOperation[] = [];\n diffNode('', from, to, ops);\n return ops;\n}\n"],"mappings":";AAGA,IAAa,aAAb,cAAgC,MAAM;CACpC,YAAY,GAAiB,GAAwB;EAGnD,AAFA,MAAMA,GAASC,CAAO,GACtB,KAAK,OAAO,WAAW,MACvB,OAAO,eAAe,MAAM,WAAW,SAAS;CAClD;AACF,GAOa,iBAAb,cAAoC,WAAW;CAC7C;CAEA,YAAY,GAAiB,GAAoD;EAE/E,AADA,MAAMD,GAAS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AC;GAI3D;EAEF,OACA,EAAI,KAAK;GAAE,IAAI;GAAW;GAAM,OAAOX;EAAG,CAAC;CAhB3C;AAiBF;AAWA,SAAgB,KAAK,GAAiB,GAA0B;CAC9D,IAAMO,IAAwB,CAAC;CAE/B,OADA,SAAS,IAAIR,GAAMC,GAAIO,CAAG,GACnBA;AACT"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/** A JSON value: the only shape patchwork reads and writes. */
|
|
2
|
+
export type JsonValue = null | boolean | number | string | JsonValue[] | {
|
|
3
|
+
[key: string]: JsonValue;
|
|
4
|
+
};
|
|
5
|
+
/** An object value (the target of most patches). */
|
|
6
|
+
export type JsonObject = {
|
|
7
|
+
[key: string]: JsonValue;
|
|
8
|
+
};
|
|
9
|
+
/** RFC 6901 JSON Pointer, e.g. `/address/street` or `/items/0`. */
|
|
10
|
+
export type JsonPointer = string;
|
|
11
|
+
export type AddOperation = {
|
|
12
|
+
op: 'add';
|
|
13
|
+
path: JsonPointer;
|
|
14
|
+
value: JsonValue;
|
|
15
|
+
};
|
|
16
|
+
export type RemoveOperation = {
|
|
17
|
+
op: 'remove';
|
|
18
|
+
path: JsonPointer;
|
|
19
|
+
};
|
|
20
|
+
export type ReplaceOperation = {
|
|
21
|
+
op: 'replace';
|
|
22
|
+
path: JsonPointer;
|
|
23
|
+
value: JsonValue;
|
|
24
|
+
};
|
|
25
|
+
export type MoveOperation = {
|
|
26
|
+
op: 'move';
|
|
27
|
+
from: JsonPointer;
|
|
28
|
+
path: JsonPointer;
|
|
29
|
+
};
|
|
30
|
+
export type CopyOperation = {
|
|
31
|
+
op: 'copy';
|
|
32
|
+
from: JsonPointer;
|
|
33
|
+
path: JsonPointer;
|
|
34
|
+
};
|
|
35
|
+
export type TestOperation = {
|
|
36
|
+
op: 'test';
|
|
37
|
+
path: JsonPointer;
|
|
38
|
+
value: JsonValue;
|
|
39
|
+
};
|
|
40
|
+
/** One RFC 6902 operation. */
|
|
41
|
+
export type PatchOperation = AddOperation | CopyOperation | MoveOperation | RemoveOperation | ReplaceOperation | TestOperation;
|
|
42
|
+
/** An RFC 6902 JSON Patch: an ordered list of operations. */
|
|
43
|
+
export type JsonPatch = readonly PatchOperation[];
|
|
44
|
+
/** Result of {@link tryApply}. Never throws: a failed `test` is data, not an exception. */
|
|
45
|
+
export type ApplyResult = {
|
|
46
|
+
doc: JsonValue;
|
|
47
|
+
ok: true;
|
|
48
|
+
} | {
|
|
49
|
+
error: PatchErrorInfo;
|
|
50
|
+
ok: false;
|
|
51
|
+
};
|
|
52
|
+
/** A machine-readable description of why a patch was rejected. */
|
|
53
|
+
export type PatchErrorInfo = {
|
|
54
|
+
index: number;
|
|
55
|
+
message: string;
|
|
56
|
+
path: JsonPointer;
|
|
57
|
+
reason: 'path' | 'test';
|
|
58
|
+
};
|
|
59
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,+DAA+D;AAC/D,MAAM,MAAM,SAAS,GAAG,IAAI,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS,EAAE,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAAC;AAEtG,oDAAoD;AACpD,MAAM,MAAM,UAAU,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAAC;AAEtD,mEAAmE;AACnE,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC;AAEjC,MAAM,MAAM,YAAY,GAAG;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,IAAI,EAAE,WAAW,CAAC;IAAC,KAAK,EAAE,SAAS,CAAA;CAAE,CAAC;AAC9E,MAAM,MAAM,eAAe,GAAG;IAAE,EAAE,EAAE,QAAQ,CAAC;IAAC,IAAI,EAAE,WAAW,CAAA;CAAE,CAAC;AACl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
|
|
6
|
-
|
|
3
|
+
"version": "26.11.14",
|
|
4
|
+
"description": "RFC 6902 JSON Patch: structural diff, immutable apply, and guarded tryApply for sync protocols.",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/helmuthdu/vielzeug",
|
|
8
|
+
"directory": "packages/patchwork"
|
|
9
|
+
},
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"type": "module",
|
|
12
|
+
"engines": {
|
|
13
|
+
"node": ">=22"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"main": "./dist/index.cjs",
|
|
19
|
+
"module": "./dist/index.js",
|
|
20
|
+
"types": "dist/index.d.ts",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"import": "./dist/index.js",
|
|
24
|
+
"require": "./dist/index.cjs",
|
|
25
|
+
"types": "./dist/index.d.ts"
|
|
26
|
+
},
|
|
27
|
+
"./merge": {
|
|
28
|
+
"import": "./dist/merge.js",
|
|
29
|
+
"require": "./dist/merge.cjs",
|
|
30
|
+
"types": "./dist/merge.d.ts"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"typesVersions": {
|
|
34
|
+
"*": {
|
|
35
|
+
"merge": [
|
|
36
|
+
"dist/merge.d.ts"
|
|
37
|
+
]
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"sideEffects": false,
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public",
|
|
43
|
+
"registry": "https://registry.npmjs.org/"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "vite build && bun run --silent build:bundle && bun run --silent build:types",
|
|
47
|
+
"build:bundle": "vite build --config vite.bundle.config.ts",
|
|
48
|
+
"build:types": "tsc -p tsconfig.declarations.json",
|
|
49
|
+
"fix": "biome check --write src",
|
|
50
|
+
"lint": "biome ci src",
|
|
51
|
+
"prepublishOnly": "bun run --silent build",
|
|
52
|
+
"test": "vitest"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@types/node": "^26.6.6",
|
|
56
|
+
"typescript": "^6.0.3",
|
|
57
|
+
"vite": "^8.3.6",
|
|
58
|
+
"vitest": "^5.1.0"
|
|
59
|
+
}
|
|
60
|
+
}
|