@stryke/json 0.9.36 → 0.9.37
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/CHANGELOG.md +7 -0
- package/dist/_virtual/rolldown_runtime.cjs +29 -1
- package/dist/index.cjs +16 -1
- package/dist/index.mjs +9 -1
- package/dist/pointer/find-reference.cjs +66 -1
- package/dist/pointer/find-reference.mjs +63 -1
- package/dist/pointer/find-reference.mjs.map +1 -1
- package/dist/pointer/index.cjs +15 -1
- package/dist/pointer/index.mjs +4 -1
- package/dist/pointer/parse.cjs +87 -1
- package/dist/pointer/parse.mjs +80 -1
- package/dist/pointer/parse.mjs.map +1 -1
- package/dist/storm-json.cjs +117 -1
- package/dist/storm-json.mjs +115 -1
- package/dist/storm-json.mjs.map +1 -1
- package/dist/type-checks/src/get-object-tag.cjs +15 -1
- package/dist/type-checks/src/get-object-tag.mjs +14 -1
- package/dist/type-checks/src/get-object-tag.mjs.map +1 -1
- package/dist/type-checks/src/is-empty.cjs +20 -1
- package/dist/type-checks/src/is-empty.mjs +20 -1
- package/dist/type-checks/src/is-empty.mjs.map +1 -1
- package/dist/type-checks/src/is-non-null-object.cjs +16 -1
- package/dist/type-checks/src/is-non-null-object.mjs +16 -1
- package/dist/type-checks/src/is-non-null-object.mjs.map +1 -1
- package/dist/type-checks/src/is-null.cjs +12 -1
- package/dist/type-checks/src/is-null.mjs +11 -1
- package/dist/type-checks/src/is-null.mjs.map +1 -1
- package/dist/type-checks/src/is-number.cjs +18 -1
- package/dist/type-checks/src/is-number.mjs +17 -1
- package/dist/type-checks/src/is-number.mjs.map +1 -1
- package/dist/type-checks/src/is-object.cjs +19 -1
- package/dist/type-checks/src/is-object.mjs +19 -1
- package/dist/type-checks/src/is-object.mjs.map +1 -1
- package/dist/type-checks/src/is-plain-object.cjs +63 -1
- package/dist/type-checks/src/is-plain-object.mjs +63 -1
- package/dist/type-checks/src/is-plain-object.mjs.map +1 -1
- package/dist/type-checks/src/is-set-object.cjs +19 -1
- package/dist/type-checks/src/is-set-object.mjs +19 -1
- package/dist/type-checks/src/is-set-object.mjs.map +1 -1
- package/dist/type-checks/src/is-set.cjs +19 -1
- package/dist/type-checks/src/is-set.mjs +19 -1
- package/dist/type-checks/src/is-set.mjs.map +1 -1
- package/dist/type-checks/src/is-string.cjs +12 -1
- package/dist/type-checks/src/is-string.mjs +11 -1
- package/dist/type-checks/src/is-string.mjs.map +1 -1
- package/dist/type-checks/src/is-undefined.cjs +8 -1
- package/dist/type-checks/src/is-undefined.mjs +7 -1
- package/dist/type-checks/src/is-undefined.mjs.map +1 -1
- package/dist/types/src/base.cjs +6 -1
- package/dist/types/src/base.mjs +5 -1
- package/dist/types/src/base.mjs.map +1 -1
- package/dist/types.mjs +1 -1
- package/dist/utils/code-frames.cjs +77 -3
- package/dist/utils/code-frames.mjs +76 -3
- package/dist/utils/code-frames.mjs.map +1 -1
- package/dist/utils/index.cjs +13 -1
- package/dist/utils/index.mjs +7 -1
- package/dist/utils/parse-error.cjs +34 -1
- package/dist/utils/parse-error.mjs +33 -1
- package/dist/utils/parse-error.mjs.map +1 -1
- package/dist/utils/parse.cjs +52 -1
- package/dist/utils/parse.mjs +51 -1
- package/dist/utils/parse.mjs.map +1 -1
- package/dist/utils/stringify.cjs +68 -1
- package/dist/utils/stringify.mjs +67 -1
- package/dist/utils/stringify.mjs.map +1 -1
- package/dist/utils/strip-comments.cjs +86 -4
- package/dist/utils/strip-comments.mjs +86 -4
- package/dist/utils/strip-comments.mjs.map +1 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
# Changelog for Stryke - JSON
|
|
4
4
|
|
|
5
|
+
## [0.9.36](https://github.com/storm-software/stryke/releases/tag/json%400.9.36) (01/16/2026)
|
|
6
|
+
|
|
7
|
+
### Updated Dependencies
|
|
8
|
+
|
|
9
|
+
- Updated **type-checks** to **v0.5.18**
|
|
10
|
+
- Updated **types** to **v0.10.32**
|
|
11
|
+
|
|
5
12
|
## [0.9.35](https://github.com/storm-software/stryke/releases/tag/json%400.9.35) (01/15/2026)
|
|
6
13
|
|
|
7
14
|
### Updated Dependencies
|
|
@@ -1 +1,29 @@
|
|
|
1
|
-
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
13
|
+
__defProp(to, key, {
|
|
14
|
+
get: ((k) => from[k]).bind(null, key),
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
23
|
+
value: mod,
|
|
24
|
+
enumerable: true
|
|
25
|
+
}) : target, mod));
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
|
|
29
|
+
exports.__toESM = __toESM;
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1,16 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_utils_strip_comments = require('./utils/strip-comments.cjs');
|
|
2
|
+
const require_utils_parse = require('./utils/parse.cjs');
|
|
3
|
+
const require_utils_code_frames = require('./utils/code-frames.cjs');
|
|
4
|
+
const require_utils_parse_error = require('./utils/parse-error.cjs');
|
|
5
|
+
const require_utils_stringify = require('./utils/stringify.cjs');
|
|
6
|
+
const require_storm_json = require('./storm-json.cjs');
|
|
7
|
+
require('./utils/index.cjs');
|
|
8
|
+
|
|
9
|
+
exports.StormJSON = require_storm_json.StormJSON;
|
|
10
|
+
exports.codeFrameColumns = require_utils_code_frames.codeFrameColumns;
|
|
11
|
+
exports.formatParseError = require_utils_parse_error.formatParseError;
|
|
12
|
+
exports.invalidKeyChars = require_utils_stringify.invalidKeyChars;
|
|
13
|
+
exports.parse = require_utils_parse.parse;
|
|
14
|
+
exports.safeParse = require_utils_parse.safeParse;
|
|
15
|
+
exports.stringify = require_utils_stringify.stringify;
|
|
16
|
+
exports.stripComments = require_utils_strip_comments.stripComments;
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1,9 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { stripComments } from "./utils/strip-comments.mjs";
|
|
2
|
+
import { parse, safeParse } from "./utils/parse.mjs";
|
|
3
|
+
import { codeFrameColumns } from "./utils/code-frames.mjs";
|
|
4
|
+
import { formatParseError } from "./utils/parse-error.mjs";
|
|
5
|
+
import { invalidKeyChars, stringify } from "./utils/stringify.mjs";
|
|
6
|
+
import { StormJSON } from "./storm-json.mjs";
|
|
7
|
+
import "./utils/index.mjs";
|
|
8
|
+
|
|
9
|
+
export { StormJSON, codeFrameColumns, formatParseError, invalidKeyChars, parse, safeParse, stringify, stripComments };
|
|
@@ -1 +1,66 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_is_string = require('../type-checks/src/is-string.cjs');
|
|
2
|
+
const require_is_set_object = require('../type-checks/src/is-set-object.cjs');
|
|
3
|
+
|
|
4
|
+
//#region src/pointer/find-reference.ts
|
|
5
|
+
const { isArray } = Array;
|
|
6
|
+
/**
|
|
7
|
+
* Finds a target in document specified by JSON Pointer. Also returns the
|
|
8
|
+
* object containing the target and key used to reference that object.
|
|
9
|
+
*
|
|
10
|
+
* Throws Error('NOT_FOUND') if pointer does not result into a value in the middle
|
|
11
|
+
* of the path. If the last element of the path does not result into a value, the
|
|
12
|
+
* lookup succeeds with `val` set to `undefined`. It can be used to discriminate
|
|
13
|
+
* missing values, because `undefined` is not a valid JSON value.
|
|
14
|
+
*
|
|
15
|
+
* If last element in array is targeted using "-", e.g. "/arr/-", use
|
|
16
|
+
* `isArrayEnd` to verify that:
|
|
17
|
+
*
|
|
18
|
+
* ```js
|
|
19
|
+
* const ref = find({arr: [1, 2, 3], ['arr', '-']});
|
|
20
|
+
* if (isArrayReference(ref)) {
|
|
21
|
+
* if (isArrayEnd(ref)) {
|
|
22
|
+
* // ...
|
|
23
|
+
* }
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @param val - Document to search in.
|
|
28
|
+
* @param path - JSON Pointer path.
|
|
29
|
+
* @returns Reference to the target.
|
|
30
|
+
*/
|
|
31
|
+
const find = (val, path) => {
|
|
32
|
+
const pathLength = path.length;
|
|
33
|
+
if (!pathLength) return { val };
|
|
34
|
+
let obj;
|
|
35
|
+
let key;
|
|
36
|
+
for (let i = 0; i < pathLength; i++) {
|
|
37
|
+
obj = val;
|
|
38
|
+
key = path[i];
|
|
39
|
+
if (isArray(obj)) {
|
|
40
|
+
const length = obj.length;
|
|
41
|
+
if (key === "-") key = length;
|
|
42
|
+
else if (require_is_string.isString(key)) {
|
|
43
|
+
const key2 = Math.trunc(Number.parseInt(key));
|
|
44
|
+
if (String(key2) !== key) throw new Error("INVALID_INDEX");
|
|
45
|
+
key = key2;
|
|
46
|
+
if (key < 0) throw new Error("INVALID_INDEX");
|
|
47
|
+
}
|
|
48
|
+
if (key) val = obj[key];
|
|
49
|
+
} else if (require_is_set_object.isSetObject(obj)) val = key && key in obj ? obj[key] : void 0;
|
|
50
|
+
else throw new Error("NOT_FOUND");
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
val,
|
|
54
|
+
obj,
|
|
55
|
+
key
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
const isArrayReference = (ref) => isArray(ref.obj) && typeof ref.key === "number";
|
|
59
|
+
const isArrayEnd = (ref) => ref.obj.length === ref.key;
|
|
60
|
+
const isObjectReference = (ref) => typeof ref.obj === "object" && typeof ref.key === "string";
|
|
61
|
+
|
|
62
|
+
//#endregion
|
|
63
|
+
exports.find = find;
|
|
64
|
+
exports.isArrayEnd = isArrayEnd;
|
|
65
|
+
exports.isArrayReference = isArrayReference;
|
|
66
|
+
exports.isObjectReference = isObjectReference;
|
|
@@ -1,2 +1,64 @@
|
|
|
1
|
-
import{isString
|
|
1
|
+
import { isString } from "../type-checks/src/is-string.mjs";
|
|
2
|
+
import { isSetObject } from "../type-checks/src/is-set-object.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/pointer/find-reference.ts
|
|
5
|
+
const { isArray } = Array;
|
|
6
|
+
/**
|
|
7
|
+
* Finds a target in document specified by JSON Pointer. Also returns the
|
|
8
|
+
* object containing the target and key used to reference that object.
|
|
9
|
+
*
|
|
10
|
+
* Throws Error('NOT_FOUND') if pointer does not result into a value in the middle
|
|
11
|
+
* of the path. If the last element of the path does not result into a value, the
|
|
12
|
+
* lookup succeeds with `val` set to `undefined`. It can be used to discriminate
|
|
13
|
+
* missing values, because `undefined` is not a valid JSON value.
|
|
14
|
+
*
|
|
15
|
+
* If last element in array is targeted using "-", e.g. "/arr/-", use
|
|
16
|
+
* `isArrayEnd` to verify that:
|
|
17
|
+
*
|
|
18
|
+
* ```js
|
|
19
|
+
* const ref = find({arr: [1, 2, 3], ['arr', '-']});
|
|
20
|
+
* if (isArrayReference(ref)) {
|
|
21
|
+
* if (isArrayEnd(ref)) {
|
|
22
|
+
* // ...
|
|
23
|
+
* }
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @param val - Document to search in.
|
|
28
|
+
* @param path - JSON Pointer path.
|
|
29
|
+
* @returns Reference to the target.
|
|
30
|
+
*/
|
|
31
|
+
const find = (val, path) => {
|
|
32
|
+
const pathLength = path.length;
|
|
33
|
+
if (!pathLength) return { val };
|
|
34
|
+
let obj;
|
|
35
|
+
let key;
|
|
36
|
+
for (let i = 0; i < pathLength; i++) {
|
|
37
|
+
obj = val;
|
|
38
|
+
key = path[i];
|
|
39
|
+
if (isArray(obj)) {
|
|
40
|
+
const length = obj.length;
|
|
41
|
+
if (key === "-") key = length;
|
|
42
|
+
else if (isString(key)) {
|
|
43
|
+
const key2 = Math.trunc(Number.parseInt(key));
|
|
44
|
+
if (String(key2) !== key) throw new Error("INVALID_INDEX");
|
|
45
|
+
key = key2;
|
|
46
|
+
if (key < 0) throw new Error("INVALID_INDEX");
|
|
47
|
+
}
|
|
48
|
+
if (key) val = obj[key];
|
|
49
|
+
} else if (isSetObject(obj)) val = key && key in obj ? obj[key] : void 0;
|
|
50
|
+
else throw new Error("NOT_FOUND");
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
val,
|
|
54
|
+
obj,
|
|
55
|
+
key
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
const isArrayReference = (ref) => isArray(ref.obj) && typeof ref.key === "number";
|
|
59
|
+
const isArrayEnd = (ref) => ref.obj.length === ref.key;
|
|
60
|
+
const isObjectReference = (ref) => typeof ref.obj === "object" && typeof ref.key === "string";
|
|
61
|
+
|
|
62
|
+
//#endregion
|
|
63
|
+
export { find, isArrayEnd, isArrayReference, isObjectReference };
|
|
2
64
|
//# sourceMappingURL=find-reference.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"find-reference.mjs","names":["obj: Reference[\"obj\"]","key: Reference[\"key\"]"],"sources":["../../src/pointer/find-reference.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { isSetObject } from \"@stryke/type-checks/is-set-object\";\nimport { isString } from \"@stryke/type-checks/is-string\";\n\nexport interface Reference {\n /** Target value where pointer is pointing. */\n readonly val: unknown;\n /** Object which contains the target value. */\n readonly obj?: unknown | object | unknown[];\n /** Key which targets the target value in the object. */\n readonly key?: string | number;\n}\n\nconst { isArray } = Array;\n\n/**\n * Finds a target in document specified by JSON Pointer. Also returns the\n * object containing the target and key used to reference that object.\n *\n * Throws Error('NOT_FOUND') if pointer does not result into a value in the middle\n * of the path. If the last element of the path does not result into a value, the\n * lookup succeeds with `val` set to `undefined`. It can be used to discriminate\n * missing values, because `undefined` is not a valid JSON value.\n *\n * If last element in array is targeted using \"-\", e.g. \"/arr/-\", use\n * `isArrayEnd` to verify that:\n *\n * ```js\n * const ref = find({arr: [1, 2, 3], ['arr', '-']});\n * if (isArrayReference(ref)) {\n * if (isArrayEnd(ref)) {\n * // ...\n * }\n * }\n * ```\n *\n * @param val - Document to search in.\n * @param path - JSON Pointer path.\n * @returns Reference to the target.\n */\nexport const find = (val: unknown, path: Reference[\"key\"][]): Reference => {\n const pathLength = path.length;\n if (!pathLength) {\n return { val };\n }\n\n let obj: Reference[\"obj\"];\n let key: Reference[\"key\"];\n for (let i = 0; i < pathLength; i++) {\n obj = val;\n key = path[i];\n if (isArray(obj)) {\n const length = obj.length;\n if (key === \"-\") {\n key = length;\n } else if (isString(key)) {\n const key2 = Math.trunc(Number.parseInt(key));\n if (String(key2) !== key) {\n throw new Error(\"INVALID_INDEX\");\n }\n\n key = key2;\n if (key < 0) {\n throw new Error(\"INVALID_INDEX\");\n }\n }\n\n if (key) {\n val = obj[key];\n }\n } else if (isSetObject(obj)) {\n val = key && key in obj ? (obj as any)[key] : undefined;\n } else throw new Error(\"NOT_FOUND\");\n }\n const ref: Reference = {\n val,\n obj,\n key\n };\n\n return ref;\n};\n\nexport interface ArrayReference<T = unknown> {\n /** `undefined` in case JSON Pointer points to last element, e.g. \"/foo/-\". */\n readonly val: undefined | T;\n readonly obj: T[];\n readonly key: number;\n}\n\nexport const isArrayReference = <T = unknown>(\n ref: Reference\n): ref is ArrayReference<T> => isArray(ref.obj) && typeof ref.key === \"number\";\n\nexport const isArrayEnd = (ref: ArrayReference): boolean =>\n ref.obj.length === ref.key;\n\nexport interface ObjectReference<T = unknown> {\n readonly val: T;\n readonly obj: Record<string, T>;\n readonly key: string;\n}\n\nexport const isObjectReference = <T = unknown>(\n ref: Reference\n): ref is ObjectReference<T> =>\n typeof ref.obj === \"object\" && typeof ref.key === \"string\";\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"find-reference.mjs","names":["obj: Reference[\"obj\"]","key: Reference[\"key\"]"],"sources":["../../src/pointer/find-reference.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { isSetObject } from \"@stryke/type-checks/is-set-object\";\nimport { isString } from \"@stryke/type-checks/is-string\";\n\nexport interface Reference {\n /** Target value where pointer is pointing. */\n readonly val: unknown;\n /** Object which contains the target value. */\n readonly obj?: unknown | object | unknown[];\n /** Key which targets the target value in the object. */\n readonly key?: string | number;\n}\n\nconst { isArray } = Array;\n\n/**\n * Finds a target in document specified by JSON Pointer. Also returns the\n * object containing the target and key used to reference that object.\n *\n * Throws Error('NOT_FOUND') if pointer does not result into a value in the middle\n * of the path. If the last element of the path does not result into a value, the\n * lookup succeeds with `val` set to `undefined`. It can be used to discriminate\n * missing values, because `undefined` is not a valid JSON value.\n *\n * If last element in array is targeted using \"-\", e.g. \"/arr/-\", use\n * `isArrayEnd` to verify that:\n *\n * ```js\n * const ref = find({arr: [1, 2, 3], ['arr', '-']});\n * if (isArrayReference(ref)) {\n * if (isArrayEnd(ref)) {\n * // ...\n * }\n * }\n * ```\n *\n * @param val - Document to search in.\n * @param path - JSON Pointer path.\n * @returns Reference to the target.\n */\nexport const find = (val: unknown, path: Reference[\"key\"][]): Reference => {\n const pathLength = path.length;\n if (!pathLength) {\n return { val };\n }\n\n let obj: Reference[\"obj\"];\n let key: Reference[\"key\"];\n for (let i = 0; i < pathLength; i++) {\n obj = val;\n key = path[i];\n if (isArray(obj)) {\n const length = obj.length;\n if (key === \"-\") {\n key = length;\n } else if (isString(key)) {\n const key2 = Math.trunc(Number.parseInt(key));\n if (String(key2) !== key) {\n throw new Error(\"INVALID_INDEX\");\n }\n\n key = key2;\n if (key < 0) {\n throw new Error(\"INVALID_INDEX\");\n }\n }\n\n if (key) {\n val = obj[key];\n }\n } else if (isSetObject(obj)) {\n val = key && key in obj ? (obj as any)[key] : undefined;\n } else throw new Error(\"NOT_FOUND\");\n }\n const ref: Reference = {\n val,\n obj,\n key\n };\n\n return ref;\n};\n\nexport interface ArrayReference<T = unknown> {\n /** `undefined` in case JSON Pointer points to last element, e.g. \"/foo/-\". */\n readonly val: undefined | T;\n readonly obj: T[];\n readonly key: number;\n}\n\nexport const isArrayReference = <T = unknown>(\n ref: Reference\n): ref is ArrayReference<T> => isArray(ref.obj) && typeof ref.key === \"number\";\n\nexport const isArrayEnd = (ref: ArrayReference): boolean =>\n ref.obj.length === ref.key;\n\nexport interface ObjectReference<T = unknown> {\n readonly val: T;\n readonly obj: Record<string, T>;\n readonly key: string;\n}\n\nexport const isObjectReference = <T = unknown>(\n ref: Reference\n): ref is ObjectReference<T> =>\n typeof ref.obj === \"object\" && typeof ref.key === \"string\";\n"],"mappings":";;;;AA8BA,MAAM,EAAE,YAAY;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BpB,MAAa,QAAQ,KAAc,SAAwC;CACzE,MAAM,aAAa,KAAK;AACxB,KAAI,CAAC,WACH,QAAO,EAAE,KAAK;CAGhB,IAAIA;CACJ,IAAIC;AACJ,MAAK,IAAI,IAAI,GAAG,IAAI,YAAY,KAAK;AACnC,QAAM;AACN,QAAM,KAAK;AACX,MAAI,QAAQ,IAAI,EAAE;GAChB,MAAM,SAAS,IAAI;AACnB,OAAI,QAAQ,IACV,OAAM;YACG,SAAS,IAAI,EAAE;IACxB,MAAM,OAAO,KAAK,MAAM,OAAO,SAAS,IAAI,CAAC;AAC7C,QAAI,OAAO,KAAK,KAAK,IACnB,OAAM,IAAI,MAAM,gBAAgB;AAGlC,UAAM;AACN,QAAI,MAAM,EACR,OAAM,IAAI,MAAM,gBAAgB;;AAIpC,OAAI,IACF,OAAM,IAAI;aAEH,YAAY,IAAI,CACzB,OAAM,OAAO,OAAO,MAAO,IAAY,OAAO;MACzC,OAAM,IAAI,MAAM,YAAY;;AAQrC,QANuB;EACrB;EACA;EACA;EACD;;AAYH,MAAa,oBACX,QAC6B,QAAQ,IAAI,IAAI,IAAI,OAAO,IAAI,QAAQ;AAEtE,MAAa,cAAc,QACzB,IAAI,IAAI,WAAW,IAAI;AAQzB,MAAa,qBACX,QAEA,OAAO,IAAI,QAAQ,YAAY,OAAO,IAAI,QAAQ"}
|
package/dist/pointer/index.cjs
CHANGED
|
@@ -1 +1,15 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_pointer_find_reference = require('./find-reference.cjs');
|
|
2
|
+
const require_pointer_parse = require('./parse.cjs');
|
|
3
|
+
|
|
4
|
+
exports.escapePointerSegment = require_pointer_parse.escapePointerSegment;
|
|
5
|
+
exports.find = require_pointer_find_reference.find;
|
|
6
|
+
exports.formatJsonPointer = require_pointer_parse.formatJsonPointer;
|
|
7
|
+
exports.isArrayEnd = require_pointer_find_reference.isArrayEnd;
|
|
8
|
+
exports.isArrayReference = require_pointer_find_reference.isArrayReference;
|
|
9
|
+
exports.isInteger = require_pointer_parse.isInteger;
|
|
10
|
+
exports.isObjectReference = require_pointer_find_reference.isObjectReference;
|
|
11
|
+
exports.isRoot = require_pointer_parse.isRoot;
|
|
12
|
+
exports.isValidIndex = require_pointer_parse.isValidIndex;
|
|
13
|
+
exports.parent = require_pointer_parse.parent;
|
|
14
|
+
exports.parseJsonPointer = require_pointer_parse.parseJsonPointer;
|
|
15
|
+
exports.unescapePointerSegment = require_pointer_parse.unescapePointerSegment;
|
package/dist/pointer/index.mjs
CHANGED
|
@@ -1 +1,4 @@
|
|
|
1
|
-
import{find
|
|
1
|
+
import { find, isArrayEnd, isArrayReference, isObjectReference } from "./find-reference.mjs";
|
|
2
|
+
import { escapePointerSegment, formatJsonPointer, isInteger, isRoot, isValidIndex, parent, parseJsonPointer, unescapePointerSegment } from "./parse.mjs";
|
|
3
|
+
|
|
4
|
+
export { escapePointerSegment, find, formatJsonPointer, isArrayEnd, isArrayReference, isInteger, isObjectReference, isRoot, isValidIndex, parent, parseJsonPointer, unescapePointerSegment };
|
package/dist/pointer/parse.cjs
CHANGED
|
@@ -1 +1,87 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_is_string = require('../type-checks/src/is-string.cjs');
|
|
2
|
+
const require_is_number = require('../type-checks/src/is-number.cjs');
|
|
3
|
+
|
|
4
|
+
//#region src/pointer/parse.ts
|
|
5
|
+
const TILDE_ONE = /~1/g;
|
|
6
|
+
const TILDE_ZERO = /~0/g;
|
|
7
|
+
const TILDE = /~/g;
|
|
8
|
+
const FORWARD_SLASH = /\//g;
|
|
9
|
+
/**
|
|
10
|
+
* Escapes a JSON pointer path segment.
|
|
11
|
+
*
|
|
12
|
+
* @param segment - JSON pointer path segment.
|
|
13
|
+
* @returns Escaped JSON pointer path segment.
|
|
14
|
+
*/
|
|
15
|
+
function escapePointerSegment(segment) {
|
|
16
|
+
if (!segment.includes("/") && !segment.includes("~")) return segment;
|
|
17
|
+
return segment.replace(TILDE, "~0").replace(FORWARD_SLASH, "~1");
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Unescapes a JSON pointer path segment.
|
|
21
|
+
*
|
|
22
|
+
* @param segment - JSON pointer path segment.
|
|
23
|
+
* @returns Unescaped JSON pointer path segment.
|
|
24
|
+
*/
|
|
25
|
+
function unescapePointerSegment(segment) {
|
|
26
|
+
if (!segment.includes("~")) return segment;
|
|
27
|
+
return segment.replace(TILDE_ONE, "/").replace(TILDE_ZERO, "~");
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Convert JSON pointer like "/foo/bar" to array like ["", "foo", "bar"], while
|
|
31
|
+
* also un-escaping reserved characters.
|
|
32
|
+
*/
|
|
33
|
+
function parseJsonPointer(pointer) {
|
|
34
|
+
if (!pointer) return [];
|
|
35
|
+
return pointer.slice(1).split("/").map((segment) => unescapePointerSegment(segment));
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Escape and format a path array like ["", "foo", "bar"] to JSON pointer
|
|
39
|
+
* like "/foo/bar".
|
|
40
|
+
*/
|
|
41
|
+
function formatJsonPointer(path) {
|
|
42
|
+
if (isRoot(path)) return "";
|
|
43
|
+
return `/${path.map((segment) => escapePointerSegment(String(segment))).join("/")}`;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Returns true if JSON Pointer points to root value, false otherwise.
|
|
47
|
+
*/
|
|
48
|
+
const isRoot = (path) => require_is_string.isString(path) ? path === "" : require_is_number.isNumber(path) ? path === 0 : Array.isArray(path) && path.length === 0;
|
|
49
|
+
/**
|
|
50
|
+
* Returns parent path, e.g. for ['foo', 'bar', 'baz'] returns ['foo', 'bar'].
|
|
51
|
+
*/
|
|
52
|
+
function parent(path) {
|
|
53
|
+
if (path.length === 0) throw new Error("NO_PARENT");
|
|
54
|
+
return path.slice(0, -1);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Check if path component can be a valid array index.
|
|
58
|
+
*/
|
|
59
|
+
function isValidIndex(index) {
|
|
60
|
+
if (require_is_number.isNumber(index)) return true;
|
|
61
|
+
const n = Number.parseInt(index, 10);
|
|
62
|
+
return String(n) === index && n >= 0;
|
|
63
|
+
}
|
|
64
|
+
const isInteger = (str) => {
|
|
65
|
+
const len = str.length;
|
|
66
|
+
let i = 0;
|
|
67
|
+
let charCode;
|
|
68
|
+
while (i < len) {
|
|
69
|
+
charCode = str.codePointAt(i);
|
|
70
|
+
if (charCode >= 48 && charCode <= 57) {
|
|
71
|
+
i++;
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
return true;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
//#endregion
|
|
80
|
+
exports.escapePointerSegment = escapePointerSegment;
|
|
81
|
+
exports.formatJsonPointer = formatJsonPointer;
|
|
82
|
+
exports.isInteger = isInteger;
|
|
83
|
+
exports.isRoot = isRoot;
|
|
84
|
+
exports.isValidIndex = isValidIndex;
|
|
85
|
+
exports.parent = parent;
|
|
86
|
+
exports.parseJsonPointer = parseJsonPointer;
|
|
87
|
+
exports.unescapePointerSegment = unescapePointerSegment;
|
package/dist/pointer/parse.mjs
CHANGED
|
@@ -1,2 +1,81 @@
|
|
|
1
|
-
import{isString
|
|
1
|
+
import { isString } from "../type-checks/src/is-string.mjs";
|
|
2
|
+
import { isNumber } from "../type-checks/src/is-number.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/pointer/parse.ts
|
|
5
|
+
const TILDE_ONE = /~1/g;
|
|
6
|
+
const TILDE_ZERO = /~0/g;
|
|
7
|
+
const TILDE = /~/g;
|
|
8
|
+
const FORWARD_SLASH = /\//g;
|
|
9
|
+
/**
|
|
10
|
+
* Escapes a JSON pointer path segment.
|
|
11
|
+
*
|
|
12
|
+
* @param segment - JSON pointer path segment.
|
|
13
|
+
* @returns Escaped JSON pointer path segment.
|
|
14
|
+
*/
|
|
15
|
+
function escapePointerSegment(segment) {
|
|
16
|
+
if (!segment.includes("/") && !segment.includes("~")) return segment;
|
|
17
|
+
return segment.replace(TILDE, "~0").replace(FORWARD_SLASH, "~1");
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Unescapes a JSON pointer path segment.
|
|
21
|
+
*
|
|
22
|
+
* @param segment - JSON pointer path segment.
|
|
23
|
+
* @returns Unescaped JSON pointer path segment.
|
|
24
|
+
*/
|
|
25
|
+
function unescapePointerSegment(segment) {
|
|
26
|
+
if (!segment.includes("~")) return segment;
|
|
27
|
+
return segment.replace(TILDE_ONE, "/").replace(TILDE_ZERO, "~");
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Convert JSON pointer like "/foo/bar" to array like ["", "foo", "bar"], while
|
|
31
|
+
* also un-escaping reserved characters.
|
|
32
|
+
*/
|
|
33
|
+
function parseJsonPointer(pointer) {
|
|
34
|
+
if (!pointer) return [];
|
|
35
|
+
return pointer.slice(1).split("/").map((segment) => unescapePointerSegment(segment));
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Escape and format a path array like ["", "foo", "bar"] to JSON pointer
|
|
39
|
+
* like "/foo/bar".
|
|
40
|
+
*/
|
|
41
|
+
function formatJsonPointer(path) {
|
|
42
|
+
if (isRoot(path)) return "";
|
|
43
|
+
return `/${path.map((segment) => escapePointerSegment(String(segment))).join("/")}`;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Returns true if JSON Pointer points to root value, false otherwise.
|
|
47
|
+
*/
|
|
48
|
+
const isRoot = (path) => isString(path) ? path === "" : isNumber(path) ? path === 0 : Array.isArray(path) && path.length === 0;
|
|
49
|
+
/**
|
|
50
|
+
* Returns parent path, e.g. for ['foo', 'bar', 'baz'] returns ['foo', 'bar'].
|
|
51
|
+
*/
|
|
52
|
+
function parent(path) {
|
|
53
|
+
if (path.length === 0) throw new Error("NO_PARENT");
|
|
54
|
+
return path.slice(0, -1);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Check if path component can be a valid array index.
|
|
58
|
+
*/
|
|
59
|
+
function isValidIndex(index) {
|
|
60
|
+
if (isNumber(index)) return true;
|
|
61
|
+
const n = Number.parseInt(index, 10);
|
|
62
|
+
return String(n) === index && n >= 0;
|
|
63
|
+
}
|
|
64
|
+
const isInteger = (str) => {
|
|
65
|
+
const len = str.length;
|
|
66
|
+
let i = 0;
|
|
67
|
+
let charCode;
|
|
68
|
+
while (i < len) {
|
|
69
|
+
charCode = str.codePointAt(i);
|
|
70
|
+
if (charCode >= 48 && charCode <= 57) {
|
|
71
|
+
i++;
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
return true;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
//#endregion
|
|
80
|
+
export { escapePointerSegment, formatJsonPointer, isInteger, isRoot, isValidIndex, parent, parseJsonPointer, unescapePointerSegment };
|
|
2
81
|
//# sourceMappingURL=parse.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse.mjs","names":["charCode: any"],"sources":["../../src/pointer/parse.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { isNumber } from \"@stryke/type-checks/is-number\";\nimport { isString } from \"@stryke/type-checks/is-string\";\nimport type { JsonPointerPath } from \"@stryke/types/json\";\n\nconst TILDE_ONE = /~1/g;\nconst TILDE_ZERO = /~0/g;\nconst TILDE = /~/g;\nconst FORWARD_SLASH = /\\//g;\n\n/**\n * Escapes a JSON pointer path segment.\n *\n * @param segment - JSON pointer path segment.\n * @returns Escaped JSON pointer path segment.\n */\nexport function escapePointerSegment(segment: string): string {\n if (!segment.includes(\"/\") && !segment.includes(\"~\")) {\n return segment;\n }\n\n return segment.replace(TILDE, \"~0\").replace(FORWARD_SLASH, \"~1\");\n}\n\n/**\n * Unescapes a JSON pointer path segment.\n *\n * @param segment - JSON pointer path segment.\n * @returns Unescaped JSON pointer path segment.\n */\nexport function unescapePointerSegment(segment: string): string {\n if (!segment.includes(\"~\")) {\n return segment;\n }\n\n return segment.replace(TILDE_ONE, \"/\").replace(TILDE_ZERO, \"~\");\n}\n\n/**\n * Convert JSON pointer like \"/foo/bar\" to array like [\"\", \"foo\", \"bar\"], while\n * also un-escaping reserved characters.\n */\nexport function parseJsonPointer(pointer: string): JsonPointerPath {\n if (!pointer) return [];\n // TODO: Performance of this line can be improved: (1) don't use .split(); (2) don't use .map().\n return pointer\n .slice(1)\n .split(\"/\")\n .map(segment => unescapePointerSegment(segment));\n}\n\n/**\n * Escape and format a path array like [\"\", \"foo\", \"bar\"] to JSON pointer\n * like \"/foo/bar\".\n */\nexport function formatJsonPointer(path: JsonPointerPath): string {\n // eslint-disable-next-line ts/no-use-before-define\n if (isRoot(path)) {\n return \"\";\n }\n\n return `/${path.map(segment => escapePointerSegment(String(segment))).join(\"/\")}`;\n}\n\n/**\n * Returns true if JSON Pointer points to root value, false otherwise.\n */\nexport const isRoot = (path: string | number | JsonPointerPath): boolean =>\n isString(path)\n ? path === \"\"\n : isNumber(path)\n ? path === 0\n : Array.isArray(path) && path.length === 0;\n\n/**\n * Returns parent path, e.g. for ['foo', 'bar', 'baz'] returns ['foo', 'bar'].\n */\nexport function parent(path: JsonPointerPath): JsonPointerPath {\n if (path.length === 0) {\n throw new Error(\"NO_PARENT\");\n }\n\n return path.slice(0, -1);\n}\n\n/**\n * Check if path component can be a valid array index.\n */\nexport function isValidIndex(index: string | number): boolean {\n if (isNumber(index)) {\n return true;\n }\n\n const n = Number.parseInt(index, 10);\n\n return String(n) === index && n >= 0;\n}\n\nexport const isInteger = (str: string): boolean => {\n const len = str.length;\n let i = 0;\n let charCode: any;\n while (i < len) {\n charCode = str.codePointAt(i);\n if (charCode >= 48 && charCode <= 57) {\n i++;\n continue;\n }\n return false;\n }\n return true;\n};\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"parse.mjs","names":["charCode: any"],"sources":["../../src/pointer/parse.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { isNumber } from \"@stryke/type-checks/is-number\";\nimport { isString } from \"@stryke/type-checks/is-string\";\nimport type { JsonPointerPath } from \"@stryke/types/json\";\n\nconst TILDE_ONE = /~1/g;\nconst TILDE_ZERO = /~0/g;\nconst TILDE = /~/g;\nconst FORWARD_SLASH = /\\//g;\n\n/**\n * Escapes a JSON pointer path segment.\n *\n * @param segment - JSON pointer path segment.\n * @returns Escaped JSON pointer path segment.\n */\nexport function escapePointerSegment(segment: string): string {\n if (!segment.includes(\"/\") && !segment.includes(\"~\")) {\n return segment;\n }\n\n return segment.replace(TILDE, \"~0\").replace(FORWARD_SLASH, \"~1\");\n}\n\n/**\n * Unescapes a JSON pointer path segment.\n *\n * @param segment - JSON pointer path segment.\n * @returns Unescaped JSON pointer path segment.\n */\nexport function unescapePointerSegment(segment: string): string {\n if (!segment.includes(\"~\")) {\n return segment;\n }\n\n return segment.replace(TILDE_ONE, \"/\").replace(TILDE_ZERO, \"~\");\n}\n\n/**\n * Convert JSON pointer like \"/foo/bar\" to array like [\"\", \"foo\", \"bar\"], while\n * also un-escaping reserved characters.\n */\nexport function parseJsonPointer(pointer: string): JsonPointerPath {\n if (!pointer) return [];\n // TODO: Performance of this line can be improved: (1) don't use .split(); (2) don't use .map().\n return pointer\n .slice(1)\n .split(\"/\")\n .map(segment => unescapePointerSegment(segment));\n}\n\n/**\n * Escape and format a path array like [\"\", \"foo\", \"bar\"] to JSON pointer\n * like \"/foo/bar\".\n */\nexport function formatJsonPointer(path: JsonPointerPath): string {\n // eslint-disable-next-line ts/no-use-before-define\n if (isRoot(path)) {\n return \"\";\n }\n\n return `/${path.map(segment => escapePointerSegment(String(segment))).join(\"/\")}`;\n}\n\n/**\n * Returns true if JSON Pointer points to root value, false otherwise.\n */\nexport const isRoot = (path: string | number | JsonPointerPath): boolean =>\n isString(path)\n ? path === \"\"\n : isNumber(path)\n ? path === 0\n : Array.isArray(path) && path.length === 0;\n\n/**\n * Returns parent path, e.g. for ['foo', 'bar', 'baz'] returns ['foo', 'bar'].\n */\nexport function parent(path: JsonPointerPath): JsonPointerPath {\n if (path.length === 0) {\n throw new Error(\"NO_PARENT\");\n }\n\n return path.slice(0, -1);\n}\n\n/**\n * Check if path component can be a valid array index.\n */\nexport function isValidIndex(index: string | number): boolean {\n if (isNumber(index)) {\n return true;\n }\n\n const n = Number.parseInt(index, 10);\n\n return String(n) === index && n >= 0;\n}\n\nexport const isInteger = (str: string): boolean => {\n const len = str.length;\n let i = 0;\n let charCode: any;\n while (i < len) {\n charCode = str.codePointAt(i);\n if (charCode >= 48 && charCode <= 57) {\n i++;\n continue;\n }\n return false;\n }\n return true;\n};\n"],"mappings":";;;;AAsBA,MAAM,YAAY;AAClB,MAAM,aAAa;AACnB,MAAM,QAAQ;AACd,MAAM,gBAAgB;;;;;;;AAQtB,SAAgB,qBAAqB,SAAyB;AAC5D,KAAI,CAAC,QAAQ,SAAS,IAAI,IAAI,CAAC,QAAQ,SAAS,IAAI,CAClD,QAAO;AAGT,QAAO,QAAQ,QAAQ,OAAO,KAAK,CAAC,QAAQ,eAAe,KAAK;;;;;;;;AASlE,SAAgB,uBAAuB,SAAyB;AAC9D,KAAI,CAAC,QAAQ,SAAS,IAAI,CACxB,QAAO;AAGT,QAAO,QAAQ,QAAQ,WAAW,IAAI,CAAC,QAAQ,YAAY,IAAI;;;;;;AAOjE,SAAgB,iBAAiB,SAAkC;AACjE,KAAI,CAAC,QAAS,QAAO,EAAE;AAEvB,QAAO,QACJ,MAAM,EAAE,CACR,MAAM,IAAI,CACV,KAAI,YAAW,uBAAuB,QAAQ,CAAC;;;;;;AAOpD,SAAgB,kBAAkB,MAA+B;AAE/D,KAAI,OAAO,KAAK,CACd,QAAO;AAGT,QAAO,IAAI,KAAK,KAAI,YAAW,qBAAqB,OAAO,QAAQ,CAAC,CAAC,CAAC,KAAK,IAAI;;;;;AAMjF,MAAa,UAAU,SACrB,SAAS,KAAK,GACV,SAAS,KACT,SAAS,KAAK,GACZ,SAAS,IACT,MAAM,QAAQ,KAAK,IAAI,KAAK,WAAW;;;;AAK/C,SAAgB,OAAO,MAAwC;AAC7D,KAAI,KAAK,WAAW,EAClB,OAAM,IAAI,MAAM,YAAY;AAG9B,QAAO,KAAK,MAAM,GAAG,GAAG;;;;;AAM1B,SAAgB,aAAa,OAAiC;AAC5D,KAAI,SAAS,MAAM,CACjB,QAAO;CAGT,MAAM,IAAI,OAAO,SAAS,OAAO,GAAG;AAEpC,QAAO,OAAO,EAAE,KAAK,SAAS,KAAK;;AAGrC,MAAa,aAAa,QAAyB;CACjD,MAAM,MAAM,IAAI;CAChB,IAAI,IAAI;CACR,IAAIA;AACJ,QAAO,IAAI,KAAK;AACd,aAAW,IAAI,YAAY,EAAE;AAC7B,MAAI,YAAY,MAAM,YAAY,IAAI;AACpC;AACA;;AAEF,SAAO;;AAET,QAAO"}
|
package/dist/storm-json.cjs
CHANGED
|
@@ -1 +1,117 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_is_object = require('./type-checks/src/is-object.cjs');
|
|
3
|
+
const require_is_string = require('./type-checks/src/is-string.cjs');
|
|
4
|
+
const require_utils_parse = require('./utils/parse.cjs');
|
|
5
|
+
const require_utils_parse_error = require('./utils/parse-error.cjs');
|
|
6
|
+
const require_utils_stringify = require('./utils/stringify.cjs');
|
|
7
|
+
let jsonc_parser = require("jsonc-parser");
|
|
8
|
+
let node_buffer = require("node:buffer");
|
|
9
|
+
let superjson = require("superjson");
|
|
10
|
+
superjson = require_rolldown_runtime.__toESM(superjson);
|
|
11
|
+
|
|
12
|
+
//#region src/storm-json.ts
|
|
13
|
+
/**
|
|
14
|
+
* A static JSON parser class used by Storm Software to serialize and deserialize JSON data
|
|
15
|
+
*
|
|
16
|
+
* @remarks
|
|
17
|
+
* This class uses the [SuperJSON](https://github.com/blitz-js/superjson) library under the hood.
|
|
18
|
+
*/
|
|
19
|
+
var StormJSON = class StormJSON extends superjson.default {
|
|
20
|
+
static #instance;
|
|
21
|
+
static get instance() {
|
|
22
|
+
if (!StormJSON.#instance) StormJSON.#instance = new StormJSON();
|
|
23
|
+
return StormJSON.#instance;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Deserialize the given value with superjson using the given metadata
|
|
27
|
+
*/
|
|
28
|
+
static deserialize(payload) {
|
|
29
|
+
return StormJSON.instance.deserialize(payload);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Serialize the given value with superjson
|
|
33
|
+
*/
|
|
34
|
+
static serialize(object) {
|
|
35
|
+
return StormJSON.instance.serialize(object);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Parse the given string value with superjson using the given metadata
|
|
39
|
+
*
|
|
40
|
+
* @param value - The string value to parse
|
|
41
|
+
* @returns The parsed data
|
|
42
|
+
*/
|
|
43
|
+
static parse(value) {
|
|
44
|
+
return require_utils_parse.parse(value);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Serializes the given data to a JSON string.
|
|
48
|
+
* By default the JSON string is formatted with a 2 space indentation to be easy readable.
|
|
49
|
+
*
|
|
50
|
+
* @param value - Object which should be serialized to JSON
|
|
51
|
+
* @param _options - JSON serialize options
|
|
52
|
+
* @returns the formatted JSON representation of the object
|
|
53
|
+
*/
|
|
54
|
+
static stringify(value, _options) {
|
|
55
|
+
const customTransformer = StormJSON.instance.customTransformerRegistry.findApplicable(value);
|
|
56
|
+
let result = value;
|
|
57
|
+
if (customTransformer && customTransformer.isApplicable(value)) result = customTransformer.serialize(result);
|
|
58
|
+
return require_utils_stringify.stringify(result);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Parses the given JSON string and returns the object the JSON content represents.
|
|
62
|
+
* By default javascript-style comments and trailing commas are allowed.
|
|
63
|
+
*
|
|
64
|
+
* @param strData - JSON content as string
|
|
65
|
+
* @param options - JSON parse options
|
|
66
|
+
* @returns Object the JSON content represents
|
|
67
|
+
*/
|
|
68
|
+
static parseJson(strData, options) {
|
|
69
|
+
try {
|
|
70
|
+
if (options?.expectComments === false) return StormJSON.instance.parse(strData);
|
|
71
|
+
} catch {}
|
|
72
|
+
const errors = [];
|
|
73
|
+
const result = (0, jsonc_parser.parse)(strData, errors, {
|
|
74
|
+
allowTrailingComma: true,
|
|
75
|
+
...options
|
|
76
|
+
});
|
|
77
|
+
if (errors.length > 0 && errors[0]) throw new Error(require_utils_parse_error.formatParseError(strData, errors[0]));
|
|
78
|
+
return result;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Register a custom schema with superjson
|
|
82
|
+
*
|
|
83
|
+
* @param name - The name of the schema
|
|
84
|
+
* @param serialize - The function to serialize the schema
|
|
85
|
+
* @param deserialize - The function to deserialize the schema
|
|
86
|
+
* @param isApplicable - The function to check if the schema is applicable
|
|
87
|
+
*/
|
|
88
|
+
static register(name, serialize, deserialize, isApplicable) {
|
|
89
|
+
StormJSON.instance.registerCustom({
|
|
90
|
+
isApplicable,
|
|
91
|
+
serialize,
|
|
92
|
+
deserialize
|
|
93
|
+
}, name);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Register a class with superjson
|
|
97
|
+
*
|
|
98
|
+
* @param classConstructor - The class constructor to register
|
|
99
|
+
*/
|
|
100
|
+
static registerClass(classConstructor, options) {
|
|
101
|
+
StormJSON.instance.registerClass(classConstructor, {
|
|
102
|
+
identifier: require_is_string.isString(options) ? options : options?.identifier || classConstructor.name,
|
|
103
|
+
allowProps: options && require_is_object.isObject(options) && options?.allowProps && Array.isArray(options.allowProps) ? options.allowProps : ["__typename"]
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
constructor() {
|
|
107
|
+
super({ dedupe: true });
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
StormJSON.instance.registerCustom({
|
|
111
|
+
isApplicable: (v) => node_buffer.Buffer.isBuffer(v),
|
|
112
|
+
serialize: (v) => v.toString("base64"),
|
|
113
|
+
deserialize: (v) => node_buffer.Buffer.from(v, "base64")
|
|
114
|
+
}, "Bytes");
|
|
115
|
+
|
|
116
|
+
//#endregion
|
|
117
|
+
exports.StormJSON = StormJSON;
|