@stryke/json 0.8.3 → 0.9.0

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/dist/index.cjs CHANGED
@@ -24,4 +24,15 @@ Object.keys(_types).forEach(function (key) {
24
24
  return _types[key];
25
25
  }
26
26
  });
27
+ });
28
+ var _utils = require("./utils/index.cjs");
29
+ Object.keys(_utils).forEach(function (key) {
30
+ if (key === "default" || key === "__esModule") return;
31
+ if (key in exports && exports[key] === _utils[key]) return;
32
+ Object.defineProperty(exports, key, {
33
+ enumerable: true,
34
+ get: function () {
35
+ return _utils[key];
36
+ }
37
+ });
27
38
  });
package/dist/index.d.ts CHANGED
@@ -8,3 +8,4 @@
8
8
  */
9
9
  export * from "./storm-json";
10
10
  export * from "./types";
11
+ export * from "./utils";
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- export*from"./storm-json";export*from"./types";
1
+ export*from"./storm-json";export*from"./types";export*from"./utils";
@@ -14,6 +14,17 @@ Object.keys(_codeFrames).forEach(function (key) {
14
14
  }
15
15
  });
16
16
  });
17
+ var _parse = require("./parse.cjs");
18
+ Object.keys(_parse).forEach(function (key) {
19
+ if (key === "default" || key === "__esModule") return;
20
+ if (key in exports && exports[key] === _parse[key]) return;
21
+ Object.defineProperty(exports, key, {
22
+ enumerable: true,
23
+ get: function () {
24
+ return _parse[key];
25
+ }
26
+ });
27
+ });
17
28
  var _parseError = require("./parse-error.cjs");
18
29
  Object.keys(_parseError).forEach(function (key) {
19
30
  if (key === "default" || key === "__esModule") return;
@@ -1,4 +1,5 @@
1
1
  export * from "./code-frames";
2
+ export * from "./parse";
2
3
  export * from "./parse-error";
3
4
  export * from "./stringify";
4
5
  export * from "./strip-comments";
@@ -1 +1 @@
1
- export*from"./code-frames";export*from"./parse-error";export*from"./stringify";export*from"./strip-comments";
1
+ export*from"./code-frames";export*from"./parse";export*from"./parse-error";export*from"./stringify";export*from"./strip-comments";
@@ -5,21 +5,22 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.parse = parse;
7
7
  exports.safeParse = safeParse;
8
- const e = /"(?:_|\\u0{2}5[Ff]){2}(?:p|\\u0{2}70)(?:r|\\u0{2}72)(?:o|\\u0{2}6[Ff])(?:t|\\u0{2}74)(?:o|\\u0{2}6[Ff])(?:_|\\u0{2}5[Ff]){2}"\s*:/,
9
- o = /"(?:c|\\u0063)(?:o|\\u006[Ff])(?:n|\\u006[Ee])(?:s|\\u0073)(?:t|\\u0074)(?:r|\\u0072)(?:u|\\u0075)(?:c|\\u0063)(?:t|\\u0074)(?:o|\\u006[Ff])(?:r|\\u0072)"\s*:/,
8
+ var _stripComments = require("./strip-comments.cjs");
9
+ const o = /"(?:_|\\u0{2}5[Ff]){2}(?:p|\\u0{2}70)(?:r|\\u0{2}72)(?:o|\\u0{2}6[Ff])(?:t|\\u0{2}74)(?:o|\\u0{2}6[Ff])(?:_|\\u0{2}5[Ff]){2}"\s*:/,
10
+ u = /"(?:c|\\u0063)(?:o|\\u006[Ff])(?:n|\\u006[Ee])(?:s|\\u0073)(?:t|\\u0074)(?:r|\\u0072)(?:u|\\u0075)(?:c|\\u0063)(?:t|\\u0074)(?:o|\\u006[Ff])(?:r|\\u0072)"\s*:/,
10
11
  i = /^\s*["[{]|^\s*-?\d{1,16}(?:\.\d{1,17})?(?:E[+-]?\d+)?\s*$/i;
11
- function u(r, t) {
12
- if (r === "__proto__" || r === "constructor" && t && typeof t == "object" && "prototype" in t) {
13
- console.warn(`Dropping "${r}" key to prevent prototype pollution.`);
12
+ function c(t, n) {
13
+ if (t === "__proto__" || t === "constructor" && n && typeof n == "object" && "prototype" in n) {
14
+ console.warn(`Dropping "${t}" key to prevent prototype pollution.`);
14
15
  return;
15
16
  }
16
- return t;
17
+ return n;
17
18
  }
18
- function parse(r, t = {}) {
19
- if (typeof r != "string") return r;
19
+ function parse(t, n = {}) {
20
+ if (typeof t != "string") return t;
21
+ let r = (0, _stripComments.stripComments)(t);
20
22
  if (r[0] === '"' && r[r.length - 1] === '"' && !r.includes("\\")) return r.slice(1, -1);
21
- const n = r.trim();
22
- if (n.length <= 9) switch (n.toLowerCase()) {
23
+ if (r = r.trim(), r.length <= 9) switch (r.toLowerCase()) {
23
24
  case "true":
24
25
  return !0;
25
26
  case "false":
@@ -36,23 +37,23 @@ function parse(r, t = {}) {
36
37
  return Number.NEGATIVE_INFINITY;
37
38
  }
38
39
  if (!i.test(r)) {
39
- if (t.strict) throw new Error("Invalid JSON");
40
+ if (n.strict) throw new Error("Invalid JSON");
40
41
  return r;
41
42
  }
42
43
  try {
43
- if (e.test(r) || o.test(r)) {
44
- if (t.strict) throw new Error("Possible prototype pollution");
45
- return JSON.parse(r, u);
44
+ if (o.test(r) || u.test(r)) {
45
+ if (n.strict) throw new Error("Possible prototype pollution");
46
+ return JSON.parse(r, c);
46
47
  }
47
48
  return JSON.parse(r);
48
- } catch (s) {
49
- if (t.strict) throw s;
50
- return r;
49
+ } catch (e) {
50
+ if (n.strict) throw e;
51
+ return t;
51
52
  }
52
53
  }
53
- function safeParse(r, t = {}) {
54
- return parse(r, {
55
- ...t,
54
+ function safeParse(t, n = {}) {
55
+ return parse(t, {
56
+ ...n,
56
57
  strict: !0
57
58
  });
58
59
  }
@@ -1 +1 @@
1
- const e=/"(?:_|\\u0{2}5[Ff]){2}(?:p|\\u0{2}70)(?:r|\\u0{2}72)(?:o|\\u0{2}6[Ff])(?:t|\\u0{2}74)(?:o|\\u0{2}6[Ff])(?:_|\\u0{2}5[Ff]){2}"\s*:/,o=/"(?:c|\\u0063)(?:o|\\u006[Ff])(?:n|\\u006[Ee])(?:s|\\u0073)(?:t|\\u0074)(?:r|\\u0072)(?:u|\\u0075)(?:c|\\u0063)(?:t|\\u0074)(?:o|\\u006[Ff])(?:r|\\u0072)"\s*:/,i=/^\s*["[{]|^\s*-?\d{1,16}(?:\.\d{1,17})?(?:E[+-]?\d+)?\s*$/i;function u(r,t){if(r==="__proto__"||r==="constructor"&&t&&typeof t=="object"&&"prototype"in t){console.warn(`Dropping "${r}" key to prevent prototype pollution.`);return}return t}export function parse(r,t={}){if(typeof r!="string")return r;if(r[0]==='"'&&r[r.length-1]==='"'&&!r.includes("\\"))return r.slice(1,-1);const n=r.trim();if(n.length<=9)switch(n.toLowerCase()){case"true":return!0;case"false":return!1;case"undefined":return;case"null":return null;case"nan":return Number.NaN;case"infinity":return Number.POSITIVE_INFINITY;case"-infinity":return Number.NEGATIVE_INFINITY}if(!i.test(r)){if(t.strict)throw new Error("Invalid JSON");return r}try{if(e.test(r)||o.test(r)){if(t.strict)throw new Error("Possible prototype pollution");return JSON.parse(r,u)}return JSON.parse(r)}catch(s){if(t.strict)throw s;return r}}export function safeParse(r,t={}){return parse(r,{...t,strict:!0})}
1
+ import{stripComments as s}from"./strip-comments";const o=/"(?:_|\\u0{2}5[Ff]){2}(?:p|\\u0{2}70)(?:r|\\u0{2}72)(?:o|\\u0{2}6[Ff])(?:t|\\u0{2}74)(?:o|\\u0{2}6[Ff])(?:_|\\u0{2}5[Ff]){2}"\s*:/,u=/"(?:c|\\u0063)(?:o|\\u006[Ff])(?:n|\\u006[Ee])(?:s|\\u0073)(?:t|\\u0074)(?:r|\\u0072)(?:u|\\u0075)(?:c|\\u0063)(?:t|\\u0074)(?:o|\\u006[Ff])(?:r|\\u0072)"\s*:/,i=/^\s*["[{]|^\s*-?\d{1,16}(?:\.\d{1,17})?(?:E[+-]?\d+)?\s*$/i;function c(t,n){if(t==="__proto__"||t==="constructor"&&n&&typeof n=="object"&&"prototype"in n){console.warn(`Dropping "${t}" key to prevent prototype pollution.`);return}return n}export function parse(t,n={}){if(typeof t!="string")return t;let r=s(t);if(r[0]==='"'&&r[r.length-1]==='"'&&!r.includes("\\"))return r.slice(1,-1);if(r=r.trim(),r.length<=9)switch(r.toLowerCase()){case"true":return!0;case"false":return!1;case"undefined":return;case"null":return null;case"nan":return Number.NaN;case"infinity":return Number.POSITIVE_INFINITY;case"-infinity":return Number.NEGATIVE_INFINITY}if(!i.test(r)){if(n.strict)throw new Error("Invalid JSON");return r}try{if(o.test(r)||u.test(r)){if(n.strict)throw new Error("Possible prototype pollution");return JSON.parse(r,c)}return JSON.parse(r)}catch(e){if(n.strict)throw e;return t}}export function safeParse(t,n={}){return parse(t,{...n,strict:!0})}
@@ -3,10 +3,41 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- Object.defineProperty(exports, "stripComments", {
7
- enumerable: true,
8
- get: function () {
9
- return _jsoncParser.stripComments;
6
+ exports.stripComments = stripComments;
7
+ var _base = require("@stryke/types/base");
8
+ const h = Symbol("singleComment"),
9
+ a = Symbol("multiComment");
10
+ function d() {
11
+ return "";
12
+ }
13
+ function C(t, f, c) {
14
+ return t.slice(f, c).replace(/\S/g, " ");
15
+ }
16
+ function y(t, f) {
17
+ let c = f - 1,
18
+ o = 0;
19
+ for (; t[c] === "\\";) c -= 1, o += 1;
20
+ return !!(o % 2);
21
+ }
22
+ function stripComments(t, {
23
+ whitespace: f = !0,
24
+ trailingCommas: c = !1
25
+ } = {}) {
26
+ if (typeof t != "string") throw new TypeError(`Expected argument \`jsonString\` to be a \`string\`, got \`${typeof t}\``);
27
+ const o = f ? C : d;
28
+ let g = !1,
29
+ i = !1,
30
+ s = 0,
31
+ n = "",
32
+ b = "",
33
+ l = -1;
34
+ for (let e = 0; e < t.length; e++) {
35
+ const r = t[e],
36
+ m = t[e + 1];
37
+ !i && r === '"' && (y(t, e) || (g = !g)), !g && (!i && r + (m ?? _base.EMPTY_STRING) === "//" ? (n += t.slice(s, e), s = e, i = h, e++) : i === h && r + (m ?? _base.EMPTY_STRING) === `\r
38
+ ` ? (e++, i = !1, n += o(t, s, e), s = e) : i === h && r === `
39
+ ` ? (i = !1, n += o(t, s, e), s = e) : !i && r + (m ?? _base.EMPTY_STRING) === "/*" ? (n += t.slice(s, e), s = e, i = a, e++) : i === a && r + (m ?? _base.EMPTY_STRING) === "*/" ? (e++, i = !1, n += o(t, s, e + 1), s = e + 1) : c && !i && (l !== -1 ? r === "}" || r === "]" ? (n += t.slice(s, e), b += o(n, 0, 1) + n.slice(1), n = "", s = e, l = -1) : r !== " " && r !== " " && r !== "\r" && r !== `
40
+ ` && (n += t.slice(s, e), s = e, l = -1) : r === "," && (b += n + t.slice(s, e), n = "", s = e, l = e)));
10
41
  }
11
- });
12
- var _jsoncParser = require("jsonc-parser");
42
+ return b + n + (i ? o(t.slice(s)) : t.slice(s));
43
+ }
@@ -1 +1,4 @@
1
- export { stripComments } from "jsonc-parser";
1
+ export declare function stripComments(value: string, { whitespace, trailingCommas }?: {
2
+ whitespace?: boolean | undefined;
3
+ trailingCommas?: boolean | undefined;
4
+ }): string;
@@ -1 +1,4 @@
1
- export{stripComments}from"jsonc-parser";
1
+ import{EMPTY_STRING as p}from"@stryke/types/base";const h=Symbol("singleComment"),a=Symbol("multiComment");function d(){return""}function C(t,f,c){return t.slice(f,c).replace(/\S/g," ")}function y(t,f){let c=f-1,o=0;for(;t[c]==="\\";)c-=1,o+=1;return!!(o%2)}export function stripComments(t,{whitespace:f=!0,trailingCommas:c=!1}={}){if(typeof t!="string")throw new TypeError(`Expected argument \`jsonString\` to be a \`string\`, got \`${typeof t}\``);const o=f?C:d;let g=!1,i=!1,s=0,n="",b="",l=-1;for(let e=0;e<t.length;e++){const r=t[e],m=t[e+1];!i&&r==='"'&&(y(t,e)||(g=!g)),!g&&(!i&&r+(m??p)==="//"?(n+=t.slice(s,e),s=e,i=h,e++):i===h&&r+(m??p)===`\r
2
+ `?(e++,i=!1,n+=o(t,s,e),s=e):i===h&&r===`
3
+ `?(i=!1,n+=o(t,s,e),s=e):!i&&r+(m??p)==="/*"?(n+=t.slice(s,e),s=e,i=a,e++):i===a&&r+(m??p)==="*/"?(e++,i=!1,n+=o(t,s,e+1),s=e+1):c&&!i&&(l!==-1?r==="}"||r==="]"?(n+=t.slice(s,e),b+=o(n,0,1)+n.slice(1),n="",s=e,l=-1):r!==" "&&r!==" "&&r!=="\r"&&r!==`
4
+ `&&(n+=t.slice(s,e),s=e,l=-1):r===","&&(b+=n+t.slice(s,e),n="",s=e,l=e)))}return b+n+(i?o(t.slice(s)):t.slice(s))}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stryke/json",
3
- "version": "0.8.3",
3
+ "version": "0.9.0",
4
4
  "type": "module",
5
5
  "description": "A package containing JSON parsing/stringify utilities used by Storm Software.",
6
6
  "repository": {
@@ -11,7 +11,6 @@
11
11
  "private": false,
12
12
  "dependencies": {
13
13
  "buffer": "6.0.3",
14
- "jsonc-parser": "3.3.1",
15
14
  "lines-and-columns": "2.0.4",
16
15
  "superjson": "2.2.2",
17
16
  "@stryke/type-checks": "^0.3.3",