@stryke/json 0.9.35 → 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.
Files changed (70) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/_virtual/rolldown_runtime.cjs +29 -1
  3. package/dist/index.cjs +16 -1
  4. package/dist/index.mjs +9 -1
  5. package/dist/pointer/find-reference.cjs +66 -1
  6. package/dist/pointer/find-reference.mjs +63 -1
  7. package/dist/pointer/find-reference.mjs.map +1 -1
  8. package/dist/pointer/index.cjs +15 -1
  9. package/dist/pointer/index.mjs +4 -1
  10. package/dist/pointer/parse.cjs +87 -1
  11. package/dist/pointer/parse.mjs +80 -1
  12. package/dist/pointer/parse.mjs.map +1 -1
  13. package/dist/storm-json.cjs +117 -1
  14. package/dist/storm-json.mjs +115 -1
  15. package/dist/storm-json.mjs.map +1 -1
  16. package/dist/type-checks/src/get-object-tag.cjs +15 -1
  17. package/dist/type-checks/src/get-object-tag.mjs +14 -1
  18. package/dist/type-checks/src/get-object-tag.mjs.map +1 -1
  19. package/dist/type-checks/src/is-empty.cjs +20 -1
  20. package/dist/type-checks/src/is-empty.mjs +20 -1
  21. package/dist/type-checks/src/is-empty.mjs.map +1 -1
  22. package/dist/type-checks/src/is-non-null-object.cjs +16 -1
  23. package/dist/type-checks/src/is-non-null-object.mjs +16 -1
  24. package/dist/type-checks/src/is-non-null-object.mjs.map +1 -1
  25. package/dist/type-checks/src/is-null.cjs +12 -1
  26. package/dist/type-checks/src/is-null.mjs +11 -1
  27. package/dist/type-checks/src/is-null.mjs.map +1 -1
  28. package/dist/type-checks/src/is-number.cjs +18 -1
  29. package/dist/type-checks/src/is-number.mjs +17 -1
  30. package/dist/type-checks/src/is-number.mjs.map +1 -1
  31. package/dist/type-checks/src/is-object.cjs +19 -1
  32. package/dist/type-checks/src/is-object.mjs +19 -1
  33. package/dist/type-checks/src/is-object.mjs.map +1 -1
  34. package/dist/type-checks/src/is-plain-object.cjs +63 -1
  35. package/dist/type-checks/src/is-plain-object.mjs +63 -1
  36. package/dist/type-checks/src/is-plain-object.mjs.map +1 -1
  37. package/dist/type-checks/src/is-set-object.cjs +19 -1
  38. package/dist/type-checks/src/is-set-object.mjs +19 -1
  39. package/dist/type-checks/src/is-set-object.mjs.map +1 -1
  40. package/dist/type-checks/src/is-set.cjs +19 -1
  41. package/dist/type-checks/src/is-set.mjs +19 -1
  42. package/dist/type-checks/src/is-set.mjs.map +1 -1
  43. package/dist/type-checks/src/is-string.cjs +12 -1
  44. package/dist/type-checks/src/is-string.mjs +11 -1
  45. package/dist/type-checks/src/is-string.mjs.map +1 -1
  46. package/dist/type-checks/src/is-undefined.cjs +8 -1
  47. package/dist/type-checks/src/is-undefined.mjs +7 -1
  48. package/dist/type-checks/src/is-undefined.mjs.map +1 -1
  49. package/dist/types/src/base.cjs +6 -1
  50. package/dist/types/src/base.mjs +5 -1
  51. package/dist/types/src/base.mjs.map +1 -1
  52. package/dist/types.mjs +1 -1
  53. package/dist/utils/code-frames.cjs +77 -3
  54. package/dist/utils/code-frames.mjs +76 -3
  55. package/dist/utils/code-frames.mjs.map +1 -1
  56. package/dist/utils/index.cjs +13 -1
  57. package/dist/utils/index.mjs +7 -1
  58. package/dist/utils/parse-error.cjs +34 -1
  59. package/dist/utils/parse-error.mjs +33 -1
  60. package/dist/utils/parse-error.mjs.map +1 -1
  61. package/dist/utils/parse.cjs +52 -1
  62. package/dist/utils/parse.mjs +51 -1
  63. package/dist/utils/parse.mjs.map +1 -1
  64. package/dist/utils/stringify.cjs +68 -1
  65. package/dist/utils/stringify.mjs +67 -1
  66. package/dist/utils/stringify.mjs.map +1 -1
  67. package/dist/utils/strip-comments.cjs +86 -4
  68. package/dist/utils/strip-comments.mjs +86 -4
  69. package/dist/utils/strip-comments.mjs.map +1 -1
  70. package/package.json +3 -3
package/CHANGELOG.md CHANGED
@@ -2,6 +2,20 @@
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
+
12
+ ## [0.9.35](https://github.com/storm-software/stryke/releases/tag/json%400.9.35) (01/15/2026)
13
+
14
+ ### Updated Dependencies
15
+
16
+ - Updated **type-checks** to **v0.5.17**
17
+ - Updated **types** to **v0.10.31**
18
+
5
19
  ## [0.9.34](https://github.com/storm-software/stryke/releases/tag/json%400.9.34) (01/13/2026)
6
20
 
7
21
  ### Updated Dependencies
@@ -1 +1,29 @@
1
- var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));exports.__toESM=s;
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 e=require(`./utils/strip-comments.cjs`),t=require(`./utils/parse.cjs`),n=require(`./utils/code-frames.cjs`),r=require(`./utils/parse-error.cjs`),i=require(`./utils/stringify.cjs`),a=require(`./storm-json.cjs`);require(`./utils/index.cjs`),exports.StormJSON=a.StormJSON,exports.codeFrameColumns=n.codeFrameColumns,exports.formatParseError=r.formatParseError,exports.invalidKeyChars=i.invalidKeyChars,exports.parse=t.parse,exports.safeParse=t.safeParse,exports.stringify=i.stringify,exports.stripComments=e.stripComments;
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{stripComments as e}from"./utils/strip-comments.mjs";import{parse as t,safeParse as n}from"./utils/parse.mjs";import{codeFrameColumns as r}from"./utils/code-frames.mjs";import{formatParseError as i}from"./utils/parse-error.mjs";import{invalidKeyChars as a,stringify as o}from"./utils/stringify.mjs";import{StormJSON as s}from"./storm-json.mjs";import"./utils/index.mjs";export{s as StormJSON,r as codeFrameColumns,i as formatParseError,a as invalidKeyChars,t as parse,n as safeParse,o as stringify,e as stripComments};
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 e=require(`../type-checks/src/is-string.cjs`),t=require(`../type-checks/src/is-set-object.cjs`),{isArray:n}=Array,r=(r,i)=>{let a=i.length;if(!a)return{val:r};let o,s;for(let c=0;c<a;c++)if(o=r,s=i[c],n(o)){let t=o.length;if(s===`-`)s=t;else if(e.isString(s)){let e=Math.trunc(Number.parseInt(s));if(String(e)!==s||(s=e,s<0))throw Error(`INVALID_INDEX`)}s&&(r=o[s])}else if(t.isSetObject(o))r=s&&s in o?o[s]:void 0;else throw Error(`NOT_FOUND`);return{val:r,obj:o,key:s}},i=e=>n(e.obj)&&typeof e.key==`number`,a=e=>e.obj.length===e.key,o=e=>typeof e.obj==`object`&&typeof e.key==`string`;exports.find=r,exports.isArrayEnd=a,exports.isArrayReference=i,exports.isObjectReference=o;
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 as e}from"../type-checks/src/is-string.mjs";import{isSetObject as t}from"../type-checks/src/is-set-object.mjs";const{isArray:n}=Array,r=(r,i)=>{let a=i.length;if(!a)return{val:r};let o,s;for(let c=0;c<a;c++)if(o=r,s=i[c],n(o)){let t=o.length;if(s===`-`)s=t;else if(e(s)){let e=Math.trunc(Number.parseInt(s));if(String(e)!==s||(s=e,s<0))throw Error(`INVALID_INDEX`)}s&&(r=o[s])}else if(t(o))r=s&&s in o?o[s]:void 0;else throw Error(`NOT_FOUND`);return{val:r,obj:o,key:s}},i=e=>n(e.obj)&&typeof e.key==`number`,a=e=>e.obj.length===e.key,o=e=>typeof e.obj==`object`&&typeof e.key==`string`;export{r as find,a as isArrayEnd,i as isArrayReference,o as isObjectReference};
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":"+HA8BA,KAAM,CAAE,WAAY,MA2BP,GAAQ,EAAc,IAAwC,CACzE,IAAM,EAAa,EAAK,OACxB,GAAI,CAAC,EACH,MAAO,CAAE,MAAK,CAGhB,IAAIA,EACAC,EACJ,IAAK,IAAI,EAAI,EAAG,EAAI,EAAY,IAG9B,GAFA,EAAM,EACN,EAAM,EAAK,GACP,EAAQ,EAAI,CAAE,CAChB,IAAM,EAAS,EAAI,OACnB,GAAI,IAAQ,IACV,EAAM,UACG,EAAS,EAAI,CAAE,CACxB,IAAM,EAAO,KAAK,MAAM,OAAO,SAAS,EAAI,CAAC,CAM7C,GALI,OAAO,EAAK,GAAK,IAIrB,EAAM,EACF,EAAM,GACR,MAAU,MAAM,gBAAgB,CAIhC,IACF,EAAM,EAAI,YAEH,EAAY,EAAI,CACzB,EAAM,GAAO,KAAO,EAAO,EAAY,GAAO,IAAA,QACzC,MAAU,MAAM,YAAY,CAQrC,MANuB,CACrB,MACA,MACA,MACD,EAYU,EACX,GAC6B,EAAQ,EAAI,IAAI,EAAI,OAAO,EAAI,KAAQ,SAEzD,EAAc,GACzB,EAAI,IAAI,SAAW,EAAI,IAQZ,EACX,GAEA,OAAO,EAAI,KAAQ,UAAY,OAAO,EAAI,KAAQ"}
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"}
@@ -1 +1,15 @@
1
- const e=require(`./find-reference.cjs`),t=require(`./parse.cjs`);exports.escapePointerSegment=t.escapePointerSegment,exports.find=e.find,exports.formatJsonPointer=t.formatJsonPointer,exports.isArrayEnd=e.isArrayEnd,exports.isArrayReference=e.isArrayReference,exports.isInteger=t.isInteger,exports.isObjectReference=e.isObjectReference,exports.isRoot=t.isRoot,exports.isValidIndex=t.isValidIndex,exports.parent=t.parent,exports.parseJsonPointer=t.parseJsonPointer,exports.unescapePointerSegment=t.unescapePointerSegment;
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;
@@ -1 +1,4 @@
1
- import{find as e,isArrayEnd as t,isArrayReference as n,isObjectReference as r}from"./find-reference.mjs";import{escapePointerSegment as i,formatJsonPointer as a,isInteger as o,isRoot as s,isValidIndex as c,parent as l,parseJsonPointer as u,unescapePointerSegment as d}from"./parse.mjs";export{i as escapePointerSegment,e as find,a as formatJsonPointer,t as isArrayEnd,n as isArrayReference,o as isInteger,r as isObjectReference,s as isRoot,c as isValidIndex,l as parent,u as parseJsonPointer,d as unescapePointerSegment};
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 };
@@ -1 +1,87 @@
1
- const e=require(`../type-checks/src/is-string.cjs`),t=require(`../type-checks/src/is-number.cjs`),n=/~1/g,r=/~0/g,i=/~/g,a=/\//g;function o(e){return!e.includes(`/`)&&!e.includes(`~`)?e:e.replace(i,`~0`).replace(a,`~1`)}function s(e){return e.includes(`~`)?e.replace(n,`/`).replace(r,`~`):e}function c(e){return e?e.slice(1).split(`/`).map(e=>s(e)):[]}function l(e){return u(e)?``:`/${e.map(e=>o(String(e))).join(`/`)}`}const u=n=>e.isString(n)?n===``:t.isNumber(n)?n===0:Array.isArray(n)&&n.length===0;function d(e){if(e.length===0)throw Error(`NO_PARENT`);return e.slice(0,-1)}function f(e){if(t.isNumber(e))return!0;let n=Number.parseInt(e,10);return String(n)===e&&n>=0}const p=e=>{let t=e.length,n=0,r;for(;n<t;){if(r=e.codePointAt(n),r>=48&&r<=57){n++;continue}return!1}return!0};exports.escapePointerSegment=o,exports.formatJsonPointer=l,exports.isInteger=p,exports.isRoot=u,exports.isValidIndex=f,exports.parent=d,exports.parseJsonPointer=c,exports.unescapePointerSegment=s;
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;
@@ -1,2 +1,81 @@
1
- import{isString as e}from"../type-checks/src/is-string.mjs";import{isNumber as t}from"../type-checks/src/is-number.mjs";const n=/~1/g,r=/~0/g,i=/~/g,a=/\//g;function o(e){return!e.includes(`/`)&&!e.includes(`~`)?e:e.replace(i,`~0`).replace(a,`~1`)}function s(e){return e.includes(`~`)?e.replace(n,`/`).replace(r,`~`):e}function c(e){return e?e.slice(1).split(`/`).map(e=>s(e)):[]}function l(e){return u(e)?``:`/${e.map(e=>o(String(e))).join(`/`)}`}const u=n=>e(n)?n===``:t(n)?n===0:Array.isArray(n)&&n.length===0;function d(e){if(e.length===0)throw Error(`NO_PARENT`);return e.slice(0,-1)}function f(e){if(t(e))return!0;let n=Number.parseInt(e,10);return String(n)===e&&n>=0}const p=e=>{let t=e.length,n=0,r;for(;n<t;){if(r=e.codePointAt(n),r>=48&&r<=57){n++;continue}return!1}return!0};export{o as escapePointerSegment,l as formatJsonPointer,p as isInteger,u as isRoot,f as isValidIndex,d as parent,c as parseJsonPointer,s as unescapePointerSegment};
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":"wHAsBA,MAAM,EAAY,MACZ,EAAa,MACb,EAAQ,KACR,EAAgB,MAQtB,SAAgB,EAAqB,EAAyB,CAK5D,MAJI,CAAC,EAAQ,SAAS,IAAI,EAAI,CAAC,EAAQ,SAAS,IAAI,CAC3C,EAGF,EAAQ,QAAQ,EAAO,KAAK,CAAC,QAAQ,EAAe,KAAK,CASlE,SAAgB,EAAuB,EAAyB,CAK9D,OAJK,EAAQ,SAAS,IAAI,CAInB,EAAQ,QAAQ,EAAW,IAAI,CAAC,QAAQ,EAAY,IAAI,CAHtD,EAUX,SAAgB,EAAiB,EAAkC,CAGjE,OAFK,EAEE,EACJ,MAAM,EAAE,CACR,MAAM,IAAI,CACV,IAAI,GAAW,EAAuB,EAAQ,CAAC,CAL7B,EAAE,CAYzB,SAAgB,EAAkB,EAA+B,CAM/D,OAJI,EAAO,EAAK,CACP,GAGF,IAAI,EAAK,IAAI,GAAW,EAAqB,OAAO,EAAQ,CAAC,CAAC,CAAC,KAAK,IAAI,GAMjF,MAAa,EAAU,GACrB,EAAS,EAAK,CACV,IAAS,GACT,EAAS,EAAK,CACZ,IAAS,EACT,MAAM,QAAQ,EAAK,EAAI,EAAK,SAAW,EAK/C,SAAgB,EAAO,EAAwC,CAC7D,GAAI,EAAK,SAAW,EAClB,MAAU,MAAM,YAAY,CAG9B,OAAO,EAAK,MAAM,EAAG,GAAG,CAM1B,SAAgB,EAAa,EAAiC,CAC5D,GAAI,EAAS,EAAM,CACjB,MAAO,GAGT,IAAM,EAAI,OAAO,SAAS,EAAO,GAAG,CAEpC,OAAO,OAAO,EAAE,GAAK,GAAS,GAAK,EAGrC,MAAa,EAAa,GAAyB,CACjD,IAAM,EAAM,EAAI,OACZ,EAAI,EACJA,EACJ,KAAO,EAAI,GAAK,CAEd,GADA,EAAW,EAAI,YAAY,EAAE,CACzB,GAAY,IAAM,GAAY,GAAI,CACpC,IACA,SAEF,MAAO,GAET,MAAO"}
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"}
@@ -1 +1,117 @@
1
- const e=require(`./_virtual/rolldown_runtime.cjs`),t=require(`./type-checks/src/is-object.cjs`),n=require(`./type-checks/src/is-string.cjs`),r=require(`./utils/parse.cjs`),i=require(`./utils/parse-error.cjs`),a=require(`./utils/stringify.cjs`);let o=require(`jsonc-parser`),s=require(`node:buffer`),c=require(`superjson`);c=e.__toESM(c);var l=class e extends c.default{static#e;static get instance(){return e.#e||=new e,e.#e}static deserialize(t){return e.instance.deserialize(t)}static serialize(t){return e.instance.serialize(t)}static parse(e){return r.parse(e)}static stringify(t,n){let r=e.instance.customTransformerRegistry.findApplicable(t),i=t;return r&&r.isApplicable(t)&&(i=r.serialize(i)),a.stringify(i)}static parseJson(t,n){try{if(n?.expectComments===!1)return e.instance.parse(t)}catch{}let r=[],a=(0,o.parse)(t,r,{allowTrailingComma:!0,...n});if(r.length>0&&r[0])throw Error(i.formatParseError(t,r[0]));return a}static register(t,n,r,i){e.instance.registerCustom({isApplicable:i,serialize:n,deserialize:r},t)}static registerClass(r,i){e.instance.registerClass(r,{identifier:n.isString(i)?i:i?.identifier||r.name,allowProps:i&&t.isObject(i)&&i?.allowProps&&Array.isArray(i.allowProps)?i.allowProps:[`__typename`]})}constructor(){super({dedupe:!0})}};l.instance.registerCustom({isApplicable:e=>s.Buffer.isBuffer(e),serialize:e=>e.toString(`base64`),deserialize:e=>s.Buffer.from(e,`base64`)},`Bytes`),exports.StormJSON=l;
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;