@procedures/json 0.0.0-stage → 0.2.3
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 +104 -2
- package/dist/.tsbuildinfo +1 -0
- package/dist/diff.d.ts +18 -0
- package/dist/diff.d.ts.map +1 -0
- package/dist/diff.js +66 -0
- package/dist/diff.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/merge.d.ts +14 -0
- package/dist/merge.d.ts.map +1 -0
- package/dist/merge.js +34 -0
- package/dist/merge.js.map +1 -0
- package/dist/module.d.ts +21 -0
- package/dist/module.d.ts.map +1 -0
- package/dist/module.js +192 -0
- package/dist/module.js.map +1 -0
- package/dist/patch.d.ts +23 -0
- package/dist/patch.d.ts.map +1 -0
- package/dist/patch.js +137 -0
- package/dist/patch.js.map +1 -0
- package/dist/path.d.ts +38 -0
- package/dist/path.d.ts.map +1 -0
- package/dist/path.js +157 -0
- package/dist/path.js.map +1 -0
- package/dist/pointer.d.ts +22 -0
- package/dist/pointer.d.ts.map +1 -0
- package/dist/pointer.js +70 -0
- package/dist/pointer.js.map +1 -0
- package/dist/procedures/json/diff.d.ts +12 -0
- package/dist/procedures/json/diff.d.ts.map +1 -0
- package/dist/procedures/json/diff.js +17 -0
- package/dist/procedures/json/diff.js.map +1 -0
- package/dist/procedures/json/index.d.ts +6 -0
- package/dist/procedures/json/index.d.ts.map +1 -0
- package/dist/procedures/json/index.js +6 -0
- package/dist/procedures/json/index.js.map +1 -0
- package/dist/procedures/json/merge.d.ts +12 -0
- package/dist/procedures/json/merge.d.ts.map +1 -0
- package/dist/procedures/json/merge.js +17 -0
- package/dist/procedures/json/merge.js.map +1 -0
- package/dist/procedures/json/patch.d.ts +12 -0
- package/dist/procedures/json/patch.d.ts.map +1 -0
- package/dist/procedures/json/patch.js +20 -0
- package/dist/procedures/json/patch.js.map +1 -0
- package/dist/procedures/json/query.d.ts +12 -0
- package/dist/procedures/json/query.d.ts.map +1 -0
- package/dist/procedures/json/query.js +28 -0
- package/dist/procedures/json/query.js.map +1 -0
- package/dist/procedures/json/validate.d.ts +12 -0
- package/dist/procedures/json/validate.d.ts.map +1 -0
- package/dist/procedures/json/validate.js +18 -0
- package/dist/procedures/json/validate.js.map +1 -0
- package/dist/procedures/yaml/index.d.ts +3 -0
- package/dist/procedures/yaml/index.d.ts.map +1 -0
- package/dist/procedures/yaml/index.js +3 -0
- package/dist/procedures/yaml/index.js.map +1 -0
- package/dist/procedures/yaml/parse.d.ts +12 -0
- package/dist/procedures/yaml/parse.d.ts.map +1 -0
- package/dist/procedures/yaml/parse.js +14 -0
- package/dist/procedures/yaml/parse.js.map +1 -0
- package/dist/procedures/yaml/stringify.d.ts +11 -0
- package/dist/procedures/yaml/stringify.d.ts.map +1 -0
- package/dist/procedures/yaml/stringify.js +15 -0
- package/dist/procedures/yaml/stringify.js.map +1 -0
- package/dist/register.d.ts +6 -0
- package/dist/register.d.ts.map +1 -0
- package/dist/register.js +8 -0
- package/dist/register.js.map +1 -0
- package/dist/schema.d.ts +38 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +92 -0
- package/dist/schema.js.map +1 -0
- package/dist/types.d.ts +90 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +8 -0
- package/dist/types.js.map +1 -0
- package/dist/value.d.ts +61 -0
- package/dist/value.d.ts.map +1 -0
- package/dist/value.js +124 -0
- package/dist/value.js.map +1 -0
- package/dist/yaml.d.ts +35 -0
- package/dist/yaml.d.ts.map +1 -0
- package/dist/yaml.js +111 -0
- package/dist/yaml.js.map +1 -0
- package/package.json +81 -3
- package/src/diff.ts +58 -0
- package/src/index.ts +14 -0
- package/src/merge.ts +34 -0
- package/src/module.ts +236 -0
- package/src/patch.ts +132 -0
- package/src/path.ts +155 -0
- package/src/pointer.ts +70 -0
- package/src/procedures/json/diff.ts +19 -0
- package/src/procedures/json/index.ts +5 -0
- package/src/procedures/json/merge.ts +19 -0
- package/src/procedures/json/patch.ts +21 -0
- package/src/procedures/json/query.ts +29 -0
- package/src/procedures/json/validate.ts +20 -0
- package/src/procedures/yaml/index.ts +2 -0
- package/src/procedures/yaml/parse.ts +16 -0
- package/src/procedures/yaml/stringify.ts +17 -0
- package/src/register.ts +10 -0
- package/src/schema.ts +104 -0
- package/src/types.ts +127 -0
- package/src/value.ts +143 -0
- package/src/yaml.ts +128 -0
package/dist/diff.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The diff of two JSON values: a JSON Patch (RFC 6902) that changes the first value into the
|
|
3
|
+
* second value. `applyPatch(a, diffJson(a, b))` gives a value equal to `b`.
|
|
4
|
+
*
|
|
5
|
+
* - Two objects: a `remove` for each key that only the first has, a diff of each shared key, and
|
|
6
|
+
* an `add` for each key that only the second has.
|
|
7
|
+
* - Two arrays: the items of the shared start and end stay. The items between them get a diff
|
|
8
|
+
* for each shared position, then a `remove` or an `add` for each other item.
|
|
9
|
+
* - Other values: one `replace`.
|
|
10
|
+
*
|
|
11
|
+
* The patch never names `__proto__`, `constructor` or `prototype`, because `applyPatch` refuses
|
|
12
|
+
* them. When a difference is under such a key, the patch replaces the object that holds the key.
|
|
13
|
+
*/
|
|
14
|
+
import type { PatchOperation } from "./patch.js";
|
|
15
|
+
import { type JsonValue } from "./value.js";
|
|
16
|
+
/** This function gives a JSON Patch that changes `a` into `b`. Equal values give an empty patch. */
|
|
17
|
+
export declare function diffJson(a: JsonValue, b: JsonValue): PatchOperation[];
|
|
18
|
+
//# sourceMappingURL=diff.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"diff.d.ts","sourceRoot":"","sources":["../src/diff.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AACjD,OAAO,EAA+D,KAAK,SAAS,EAAE,MAAM,YAAY,CAAC;AAqCzG,oGAAoG;AACpG,wBAAgB,QAAQ,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,SAAS,GAAG,cAAc,EAAE,CAIrE"}
|
package/dist/diff.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The diff of two JSON values: a JSON Patch (RFC 6902) that changes the first value into the
|
|
3
|
+
* second value. `applyPatch(a, diffJson(a, b))` gives a value equal to `b`.
|
|
4
|
+
*
|
|
5
|
+
* - Two objects: a `remove` for each key that only the first has, a diff of each shared key, and
|
|
6
|
+
* an `add` for each key that only the second has.
|
|
7
|
+
* - Two arrays: the items of the shared start and end stay. The items between them get a diff
|
|
8
|
+
* for each shared position, then a `remove` or an `add` for each other item.
|
|
9
|
+
* - Other values: one `replace`.
|
|
10
|
+
*
|
|
11
|
+
* The patch never names `__proto__`, `constructor` or `prototype`, because `applyPatch` refuses
|
|
12
|
+
* them. When a difference is under such a key, the patch replaces the object that holds the key.
|
|
13
|
+
*/
|
|
14
|
+
import { cloneJson, formatPointer, hasOwn, jsonEqual, PROTOTYPE_KEYS } from "./value.js";
|
|
15
|
+
function diffAt(a, b, tokens, out) {
|
|
16
|
+
if (jsonEqual(a, b))
|
|
17
|
+
return;
|
|
18
|
+
const path = formatPointer(tokens);
|
|
19
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
20
|
+
diffArrays(a, b, tokens, out);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (a !== null && b !== null && typeof a === "object" && typeof b === "object" && !Array.isArray(a) && !Array.isArray(b)) {
|
|
24
|
+
const keysA = Object.keys(a);
|
|
25
|
+
const keysB = Object.keys(b);
|
|
26
|
+
const named = [...keysA, ...keysB].some((key) => PROTOTYPE_KEYS.has(key) && !(hasOwn(a, key) && hasOwn(b, key) && jsonEqual(a[key], b[key])));
|
|
27
|
+
if (!named) {
|
|
28
|
+
for (const key of keysA)
|
|
29
|
+
if (!hasOwn(b, key))
|
|
30
|
+
out.push({ op: "remove", path: formatPointer([...tokens, key]) });
|
|
31
|
+
for (const key of keysA)
|
|
32
|
+
if (hasOwn(b, key))
|
|
33
|
+
diffAt(a[key], b[key], [...tokens, key], out);
|
|
34
|
+
for (const key of keysB)
|
|
35
|
+
if (!hasOwn(a, key))
|
|
36
|
+
out.push({ op: "add", path: formatPointer([...tokens, key]), value: cloneJson(b[key]) });
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
out.push({ op: "replace", path, value: cloneJson(b) });
|
|
41
|
+
}
|
|
42
|
+
function diffArrays(a, b, tokens, out) {
|
|
43
|
+
let start = 0;
|
|
44
|
+
while (start < a.length && start < b.length && jsonEqual(a[start], b[start]))
|
|
45
|
+
start++;
|
|
46
|
+
let end = 0;
|
|
47
|
+
while (end < a.length - start && end < b.length - start && jsonEqual(a[a.length - 1 - end], b[b.length - 1 - end]))
|
|
48
|
+
end++;
|
|
49
|
+
const middleA = a.length - start - end;
|
|
50
|
+
const middleB = b.length - start - end;
|
|
51
|
+
const shared = Math.min(middleA, middleB);
|
|
52
|
+
for (let i = 0; i < shared; i++)
|
|
53
|
+
diffAt(a[start + i], b[start + i], [...tokens, start + i], out);
|
|
54
|
+
// The items after the shared positions: remove from the last one, so each index stays valid
|
|
55
|
+
for (let i = middleA - 1; i >= shared; i--)
|
|
56
|
+
out.push({ op: "remove", path: formatPointer([...tokens, start + i]) });
|
|
57
|
+
for (let i = shared; i < middleB; i++)
|
|
58
|
+
out.push({ op: "add", path: formatPointer([...tokens, start + i]), value: cloneJson(b[start + i]) });
|
|
59
|
+
}
|
|
60
|
+
/** This function gives a JSON Patch that changes `a` into `b`. Equal values give an empty patch. */
|
|
61
|
+
export function diffJson(a, b) {
|
|
62
|
+
const out = [];
|
|
63
|
+
diffAt(a, b, [], out);
|
|
64
|
+
return out;
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=diff.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AAE,CAAC,EAAE;QAAE,GAAG,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,aAAa,CAAC,CAAC,GAAG,MAAM,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAE,CAAC,EAAE,CAAC,CAAC;AAC/I,CAAC;AAED,oGAAoG;AACpG,MAAM,UAAU,QAAQ,CAAC,CAAY,EAAE,CAAY;IACjD,MAAM,GAAG,GAAqB,EAAE,CAAC;IACjC,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,GAAG,CAAC,CAAC;IACtB,OAAO,GAAG,CAAC;AACb,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export * from "./module.js";
|
|
2
|
+
export * from "./types.js";
|
|
3
|
+
export { jsonQuery, jsonPatch, jsonDiff, jsonMerge, jsonValidate } from "./procedures/json/index.js";
|
|
4
|
+
export { yamlParse, yamlStringify } from "./procedures/yaml/index.js";
|
|
5
|
+
export { assertJson, cloneJson, jsonEqual, JsonError, PROTOTYPE_KEYS, formatPointer, escapeToken, type JsonErrorCode } from "./value.js";
|
|
6
|
+
export { parsePointer, lookup, arrayIndex, type Lookup } from "./pointer.js";
|
|
7
|
+
export { parsePath, queryPath, type PathMatch, type PathStep } from "./path.js";
|
|
8
|
+
export { applyPatch } from "./patch.js";
|
|
9
|
+
export { diffJson } from "./diff.js";
|
|
10
|
+
export { mergePatch } from "./merge.js";
|
|
11
|
+
export { validateJson, NOT_EXAMINED, type ValidationIssue, type ValidationResult } from "./schema.js";
|
|
12
|
+
export { parseYaml, stringifyYaml, type YamlParseOptions, type YamlStringifyOptions } from "./yaml.js";
|
|
13
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,QAAQ,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AACrG,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAEtE,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,cAAc,EAAE,aAAa,EAAE,WAAW,EAAE,KAAK,aAAa,EAAE,MAAM,YAAY,CAAC;AACzI,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,KAAK,MAAM,EAAE,MAAM,cAAc,CAAC;AAC7E,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,KAAK,SAAS,EAAE,KAAK,QAAQ,EAAE,MAAM,WAAW,CAAC;AAChF,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AACrC,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,KAAK,eAAe,EAAE,KAAK,gBAAgB,EAAE,MAAM,aAAa,CAAC;AACtG,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,KAAK,gBAAgB,EAAE,KAAK,oBAAoB,EAAE,MAAM,WAAW,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// The root registers nothing: import "<package>/register", or give the module to createRegistry()
|
|
2
|
+
export * from "./module.js";
|
|
3
|
+
export * from "./types.js";
|
|
4
|
+
export { jsonQuery, jsonPatch, jsonDiff, jsonMerge, jsonValidate } from "./procedures/json/index.js";
|
|
5
|
+
export { yamlParse, yamlStringify } from "./procedures/yaml/index.js";
|
|
6
|
+
// The functions behind the procedures, for a program in the same process
|
|
7
|
+
export { assertJson, cloneJson, jsonEqual, JsonError, PROTOTYPE_KEYS, formatPointer, escapeToken } from "./value.js";
|
|
8
|
+
export { parsePointer, lookup, arrayIndex } from "./pointer.js";
|
|
9
|
+
export { parsePath, queryPath } from "./path.js";
|
|
10
|
+
export { applyPatch } from "./patch.js";
|
|
11
|
+
export { diffJson } from "./diff.js";
|
|
12
|
+
export { mergePatch } from "./merge.js";
|
|
13
|
+
export { validateJson, NOT_EXAMINED } from "./schema.js";
|
|
14
|
+
export { parseYaml, stringifyYaml } from "./yaml.js";
|
|
15
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,kGAAkG;AAClG,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,QAAQ,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AACrG,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AACtE,yEAAyE;AACzE,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,cAAc,EAAE,aAAa,EAAE,WAAW,EAAsB,MAAM,YAAY,CAAC;AACzI,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,UAAU,EAAe,MAAM,cAAc,CAAC;AAC7E,OAAO,EAAE,SAAS,EAAE,SAAS,EAAiC,MAAM,WAAW,CAAC;AAChF,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AACrC,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,EAAE,YAAY,EAAE,YAAY,EAA+C,MAAM,aAAa,CAAC;AACtG,OAAO,EAAE,SAAS,EAAE,aAAa,EAAoD,MAAM,WAAW,CAAC"}
|
package/dist/merge.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON Merge Patch (RFC 7386): a patch is a JSON value of the same form as the target.
|
|
3
|
+
*
|
|
4
|
+
* - A patch that is an object changes the target member by member. A member with the value null
|
|
5
|
+
* removes the key. Another member merges into the key.
|
|
6
|
+
* - A patch that is not an object replaces the target.
|
|
7
|
+
*
|
|
8
|
+
* The merge applies to a copy of the target. A key `__proto__`, `constructor` or `prototype` in
|
|
9
|
+
* an object of the patch is refused (roadmap R6).
|
|
10
|
+
*/
|
|
11
|
+
import { type JsonValue } from "./value.js";
|
|
12
|
+
/** This function applies a JSON merge patch to a copy of the target and gives the result. */
|
|
13
|
+
export declare function mergePatch(target: JsonValue, patch: JsonValue): JsonValue;
|
|
14
|
+
//# sourceMappingURL=merge.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"merge.d.ts","sourceRoot":"","sources":["../src/merge.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAmF,KAAK,SAAS,EAAE,MAAM,YAAY,CAAC;AAmB7H,6FAA6F;AAC7F,wBAAgB,UAAU,CAAC,MAAM,EAAE,SAAS,EAAE,KAAK,EAAE,SAAS,GAAG,SAAS,CAEzE"}
|
package/dist/merge.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON Merge Patch (RFC 7386): a patch is a JSON value of the same form as the target.
|
|
3
|
+
*
|
|
4
|
+
* - A patch that is an object changes the target member by member. A member with the value null
|
|
5
|
+
* removes the key. Another member merges into the key.
|
|
6
|
+
* - A patch that is not an object replaces the target.
|
|
7
|
+
*
|
|
8
|
+
* The merge applies to a copy of the target. A key `__proto__`, `constructor` or `prototype` in
|
|
9
|
+
* an object of the patch is refused (roadmap R6).
|
|
10
|
+
*/
|
|
11
|
+
import { cloneJson, formatPointer, hasOwn, isPlainObject, JsonError, PROTOTYPE_KEYS, put } from "./value.js";
|
|
12
|
+
function mergeAt(target, patch, tokens) {
|
|
13
|
+
if (!isPlainObject(patch))
|
|
14
|
+
return cloneJson(patch);
|
|
15
|
+
const result = isPlainObject(target) ? cloneJson(target) : {};
|
|
16
|
+
for (const key of Object.keys(patch)) {
|
|
17
|
+
if (PROTOTYPE_KEYS.has(key)) {
|
|
18
|
+
throw new JsonError("PROTOTYPE_KEY", `The merge patch has the key "${key}" at "${formatPointer([...tokens, key])}", which reaches a prototype`);
|
|
19
|
+
}
|
|
20
|
+
const value = patch[key];
|
|
21
|
+
if (value === null) {
|
|
22
|
+
delete result[key];
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
put(result, key, mergeAt(hasOwn(result, key) ? result[key] : undefined, value, [...tokens, key]));
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
/** This function applies a JSON merge patch to a copy of the target and gives the result. */
|
|
31
|
+
export function mergePatch(target, patch) {
|
|
32
|
+
return mergeAt(target, patch, []);
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=merge.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"merge.js","sourceRoot":"","sources":["../src/merge.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,EAAE,aAAa,EAAE,SAAS,EAAE,cAAc,EAAE,GAAG,EAAkB,MAAM,YAAY,CAAC;AAE7H,SAAS,OAAO,CAAC,MAA6B,EAAE,KAAgB,EAAE,MAAgB;IAChF,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC;QAAE,OAAO,SAAS,CAAC,KAAK,CAAC,CAAC;IACnD,MAAM,MAAM,GAA8B,aAAa,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,MAAmC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IACtH,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;QACrC,IAAI,cAAc,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;YAC5B,MAAM,IAAI,SAAS,CAAC,eAAe,EAAE,gCAAgC,GAAG,SAAS,aAAa,CAAC,CAAC,GAAG,MAAM,EAAE,GAAG,CAAC,CAAC,8BAA8B,CAAC,CAAC;QAClJ,CAAC;QACD,MAAM,KAAK,GAAG,KAAK,CAAC,GAAG,CAAc,CAAC;QACtC,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;YACnB,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC;QACrB,CAAC;aAAM,CAAC;YACN,GAAG,CAAC,MAAM,EAAE,GAAG,EAAE,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,EAAE,KAAK,EAAE,CAAC,GAAG,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC;QACpG,CAAC;IACH,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,6FAA6F;AAC7F,MAAM,UAAU,UAAU,CAAC,MAAiB,EAAE,KAAgB;IAC5D,OAAO,OAAO,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC;AACpC,CAAC"}
|
package/dist/module.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The procedures of @procedures/json as data. Importing this file registers nothing.
|
|
3
|
+
*
|
|
4
|
+
* Scaffold a procedure with: mark procedure new <name> --path packages/json
|
|
5
|
+
*
|
|
6
|
+
* Each procedure is pure (`PURE_EFFECTS`): its output comes from its input, and it reads and
|
|
7
|
+
* changes nothing else (roadmap R39).
|
|
8
|
+
*/
|
|
9
|
+
import { type ProcedureModule, type ProcedureRegistry } from "@procedures/client";
|
|
10
|
+
/**
|
|
11
|
+
* The module of this package, for `createRegistry()`: its procedures as data. Importing this
|
|
12
|
+
* file registers nothing.
|
|
13
|
+
*/
|
|
14
|
+
export declare const jsonModule: ProcedureModule;
|
|
15
|
+
export default jsonModule;
|
|
16
|
+
/**
|
|
17
|
+
* This function registers the procedures of @procedures/json in a registry. The default is the
|
|
18
|
+
* global registry.
|
|
19
|
+
*/
|
|
20
|
+
export declare function registerJsonProcedures(registry?: ProcedureRegistry): void;
|
|
21
|
+
//# sourceMappingURL=module.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"module.d.ts","sourceRoot":"","sources":["../src/module.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EAAmE,KAAK,eAAe,EAAE,KAAK,iBAAiB,EAA+B,MAAM,oBAAoB,CAAC;AA2MhL;;;GAGG;AACH,eAAO,MAAM,UAAU,EAAE,eAQvB,CAAC;eAEY,UAAU;AAEzB;;;GAGG;AACH,wBAAgB,sBAAsB,CAAC,QAAQ,GAAE,iBAAsC,GAAG,IAAI,CAE7F"}
|
package/dist/module.js
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The procedures of @procedures/json as data. Importing this file registers nothing.
|
|
3
|
+
*
|
|
4
|
+
* Scaffold a procedure with: mark procedure new <name> --path packages/json
|
|
5
|
+
*
|
|
6
|
+
* Each procedure is pure (`PURE_EFFECTS`): its output comes from its input, and it reads and
|
|
7
|
+
* changes nothing else (roadmap R39).
|
|
8
|
+
*/
|
|
9
|
+
import { z } from "zod";
|
|
10
|
+
import { defineModule, registerModules, PROCEDURE_REGISTRY, PURE_EFFECTS, createProcedure, zodAdapter } from "@procedures/client";
|
|
11
|
+
import { jsonQuery } from "./procedures/json/query.js";
|
|
12
|
+
import { jsonPatch } from "./procedures/json/patch.js";
|
|
13
|
+
import { jsonDiff } from "./procedures/json/diff.js";
|
|
14
|
+
import { jsonMerge } from "./procedures/json/merge.js";
|
|
15
|
+
import { jsonValidate } from "./procedures/json/validate.js";
|
|
16
|
+
import { yamlParse } from "./procedures/yaml/parse.js";
|
|
17
|
+
import { yamlStringify } from "./procedures/yaml/stringify.js";
|
|
18
|
+
// =============================================================================
|
|
19
|
+
// Schemas
|
|
20
|
+
// =============================================================================
|
|
21
|
+
/** A JSON value. The handler examines it, so a key `__proto__` of the data reaches the check. */
|
|
22
|
+
const json = (description) => z.unknown().describe(description);
|
|
23
|
+
const operation = z.object({
|
|
24
|
+
op: z.enum(["add", "remove", "replace", "move", "copy", "test"]).describe("The operation"),
|
|
25
|
+
path: z.string().describe("The JSON Pointer of the location"),
|
|
26
|
+
from: z.string().optional().describe("The JSON Pointer of the source of move and copy"),
|
|
27
|
+
value: json("The value of add, replace and test").optional(),
|
|
28
|
+
});
|
|
29
|
+
const JsonQueryInputSchema = z.object({
|
|
30
|
+
value: json("The JSON value to search"),
|
|
31
|
+
pointer: z.string().optional().describe("A JSON Pointer (RFC 6901), for example /items/0/name. Give pointer or path"),
|
|
32
|
+
path: z.string().optional().describe("A path query, for example items[*].name or $..id. Give pointer or path"),
|
|
33
|
+
});
|
|
34
|
+
const JsonQueryOutputSchema = z.object({
|
|
35
|
+
found: z.boolean().describe("True when one or more values match"),
|
|
36
|
+
value: json("The value of the first match").optional(),
|
|
37
|
+
matches: z.array(z.object({ pointer: z.string().describe("The JSON Pointer of the match"), value: json("The value of the match") })).describe("Each match, in document order"),
|
|
38
|
+
});
|
|
39
|
+
const JsonPatchInputSchema = z.object({
|
|
40
|
+
value: json("The JSON value to change"),
|
|
41
|
+
patch: z.array(operation).describe("The operations of the JSON Patch (RFC 6902), in order"),
|
|
42
|
+
});
|
|
43
|
+
const JsonPatchOutputSchema = z.object({ value: json("The value after the patch") });
|
|
44
|
+
const JsonDiffInputSchema = z.object({
|
|
45
|
+
from: json("The first JSON value"),
|
|
46
|
+
to: json("The second JSON value"),
|
|
47
|
+
});
|
|
48
|
+
const JsonDiffOutputSchema = z.object({
|
|
49
|
+
patch: z.array(operation).describe("The JSON Patch (RFC 6902) that changes from into to"),
|
|
50
|
+
});
|
|
51
|
+
const JsonMergeInputSchema = z.object({
|
|
52
|
+
target: json("The JSON value to change"),
|
|
53
|
+
patch: json("The merge patch (RFC 7386): an object of changes, where null removes a key"),
|
|
54
|
+
});
|
|
55
|
+
const JsonMergeOutputSchema = z.object({ value: json("The value after the merge") });
|
|
56
|
+
const JsonValidateInputSchema = z.object({
|
|
57
|
+
value: json("The JSON value to examine"),
|
|
58
|
+
schema: json("The JSON Schema: an object or a boolean"),
|
|
59
|
+
});
|
|
60
|
+
const JsonValidateOutputSchema = z.object({
|
|
61
|
+
valid: z.boolean().describe("True when the check found no problem"),
|
|
62
|
+
errors: z.array(z.object({ pointer: z.string().describe("The JSON Pointer of the part"), message: z.string().describe("The problem") })).describe("Each problem"),
|
|
63
|
+
unchecked: z.array(z.string()).describe("The keywords of the schema that the check does not examine. Empty: the verdict is complete"),
|
|
64
|
+
});
|
|
65
|
+
const YamlParseInputSchema = z.object({
|
|
66
|
+
text: z.string().describe("The YAML text"),
|
|
67
|
+
all: z.boolean().optional().describe("True: the text can have more than one document, and value is the list of documents"),
|
|
68
|
+
});
|
|
69
|
+
const YamlParseOutputSchema = z.object({ value: json("The JSON value of the text") });
|
|
70
|
+
const YamlStringifyInputSchema = z.object({
|
|
71
|
+
value: json("The JSON value to write"),
|
|
72
|
+
indent: z.number().int().min(1).max(8).optional().describe("The spaces of one level of indentation (default: 2)"),
|
|
73
|
+
lineWidth: z.number().int().min(0).optional().describe("The longest line before a fold. 0: no fold (default: 80)"),
|
|
74
|
+
sortKeys: z.boolean().optional().describe("True: the keys of each map in alphabetical order"),
|
|
75
|
+
});
|
|
76
|
+
const YamlStringifyOutputSchema = z.object({ text: z.string().describe("The YAML text") });
|
|
77
|
+
// =============================================================================
|
|
78
|
+
// Procedures
|
|
79
|
+
// =============================================================================
|
|
80
|
+
const jsonQueryProcedure = createProcedure()
|
|
81
|
+
.path(["json", "query"])
|
|
82
|
+
.input(zodAdapter(JsonQueryInputSchema))
|
|
83
|
+
.output(zodAdapter(JsonQueryOutputSchema))
|
|
84
|
+
.meta({
|
|
85
|
+
description: "Find the values at a JSON Pointer or a path in a JSON value",
|
|
86
|
+
args: [],
|
|
87
|
+
shorts: { pointer: "p" },
|
|
88
|
+
output: "json",
|
|
89
|
+
effects: PURE_EFFECTS,
|
|
90
|
+
})
|
|
91
|
+
.handler(async (input) => jsonQuery(input))
|
|
92
|
+
.build();
|
|
93
|
+
const jsonPatchProcedure = createProcedure()
|
|
94
|
+
.path(["json", "patch"])
|
|
95
|
+
.input(zodAdapter(JsonPatchInputSchema))
|
|
96
|
+
.output(zodAdapter(JsonPatchOutputSchema))
|
|
97
|
+
.meta({
|
|
98
|
+
description: "Apply an RFC 6902 JSON Patch to a JSON value",
|
|
99
|
+
args: [],
|
|
100
|
+
shorts: {},
|
|
101
|
+
output: "json",
|
|
102
|
+
effects: PURE_EFFECTS,
|
|
103
|
+
})
|
|
104
|
+
.handler(async (input) => jsonPatch(input))
|
|
105
|
+
.build();
|
|
106
|
+
const jsonDiffProcedure = createProcedure()
|
|
107
|
+
.path(["json", "diff"])
|
|
108
|
+
.input(zodAdapter(JsonDiffInputSchema))
|
|
109
|
+
.output(zodAdapter(JsonDiffOutputSchema))
|
|
110
|
+
.meta({
|
|
111
|
+
description: "Give the RFC 6902 JSON Patch that changes one JSON value into another",
|
|
112
|
+
args: [],
|
|
113
|
+
shorts: {},
|
|
114
|
+
output: "json",
|
|
115
|
+
effects: PURE_EFFECTS,
|
|
116
|
+
})
|
|
117
|
+
.handler(async (input) => jsonDiff(input))
|
|
118
|
+
.build();
|
|
119
|
+
const jsonMergeProcedure = createProcedure()
|
|
120
|
+
.path(["json", "merge"])
|
|
121
|
+
.input(zodAdapter(JsonMergeInputSchema))
|
|
122
|
+
.output(zodAdapter(JsonMergeOutputSchema))
|
|
123
|
+
.meta({
|
|
124
|
+
description: "Apply an RFC 7386 JSON merge patch to a JSON value",
|
|
125
|
+
args: [],
|
|
126
|
+
shorts: {},
|
|
127
|
+
output: "json",
|
|
128
|
+
effects: PURE_EFFECTS,
|
|
129
|
+
})
|
|
130
|
+
.handler(async (input) => jsonMerge(input))
|
|
131
|
+
.build();
|
|
132
|
+
const jsonValidateProcedure = createProcedure()
|
|
133
|
+
.path(["json", "validate"])
|
|
134
|
+
.input(zodAdapter(JsonValidateInputSchema))
|
|
135
|
+
.output(zodAdapter(JsonValidateOutputSchema))
|
|
136
|
+
.meta({
|
|
137
|
+
description: "Examine a JSON value against a JSON Schema",
|
|
138
|
+
args: [],
|
|
139
|
+
shorts: {},
|
|
140
|
+
output: "json",
|
|
141
|
+
effects: PURE_EFFECTS,
|
|
142
|
+
})
|
|
143
|
+
.handler(async (input) => jsonValidate(input))
|
|
144
|
+
.build();
|
|
145
|
+
const yamlParseProcedure = createProcedure()
|
|
146
|
+
.path(["yaml", "parse"])
|
|
147
|
+
.input(zodAdapter(YamlParseInputSchema))
|
|
148
|
+
.output(zodAdapter(YamlParseOutputSchema))
|
|
149
|
+
.meta({
|
|
150
|
+
description: "Parse YAML text into a JSON value",
|
|
151
|
+
args: ["text"],
|
|
152
|
+
shorts: { all: "a" },
|
|
153
|
+
output: "json",
|
|
154
|
+
effects: PURE_EFFECTS,
|
|
155
|
+
})
|
|
156
|
+
.handler(async (input) => yamlParse(input))
|
|
157
|
+
.build();
|
|
158
|
+
const yamlStringifyProcedure = createProcedure()
|
|
159
|
+
.path(["yaml", "stringify"])
|
|
160
|
+
.input(zodAdapter(YamlStringifyInputSchema))
|
|
161
|
+
.output(zodAdapter(YamlStringifyOutputSchema))
|
|
162
|
+
.meta({
|
|
163
|
+
description: "Write a JSON value as YAML text",
|
|
164
|
+
args: [],
|
|
165
|
+
shorts: { indent: "i", sortKeys: "s" },
|
|
166
|
+
output: "json",
|
|
167
|
+
effects: PURE_EFFECTS,
|
|
168
|
+
})
|
|
169
|
+
.handler(async (input) => yamlStringify(input))
|
|
170
|
+
.build();
|
|
171
|
+
/**
|
|
172
|
+
* The module of this package, for `createRegistry()`: its procedures as data. Importing this
|
|
173
|
+
* file registers nothing.
|
|
174
|
+
*/
|
|
175
|
+
export const jsonModule = defineModule("@procedures/json", [
|
|
176
|
+
jsonQueryProcedure,
|
|
177
|
+
jsonPatchProcedure,
|
|
178
|
+
jsonDiffProcedure,
|
|
179
|
+
jsonMergeProcedure,
|
|
180
|
+
jsonValidateProcedure,
|
|
181
|
+
yamlParseProcedure,
|
|
182
|
+
yamlStringifyProcedure,
|
|
183
|
+
]);
|
|
184
|
+
export default jsonModule;
|
|
185
|
+
/**
|
|
186
|
+
* This function registers the procedures of @procedures/json in a registry. The default is the
|
|
187
|
+
* global registry.
|
|
188
|
+
*/
|
|
189
|
+
export function registerJsonProcedures(registry = PROCEDURE_REGISTRY) {
|
|
190
|
+
registerModules(registry, [jsonModule]);
|
|
191
|
+
}
|
|
192
|
+
//# sourceMappingURL=module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"module.js","sourceRoot":"","sources":["../src/module.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,YAAY,EAAE,eAAe,EAAE,kBAAkB,EAAE,YAAY,EAAgD,eAAe,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChL,OAAO,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAC;AACvD,OAAO,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAC;AACvD,OAAO,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AACrD,OAAO,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAC;AACvD,OAAO,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAC7D,OAAO,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAC;AACvD,OAAO,EAAE,aAAa,EAAE,MAAM,gCAAgC,CAAC;AAkB/D,gFAAgF;AAChF,UAAU;AACV,gFAAgF;AAEhF,iGAA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AkB;IAClB,iBAAiB;IACjB,kBAAkB;IAClB,qBAAqB;IACrB,kBAAkB;IAClB,sBAAsB;CACvB,CAAC,CAAC;AAEH,eAAe,UAAU,CAAC;AAE1B;;;GAGG;AACH,MAAM,UAAU,sBAAsB,CAAC,QAAQ,GAAsB,kBAAkB;IACrF,eAAe,CAAC,QAAQ,EAAE,CAAC,UAAU,CAAC,CAAC,CAAC;AAC1C,CAAC"}
|
package/dist/patch.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON Patch (RFC 6902): apply a list of operations to a JSON value.
|
|
3
|
+
*
|
|
4
|
+
* The operations are `add`, `remove`, `replace`, `move`, `copy` and `test`. The patch applies to
|
|
5
|
+
* a copy of the value, and all operations apply or none: the first failure throws, and the input
|
|
6
|
+
* value does not change. A `path` or a `from` that names `__proto__`, `constructor` or
|
|
7
|
+
* `prototype` is refused (roadmap R6).
|
|
8
|
+
*/
|
|
9
|
+
import { type JsonValue } from "./value.js";
|
|
10
|
+
/** One operation of a JSON Patch. */
|
|
11
|
+
export interface PatchOperation {
|
|
12
|
+
op: "add" | "remove" | "replace" | "move" | "copy" | "test";
|
|
13
|
+
path: string;
|
|
14
|
+
from?: string | undefined;
|
|
15
|
+
value?: unknown;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* This function applies a JSON Patch to a copy of a value and gives the result. A bad operation,
|
|
19
|
+
* a location that does not exist and a failed `test` throw a `JsonError` that names the index of
|
|
20
|
+
* the operation.
|
|
21
|
+
*/
|
|
22
|
+
export declare function applyPatch(value: JsonValue, operations: readonly PatchOperation[]): JsonValue;
|
|
23
|
+
//# sourceMappingURL=patch.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"patch.d.ts","sourceRoot":"","sources":["../src/patch.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EAAgF,KAAK,SAAS,EAAE,MAAM,YAAY,CAAC;AAE1H,qCAAqC;AACrC,MAAM,WAAW,cAAc;IAC7B,EAAE,EAAE,KAAK,GAAG,QAAQ,GAAG,SAAS,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,CAAC;IAC5D,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC1B,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AA2DD;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,SAAS,cAAc,EAAE,GAAG,SAAS,CAiD7F"}
|
package/dist/patch.js
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON Patch (RFC 6902): apply a list of operations to a JSON value.
|
|
3
|
+
*
|
|
4
|
+
* The operations are `add`, `remove`, `replace`, `move`, `copy` and `test`. The patch applies to
|
|
5
|
+
* a copy of the value, and all operations apply or none: the first failure throws, and the input
|
|
6
|
+
* value does not change. A `path` or a `from` that names `__proto__`, `constructor` or
|
|
7
|
+
* `prototype` is refused (roadmap R6).
|
|
8
|
+
*/
|
|
9
|
+
import { arrayIndex, lookup, parsePointer } from "./pointer.js";
|
|
10
|
+
import { cloneJson, formatPointer, isPlainObject, jsonEqual, JsonError, put, typeName } from "./value.js";
|
|
11
|
+
/** The parent of a location: the container and the last token. */
|
|
12
|
+
function parentOf(document, tokens, fail) {
|
|
13
|
+
const parentTokens = tokens.slice(0, -1);
|
|
14
|
+
const found = lookup(document, parentTokens);
|
|
15
|
+
if (!found.found)
|
|
16
|
+
fail(`the location ${formatPointer(parentTokens)} does not exist`);
|
|
17
|
+
const container = found.value;
|
|
18
|
+
if (container === null || typeof container !== "object")
|
|
19
|
+
fail(`the location ${formatPointer(parentTokens)} is ${typeName(container)}, not an object or an array`);
|
|
20
|
+
return { container: container, token: tokens[tokens.length - 1] };
|
|
21
|
+
}
|
|
22
|
+
function add(document, tokens, value, fail) {
|
|
23
|
+
if (tokens.length === 0)
|
|
24
|
+
return value;
|
|
25
|
+
const { container, token } = parentOf(document, tokens, fail);
|
|
26
|
+
if (Array.isArray(container)) {
|
|
27
|
+
if (token === "-") {
|
|
28
|
+
container.push(value);
|
|
29
|
+
return document;
|
|
30
|
+
}
|
|
31
|
+
const index = arrayIndex(token);
|
|
32
|
+
if (index === undefined || index > container.length)
|
|
33
|
+
fail(`the array index "${token}" is not 0 to ${container.length} or "-"`);
|
|
34
|
+
container.splice(index, 0, value);
|
|
35
|
+
return document;
|
|
36
|
+
}
|
|
37
|
+
put(container, token, value);
|
|
38
|
+
return document;
|
|
39
|
+
}
|
|
40
|
+
function remove(document, tokens, fail) {
|
|
41
|
+
if (tokens.length === 0)
|
|
42
|
+
fail("a patch cannot remove the whole value");
|
|
43
|
+
const { container, token } = parentOf(document, tokens, fail);
|
|
44
|
+
if (Array.isArray(container)) {
|
|
45
|
+
const index = arrayIndex(token);
|
|
46
|
+
if (index === undefined || index >= container.length)
|
|
47
|
+
fail(`the array index "${token}" does not exist`);
|
|
48
|
+
const [removed] = container.splice(index, 1);
|
|
49
|
+
return { document, removed: removed };
|
|
50
|
+
}
|
|
51
|
+
if (!Object.prototype.hasOwnProperty.call(container, token))
|
|
52
|
+
fail(`the location ${formatPointer(tokens)} does not exist`);
|
|
53
|
+
const removed = container[token];
|
|
54
|
+
delete container[token];
|
|
55
|
+
return { document, removed };
|
|
56
|
+
}
|
|
57
|
+
function replace(document, tokens, value, fail) {
|
|
58
|
+
if (tokens.length === 0)
|
|
59
|
+
return value;
|
|
60
|
+
if (!lookup(document, tokens).found)
|
|
61
|
+
fail(`the location ${formatPointer(tokens)} does not exist`);
|
|
62
|
+
const { container, token } = parentOf(document, tokens, fail);
|
|
63
|
+
if (Array.isArray(container))
|
|
64
|
+
container[arrayIndex(token)] = value;
|
|
65
|
+
else
|
|
66
|
+
put(container, token, value);
|
|
67
|
+
return document;
|
|
68
|
+
}
|
|
69
|
+
/** The value of an operation: present, and JSON. */
|
|
70
|
+
function valueOf(operation, fail) {
|
|
71
|
+
if (!("value" in operation) || operation.value === undefined)
|
|
72
|
+
fail(`the operation ${operation.op} needs a value`);
|
|
73
|
+
return cloneJson(operation.value);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* This function applies a JSON Patch to a copy of a value and gives the result. A bad operation,
|
|
77
|
+
* a location that does not exist and a failed `test` throw a `JsonError` that names the index of
|
|
78
|
+
* the operation.
|
|
79
|
+
*/
|
|
80
|
+
export function applyPatch(value, operations) {
|
|
81
|
+
let document = cloneJson(value);
|
|
82
|
+
operations.forEach((operation, index) => {
|
|
83
|
+
const fail = (message, code = "INVALID_PATCH") => {
|
|
84
|
+
throw new JsonError(code, `Operation ${index} (${operation.op} ${operation.path}): ${message}`);
|
|
85
|
+
};
|
|
86
|
+
if (!isPlainObject(operation) || typeof operation.path !== "string")
|
|
87
|
+
fail("an operation is an object with op and path");
|
|
88
|
+
const tokens = parsePointer(operation.path, `path of operation ${index}`);
|
|
89
|
+
const fromTokens = () => {
|
|
90
|
+
if (typeof operation.from !== "string")
|
|
91
|
+
fail(`the operation ${operation.op} needs from`);
|
|
92
|
+
return parsePointer(operation.from, `from of operation ${index}`);
|
|
93
|
+
};
|
|
94
|
+
switch (operation.op) {
|
|
95
|
+
case "add":
|
|
96
|
+
document = add(document, tokens, valueOf(operation, fail), fail);
|
|
97
|
+
break;
|
|
98
|
+
case "remove":
|
|
99
|
+
document = remove(document, tokens, fail).document;
|
|
100
|
+
break;
|
|
101
|
+
case "replace":
|
|
102
|
+
document = replace(document, tokens, valueOf(operation, fail), fail);
|
|
103
|
+
break;
|
|
104
|
+
case "move": {
|
|
105
|
+
const from = fromTokens();
|
|
106
|
+
if (from.length === tokens.length && from.every((token, at) => token === tokens[at]))
|
|
107
|
+
break;
|
|
108
|
+
if (from.length < tokens.length && from.every((token, at) => token === tokens[at]))
|
|
109
|
+
fail("a value cannot move into itself");
|
|
110
|
+
const removed = remove(document, from, fail);
|
|
111
|
+
document = add(removed.document, tokens, removed.removed, fail);
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
case "copy": {
|
|
115
|
+
const from = fromTokens();
|
|
116
|
+
const found = lookup(document, from);
|
|
117
|
+
if (!found.found)
|
|
118
|
+
fail(`the location ${formatPointer(from)} does not exist`);
|
|
119
|
+
document = add(document, tokens, cloneJson(found.value), fail);
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
case "test": {
|
|
123
|
+
const expected = valueOf(operation, fail);
|
|
124
|
+
const found = lookup(document, tokens);
|
|
125
|
+
if (!found.found)
|
|
126
|
+
fail(`the location ${operation.path} does not exist`, "TEST_FAILED");
|
|
127
|
+
if (!jsonEqual(found.value, expected))
|
|
128
|
+
fail(`the value is not ${JSON.stringify(expected)}`, "TEST_FAILED");
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
default:
|
|
132
|
+
fail(`the operation ${JSON.stringify(operation.op)} is not add, remove, replace, move, copy or test`);
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
return document;
|
|
136
|
+
}
|
|
137
|
+
//# sourceMappingURL=patch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"patch.js","sourceRoot":"","sources":["../src/patch.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAChE,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,aAAa,EAAE,SAAS,EAAE,SAAS,EAAE,GAAG,EAAE,QAAQ,EAAkB,MAAM,YAAY,CAAC;AAU1H,kEAAkE;AAClE,SAAS,QAAQ,CAAC,QAAmB,EAAE,MAAyB,EAAE,IAAgC;IAChG,MAAM,YAAY,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;IACzC,MAAM,KAAK,GAAG,MAAM,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC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