@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/dist/utils/parse.cjs
CHANGED
|
@@ -1 +1,52 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_utils_strip_comments = require('./strip-comments.cjs');
|
|
2
|
+
|
|
3
|
+
//#region src/utils/parse.ts
|
|
4
|
+
const suspectProtoRx = /"(?:_|\\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*:/;
|
|
5
|
+
const suspectConstructorRx = /"(?: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*:/;
|
|
6
|
+
const JsonSigRx = /^\s*["[{]|^\s*-?\d{1,16}(?:\.\d{1,17})?(?:E[+-]?\d+)?\s*$/i;
|
|
7
|
+
function jsonParseTransform(key, value) {
|
|
8
|
+
if (key === "__proto__" || key === "constructor" && value && typeof value === "object" && "prototype" in value) {
|
|
9
|
+
console.warn(`Dropping "${key}" key to prevent prototype pollution.`);
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
return value;
|
|
13
|
+
}
|
|
14
|
+
function parse(value, options = {}) {
|
|
15
|
+
if (typeof value !== "string") return value;
|
|
16
|
+
let stripped = require_utils_strip_comments.stripComments(value);
|
|
17
|
+
if (stripped[0] === "\"" && stripped[stripped.length - 1] === "\"" && !stripped.includes("\\")) return stripped.slice(1, -1);
|
|
18
|
+
stripped = stripped.trim();
|
|
19
|
+
if (stripped.length <= 9) switch (stripped.toLowerCase()) {
|
|
20
|
+
case "true": return true;
|
|
21
|
+
case "false": return false;
|
|
22
|
+
case "undefined": return;
|
|
23
|
+
case "null": return null;
|
|
24
|
+
case "nan": return NaN;
|
|
25
|
+
case "infinity": return Number.POSITIVE_INFINITY;
|
|
26
|
+
case "-infinity": return Number.NEGATIVE_INFINITY;
|
|
27
|
+
}
|
|
28
|
+
if (!JsonSigRx.test(stripped)) {
|
|
29
|
+
if (options.strict) throw new Error("Invalid JSON");
|
|
30
|
+
return stripped;
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
if (suspectProtoRx.test(stripped) || suspectConstructorRx.test(stripped)) {
|
|
34
|
+
if (options.strict) throw new Error("Possible prototype pollution");
|
|
35
|
+
return JSON.parse(stripped, jsonParseTransform);
|
|
36
|
+
}
|
|
37
|
+
return JSON.parse(stripped);
|
|
38
|
+
} catch (error) {
|
|
39
|
+
if (options.strict) throw error;
|
|
40
|
+
return value;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function safeParse(value, options = {}) {
|
|
44
|
+
return parse(value, {
|
|
45
|
+
...options,
|
|
46
|
+
strict: true
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
//#endregion
|
|
51
|
+
exports.parse = parse;
|
|
52
|
+
exports.safeParse = safeParse;
|
package/dist/utils/parse.mjs
CHANGED
|
@@ -1,2 +1,52 @@
|
|
|
1
|
-
import{stripComments
|
|
1
|
+
import { stripComments } from "./strip-comments.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/utils/parse.ts
|
|
4
|
+
const suspectProtoRx = /"(?:_|\\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*:/;
|
|
5
|
+
const suspectConstructorRx = /"(?: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*:/;
|
|
6
|
+
const JsonSigRx = /^\s*["[{]|^\s*-?\d{1,16}(?:\.\d{1,17})?(?:E[+-]?\d+)?\s*$/i;
|
|
7
|
+
function jsonParseTransform(key, value) {
|
|
8
|
+
if (key === "__proto__" || key === "constructor" && value && typeof value === "object" && "prototype" in value) {
|
|
9
|
+
console.warn(`Dropping "${key}" key to prevent prototype pollution.`);
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
return value;
|
|
13
|
+
}
|
|
14
|
+
function parse(value, options = {}) {
|
|
15
|
+
if (typeof value !== "string") return value;
|
|
16
|
+
let stripped = stripComments(value);
|
|
17
|
+
if (stripped[0] === "\"" && stripped[stripped.length - 1] === "\"" && !stripped.includes("\\")) return stripped.slice(1, -1);
|
|
18
|
+
stripped = stripped.trim();
|
|
19
|
+
if (stripped.length <= 9) switch (stripped.toLowerCase()) {
|
|
20
|
+
case "true": return true;
|
|
21
|
+
case "false": return false;
|
|
22
|
+
case "undefined": return;
|
|
23
|
+
case "null": return null;
|
|
24
|
+
case "nan": return NaN;
|
|
25
|
+
case "infinity": return Number.POSITIVE_INFINITY;
|
|
26
|
+
case "-infinity": return Number.NEGATIVE_INFINITY;
|
|
27
|
+
}
|
|
28
|
+
if (!JsonSigRx.test(stripped)) {
|
|
29
|
+
if (options.strict) throw new Error("Invalid JSON");
|
|
30
|
+
return stripped;
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
if (suspectProtoRx.test(stripped) || suspectConstructorRx.test(stripped)) {
|
|
34
|
+
if (options.strict) throw new Error("Possible prototype pollution");
|
|
35
|
+
return JSON.parse(stripped, jsonParseTransform);
|
|
36
|
+
}
|
|
37
|
+
return JSON.parse(stripped);
|
|
38
|
+
} catch (error) {
|
|
39
|
+
if (options.strict) throw error;
|
|
40
|
+
return value;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function safeParse(value, options = {}) {
|
|
44
|
+
return parse(value, {
|
|
45
|
+
...options,
|
|
46
|
+
strict: true
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
//#endregion
|
|
51
|
+
export { parse, safeParse };
|
|
2
52
|
//# sourceMappingURL=parse.mjs.map
|
package/dist/utils/parse.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse.mjs","names":[],"sources":["../../src/utils/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 { stripComments } from \"./strip-comments\";\n\n// https://github.com/fastify/secure-json-parse\n// https://github.com/hapijs/bourne\nconst suspectProtoRx =\n /\"(?:_|\\\\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*:/;\nconst suspectConstructorRx =\n /\"(?: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*:/;\n\nconst JsonSigRx = /^\\s*[\"[{]|^\\s*-?\\d{1,16}(?:\\.\\d{1,17})?(?:E[+-]?\\d+)?\\s*$/i;\n\nfunction jsonParseTransform(key: string, value: any): any {\n if (\n key === \"__proto__\" ||\n (key === \"constructor\" &&\n value &&\n typeof value === \"object\" &&\n \"prototype\" in value)\n ) {\n // eslint-disable-next-line no-console\n console.warn(`Dropping \"${key}\" key to prevent prototype pollution.`);\n return;\n }\n return value;\n}\n\nexport interface Options {\n strict?: boolean;\n}\n\nexport function parse<T = unknown>(value: any, options: Options = {}): T {\n if (typeof value !== \"string\") {\n return value;\n }\n\n let stripped = stripComments(value);\n\n if (\n stripped[0] === '\"' &&\n stripped[stripped.length - 1] === '\"' &&\n !stripped.includes(\"\\\\\")\n ) {\n return stripped.slice(1, -1) as T;\n }\n\n stripped = stripped.trim();\n\n if (stripped.length <= 9) {\n switch (stripped.toLowerCase()) {\n case \"true\": {\n return true as T;\n }\n case \"false\": {\n return false as T;\n }\n case \"undefined\": {\n return undefined as T;\n }\n case \"null\": {\n return null as T;\n }\n case \"nan\": {\n return Number.NaN as T;\n }\n case \"infinity\": {\n return Number.POSITIVE_INFINITY as T;\n }\n case \"-infinity\": {\n return Number.NEGATIVE_INFINITY as T;\n }\n }\n }\n\n if (!JsonSigRx.test(stripped)) {\n if (options.strict) {\n throw new Error(\"Invalid JSON\");\n }\n return stripped as T;\n }\n\n try {\n if (suspectProtoRx.test(stripped) || suspectConstructorRx.test(stripped)) {\n if (options.strict) {\n throw new Error(\"Possible prototype pollution\");\n }\n return JSON.parse(stripped, jsonParseTransform);\n }\n\n return JSON.parse(stripped);\n } catch (error) {\n if (options.strict) {\n throw error;\n }\n return value as T;\n }\n}\n\nexport function safeParse<T = unknown>(value: any, options: Options = {}): T {\n return parse<T>(value, { ...options, strict: true });\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"parse.mjs","names":[],"sources":["../../src/utils/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 { stripComments } from \"./strip-comments\";\n\n// https://github.com/fastify/secure-json-parse\n// https://github.com/hapijs/bourne\nconst suspectProtoRx =\n /\"(?:_|\\\\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*:/;\nconst suspectConstructorRx =\n /\"(?: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*:/;\n\nconst JsonSigRx = /^\\s*[\"[{]|^\\s*-?\\d{1,16}(?:\\.\\d{1,17})?(?:E[+-]?\\d+)?\\s*$/i;\n\nfunction jsonParseTransform(key: string, value: any): any {\n if (\n key === \"__proto__\" ||\n (key === \"constructor\" &&\n value &&\n typeof value === \"object\" &&\n \"prototype\" in value)\n ) {\n // eslint-disable-next-line no-console\n console.warn(`Dropping \"${key}\" key to prevent prototype pollution.`);\n return;\n }\n return value;\n}\n\nexport interface Options {\n strict?: boolean;\n}\n\nexport function parse<T = unknown>(value: any, options: Options = {}): T {\n if (typeof value !== \"string\") {\n return value;\n }\n\n let stripped = stripComments(value);\n\n if (\n stripped[0] === '\"' &&\n stripped[stripped.length - 1] === '\"' &&\n !stripped.includes(\"\\\\\")\n ) {\n return stripped.slice(1, -1) as T;\n }\n\n stripped = stripped.trim();\n\n if (stripped.length <= 9) {\n switch (stripped.toLowerCase()) {\n case \"true\": {\n return true as T;\n }\n case \"false\": {\n return false as T;\n }\n case \"undefined\": {\n return undefined as T;\n }\n case \"null\": {\n return null as T;\n }\n case \"nan\": {\n return Number.NaN as T;\n }\n case \"infinity\": {\n return Number.POSITIVE_INFINITY as T;\n }\n case \"-infinity\": {\n return Number.NEGATIVE_INFINITY as T;\n }\n }\n }\n\n if (!JsonSigRx.test(stripped)) {\n if (options.strict) {\n throw new Error(\"Invalid JSON\");\n }\n return stripped as T;\n }\n\n try {\n if (suspectProtoRx.test(stripped) || suspectConstructorRx.test(stripped)) {\n if (options.strict) {\n throw new Error(\"Possible prototype pollution\");\n }\n return JSON.parse(stripped, jsonParseTransform);\n }\n\n return JSON.parse(stripped);\n } catch (error) {\n if (options.strict) {\n throw error;\n }\n return value as T;\n }\n}\n\nexport function safeParse<T = unknown>(value: any, options: Options = {}): T {\n return parse<T>(value, { ...options, strict: true });\n}\n"],"mappings":";;;AAsBA,MAAM,iBACJ;AACF,MAAM,uBACJ;AAEF,MAAM,YAAY;AAElB,SAAS,mBAAmB,KAAa,OAAiB;AACxD,KACE,QAAQ,eACP,QAAQ,iBACP,SACA,OAAO,UAAU,YACjB,eAAe,OACjB;AAEA,UAAQ,KAAK,aAAa,IAAI,uCAAuC;AACrE;;AAEF,QAAO;;AAOT,SAAgB,MAAmB,OAAY,UAAmB,EAAE,EAAK;AACvE,KAAI,OAAO,UAAU,SACnB,QAAO;CAGT,IAAI,WAAW,cAAc,MAAM;AAEnC,KACE,SAAS,OAAO,QAChB,SAAS,SAAS,SAAS,OAAO,QAClC,CAAC,SAAS,SAAS,KAAK,CAExB,QAAO,SAAS,MAAM,GAAG,GAAG;AAG9B,YAAW,SAAS,MAAM;AAE1B,KAAI,SAAS,UAAU,EACrB,SAAQ,SAAS,aAAa,EAA9B;EACE,KAAK,OACH,QAAO;EAET,KAAK,QACH,QAAO;EAET,KAAK,YACH;EAEF,KAAK,OACH,QAAO;EAET,KAAK,MACH,QAAO;EAET,KAAK,WACH,QAAO,OAAO;EAEhB,KAAK,YACH,QAAO,OAAO;;AAKpB,KAAI,CAAC,UAAU,KAAK,SAAS,EAAE;AAC7B,MAAI,QAAQ,OACV,OAAM,IAAI,MAAM,eAAe;AAEjC,SAAO;;AAGT,KAAI;AACF,MAAI,eAAe,KAAK,SAAS,IAAI,qBAAqB,KAAK,SAAS,EAAE;AACxE,OAAI,QAAQ,OACV,OAAM,IAAI,MAAM,+BAA+B;AAEjD,UAAO,KAAK,MAAM,UAAU,mBAAmB;;AAGjD,SAAO,KAAK,MAAM,SAAS;UACpB,OAAO;AACd,MAAI,QAAQ,OACV,OAAM;AAER,SAAO;;;AAIX,SAAgB,UAAuB,OAAY,UAAmB,EAAE,EAAK;AAC3E,QAAO,MAAS,OAAO;EAAE,GAAG;EAAS,QAAQ;EAAM,CAAC"}
|
package/dist/utils/stringify.cjs
CHANGED
|
@@ -1 +1,68 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_is_number = require('../type-checks/src/is-number.cjs');
|
|
2
|
+
const require_is_undefined = require('../type-checks/src/is-undefined.cjs');
|
|
3
|
+
|
|
4
|
+
//#region src/utils/stringify.ts
|
|
5
|
+
const invalidKeyChars = [
|
|
6
|
+
"@",
|
|
7
|
+
"/",
|
|
8
|
+
"#",
|
|
9
|
+
"$",
|
|
10
|
+
" ",
|
|
11
|
+
":",
|
|
12
|
+
";",
|
|
13
|
+
",",
|
|
14
|
+
".",
|
|
15
|
+
"!",
|
|
16
|
+
"?",
|
|
17
|
+
"&",
|
|
18
|
+
"=",
|
|
19
|
+
"+",
|
|
20
|
+
"-",
|
|
21
|
+
"*",
|
|
22
|
+
"%",
|
|
23
|
+
"^",
|
|
24
|
+
"~",
|
|
25
|
+
"|",
|
|
26
|
+
"\\",
|
|
27
|
+
"\"",
|
|
28
|
+
"'",
|
|
29
|
+
"`",
|
|
30
|
+
"{",
|
|
31
|
+
"}",
|
|
32
|
+
"[",
|
|
33
|
+
"]",
|
|
34
|
+
"(",
|
|
35
|
+
")",
|
|
36
|
+
"<",
|
|
37
|
+
">"
|
|
38
|
+
];
|
|
39
|
+
/**
|
|
40
|
+
* Stringify a value to a JSON-like string.
|
|
41
|
+
*
|
|
42
|
+
* @param value - The value to stringify
|
|
43
|
+
* @param spacing - The spacing to use for the stringification
|
|
44
|
+
* @returns The stringified value
|
|
45
|
+
*/
|
|
46
|
+
const stringify = (value, spacing = 2) => {
|
|
47
|
+
const space = require_is_number.isNumber(spacing) ? " ".repeat(spacing) : spacing;
|
|
48
|
+
switch (value) {
|
|
49
|
+
case null: return "null";
|
|
50
|
+
case void 0: return "\"undefined\"";
|
|
51
|
+
case true: return "true";
|
|
52
|
+
case false: return "false";
|
|
53
|
+
case Number.POSITIVE_INFINITY: return "infinity";
|
|
54
|
+
case Number.NEGATIVE_INFINITY: return "-infinity";
|
|
55
|
+
}
|
|
56
|
+
if (Array.isArray(value)) return `[${space}${value.map((v) => stringify(v, space)).join(`,${space}`)}${space}]`;
|
|
57
|
+
if (value instanceof Uint8Array) return value.toString();
|
|
58
|
+
switch (typeof value) {
|
|
59
|
+
case "number": return `${value}`;
|
|
60
|
+
case "string": return JSON.stringify(value);
|
|
61
|
+
case "object": return `{${space}${Object.keys(value).filter((key) => !require_is_undefined.isUndefined(value[key])).map((key) => `${invalidKeyChars.some((invalidKeyChar) => key.includes(invalidKeyChar)) ? `"${key}"` : key}: ${space}${stringify(value[key], space)}`).join(`,${space}`)}${space}}`;
|
|
62
|
+
default: return "null";
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
//#endregion
|
|
67
|
+
exports.invalidKeyChars = invalidKeyChars;
|
|
68
|
+
exports.stringify = stringify;
|
package/dist/utils/stringify.mjs
CHANGED
|
@@ -1,2 +1,68 @@
|
|
|
1
|
-
import{isNumber
|
|
1
|
+
import { isNumber } from "../type-checks/src/is-number.mjs";
|
|
2
|
+
import { isUndefined } from "../type-checks/src/is-undefined.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/utils/stringify.ts
|
|
5
|
+
const invalidKeyChars = [
|
|
6
|
+
"@",
|
|
7
|
+
"/",
|
|
8
|
+
"#",
|
|
9
|
+
"$",
|
|
10
|
+
" ",
|
|
11
|
+
":",
|
|
12
|
+
";",
|
|
13
|
+
",",
|
|
14
|
+
".",
|
|
15
|
+
"!",
|
|
16
|
+
"?",
|
|
17
|
+
"&",
|
|
18
|
+
"=",
|
|
19
|
+
"+",
|
|
20
|
+
"-",
|
|
21
|
+
"*",
|
|
22
|
+
"%",
|
|
23
|
+
"^",
|
|
24
|
+
"~",
|
|
25
|
+
"|",
|
|
26
|
+
"\\",
|
|
27
|
+
"\"",
|
|
28
|
+
"'",
|
|
29
|
+
"`",
|
|
30
|
+
"{",
|
|
31
|
+
"}",
|
|
32
|
+
"[",
|
|
33
|
+
"]",
|
|
34
|
+
"(",
|
|
35
|
+
")",
|
|
36
|
+
"<",
|
|
37
|
+
">"
|
|
38
|
+
];
|
|
39
|
+
/**
|
|
40
|
+
* Stringify a value to a JSON-like string.
|
|
41
|
+
*
|
|
42
|
+
* @param value - The value to stringify
|
|
43
|
+
* @param spacing - The spacing to use for the stringification
|
|
44
|
+
* @returns The stringified value
|
|
45
|
+
*/
|
|
46
|
+
const stringify = (value, spacing = 2) => {
|
|
47
|
+
const space = isNumber(spacing) ? " ".repeat(spacing) : spacing;
|
|
48
|
+
switch (value) {
|
|
49
|
+
case null: return "null";
|
|
50
|
+
case void 0: return "\"undefined\"";
|
|
51
|
+
case true: return "true";
|
|
52
|
+
case false: return "false";
|
|
53
|
+
case Number.POSITIVE_INFINITY: return "infinity";
|
|
54
|
+
case Number.NEGATIVE_INFINITY: return "-infinity";
|
|
55
|
+
}
|
|
56
|
+
if (Array.isArray(value)) return `[${space}${value.map((v) => stringify(v, space)).join(`,${space}`)}${space}]`;
|
|
57
|
+
if (value instanceof Uint8Array) return value.toString();
|
|
58
|
+
switch (typeof value) {
|
|
59
|
+
case "number": return `${value}`;
|
|
60
|
+
case "string": return JSON.stringify(value);
|
|
61
|
+
case "object": return `{${space}${Object.keys(value).filter((key) => !isUndefined(value[key])).map((key) => `${invalidKeyChars.some((invalidKeyChar) => key.includes(invalidKeyChar)) ? `"${key}"` : key}: ${space}${stringify(value[key], space)}`).join(`,${space}`)}${space}}`;
|
|
62
|
+
default: return "null";
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
//#endregion
|
|
67
|
+
export { invalidKeyChars, stringify };
|
|
2
68
|
//# sourceMappingURL=stringify.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stringify.mjs","names":[],"sources":["../../src/utils/stringify.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 { isUndefined } from \"@stryke/type-checks/is-undefined\";\n\nexport const invalidKeyChars = [\n \"@\",\n \"/\",\n \"#\",\n \"$\",\n \" \",\n \":\",\n \";\",\n \",\",\n \".\",\n \"!\",\n \"?\",\n \"&\",\n \"=\",\n \"+\",\n \"-\",\n \"*\",\n \"%\",\n \"^\",\n \"~\",\n \"|\",\n \"\\\\\",\n '\"',\n \"'\",\n \"`\",\n \"{\",\n \"}\",\n \"[\",\n \"]\",\n \"(\",\n \")\",\n \"<\",\n \">\"\n] as const;\n\n/**\n * Stringify a value to a JSON-like string.\n *\n * @param value - The value to stringify\n * @param spacing - The spacing to use for the stringification\n * @returns The stringified value\n */\nexport const stringify = (\n value: unknown,\n spacing: string | number = 2\n): string => {\n const space = isNumber(spacing) ? \" \".repeat(spacing) : spacing;\n\n switch (value) {\n case null: {\n return \"null\";\n }\n case undefined: {\n return '\"undefined\"';\n }\n case true: {\n return \"true\";\n }\n case false: {\n return \"false\";\n }\n case Number.POSITIVE_INFINITY: {\n return \"infinity\";\n }\n case Number.NEGATIVE_INFINITY: {\n return \"-infinity\";\n }\n }\n\n if (Array.isArray(value)) {\n return `[${space}${value.map(v => stringify(v, space)).join(`,${space}`)}${space}]`;\n }\n if (value instanceof Uint8Array) {\n return value.toString();\n }\n\n // eslint-disable-next-line ts/switch-exhaustiveness-check\n switch (typeof value) {\n case \"number\": {\n return `${value}`;\n }\n case \"string\": {\n return JSON.stringify(value);\n }\n case \"object\": {\n const keys = Object.keys(value as object).filter(\n key => !isUndefined((value as any)[key])\n );\n\n return `{${space}${keys\n .map(\n key =>\n `${invalidKeyChars.some(invalidKeyChar => key.includes(invalidKeyChar)) ? `\"${key}\"` : key}: ${space}${stringify((value as any)[key], space)}`\n )\n .join(`,${space}`)}${space}}`;\n }\n default:\n return \"null\";\n }\n};\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"stringify.mjs","names":[],"sources":["../../src/utils/stringify.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 { isUndefined } from \"@stryke/type-checks/is-undefined\";\n\nexport const invalidKeyChars = [\n \"@\",\n \"/\",\n \"#\",\n \"$\",\n \" \",\n \":\",\n \";\",\n \",\",\n \".\",\n \"!\",\n \"?\",\n \"&\",\n \"=\",\n \"+\",\n \"-\",\n \"*\",\n \"%\",\n \"^\",\n \"~\",\n \"|\",\n \"\\\\\",\n '\"',\n \"'\",\n \"`\",\n \"{\",\n \"}\",\n \"[\",\n \"]\",\n \"(\",\n \")\",\n \"<\",\n \">\"\n] as const;\n\n/**\n * Stringify a value to a JSON-like string.\n *\n * @param value - The value to stringify\n * @param spacing - The spacing to use for the stringification\n * @returns The stringified value\n */\nexport const stringify = (\n value: unknown,\n spacing: string | number = 2\n): string => {\n const space = isNumber(spacing) ? \" \".repeat(spacing) : spacing;\n\n switch (value) {\n case null: {\n return \"null\";\n }\n case undefined: {\n return '\"undefined\"';\n }\n case true: {\n return \"true\";\n }\n case false: {\n return \"false\";\n }\n case Number.POSITIVE_INFINITY: {\n return \"infinity\";\n }\n case Number.NEGATIVE_INFINITY: {\n return \"-infinity\";\n }\n }\n\n if (Array.isArray(value)) {\n return `[${space}${value.map(v => stringify(v, space)).join(`,${space}`)}${space}]`;\n }\n if (value instanceof Uint8Array) {\n return value.toString();\n }\n\n // eslint-disable-next-line ts/switch-exhaustiveness-check\n switch (typeof value) {\n case \"number\": {\n return `${value}`;\n }\n case \"string\": {\n return JSON.stringify(value);\n }\n case \"object\": {\n const keys = Object.keys(value as object).filter(\n key => !isUndefined((value as any)[key])\n );\n\n return `{${space}${keys\n .map(\n key =>\n `${invalidKeyChars.some(invalidKeyChar => key.includes(invalidKeyChar)) ? `\"${key}\"` : key}: ${space}${stringify((value as any)[key], space)}`\n )\n .join(`,${space}`)}${space}}`;\n }\n default:\n return \"null\";\n }\n};\n"],"mappings":";;;;AAqBA,MAAa,kBAAkB;CAC7B;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD;;;;;;;;AASD,MAAa,aACX,OACA,UAA2B,MAChB;CACX,MAAM,QAAQ,SAAS,QAAQ,GAAG,IAAI,OAAO,QAAQ,GAAG;AAExD,SAAQ,OAAR;EACE,KAAK,KACH,QAAO;EAET,KAAK,OACH,QAAO;EAET,KAAK,KACH,QAAO;EAET,KAAK,MACH,QAAO;EAET,KAAK,OAAO,kBACV,QAAO;EAET,KAAK,OAAO,kBACV,QAAO;;AAIX,KAAI,MAAM,QAAQ,MAAM,CACtB,QAAO,IAAI,QAAQ,MAAM,KAAI,MAAK,UAAU,GAAG,MAAM,CAAC,CAAC,KAAK,IAAI,QAAQ,GAAG,MAAM;AAEnF,KAAI,iBAAiB,WACnB,QAAO,MAAM,UAAU;AAIzB,SAAQ,OAAO,OAAf;EACE,KAAK,SACH,QAAO,GAAG;EAEZ,KAAK,SACH,QAAO,KAAK,UAAU,MAAM;EAE9B,KAAK,SAKH,QAAO,IAAI,QAJE,OAAO,KAAK,MAAgB,CAAC,QACxC,QAAO,CAAC,YAAa,MAAc,KAAK,CACzC,CAGE,KACC,QACE,GAAG,gBAAgB,MAAK,mBAAkB,IAAI,SAAS,eAAe,CAAC,GAAG,IAAI,IAAI,KAAK,IAAI,IAAI,QAAQ,UAAW,MAAc,MAAM,MAAM,GAC/I,CACA,KAAK,IAAI,QAAQ,GAAG,MAAM;EAE/B,QACE,QAAO"}
|
|
@@ -1,4 +1,86 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
const require_base = require('../types/src/base.cjs');
|
|
2
|
+
|
|
3
|
+
//#region src/utils/strip-comments.ts
|
|
4
|
+
const singleComment = Symbol("singleComment");
|
|
5
|
+
const multiComment = Symbol("multiComment");
|
|
6
|
+
function stripWithoutWhitespace() {
|
|
7
|
+
return "";
|
|
8
|
+
}
|
|
9
|
+
function stripWithWhitespace(value, start, end) {
|
|
10
|
+
return value.slice(start, end).replace(/\S/g, " ");
|
|
11
|
+
}
|
|
12
|
+
function isEscaped(value, quotePosition) {
|
|
13
|
+
let index = quotePosition - 1;
|
|
14
|
+
let backslashCount = 0;
|
|
15
|
+
while (value[index] === "\\") {
|
|
16
|
+
index -= 1;
|
|
17
|
+
backslashCount += 1;
|
|
18
|
+
}
|
|
19
|
+
return Boolean(backslashCount % 2);
|
|
20
|
+
}
|
|
21
|
+
function stripComments(value, { whitespace = true, trailingCommas = false } = {}) {
|
|
22
|
+
if (typeof value !== "string") throw new TypeError(`Expected argument \`jsonString\` to be a \`string\`, got \`${typeof value}\``);
|
|
23
|
+
const strip = whitespace ? stripWithWhitespace : stripWithoutWhitespace;
|
|
24
|
+
let isInsideString = false;
|
|
25
|
+
let isInsideComment = false;
|
|
26
|
+
let offset = 0;
|
|
27
|
+
let buffer = "";
|
|
28
|
+
let result = "";
|
|
29
|
+
let commaIndex = -1;
|
|
30
|
+
for (let index = 0; index < value.length; index++) {
|
|
31
|
+
const currentCharacter = value[index];
|
|
32
|
+
const nextCharacter = value[index + 1];
|
|
33
|
+
if (!isInsideComment && currentCharacter === "\"") {
|
|
34
|
+
if (!isEscaped(value, index)) isInsideString = !isInsideString;
|
|
35
|
+
}
|
|
36
|
+
if (isInsideString) continue;
|
|
37
|
+
if (!isInsideComment && currentCharacter + (nextCharacter ?? require_base.EMPTY_STRING) === "//") {
|
|
38
|
+
buffer += value.slice(offset, index);
|
|
39
|
+
offset = index;
|
|
40
|
+
isInsideComment = singleComment;
|
|
41
|
+
index++;
|
|
42
|
+
} else if (isInsideComment === singleComment && currentCharacter + (nextCharacter ?? require_base.EMPTY_STRING) === "\r\n") {
|
|
43
|
+
index++;
|
|
44
|
+
isInsideComment = false;
|
|
45
|
+
buffer += strip(value, offset, index);
|
|
46
|
+
offset = index;
|
|
47
|
+
} else if (isInsideComment === singleComment && currentCharacter === "\n") {
|
|
48
|
+
isInsideComment = false;
|
|
49
|
+
buffer += strip(value, offset, index);
|
|
50
|
+
offset = index;
|
|
51
|
+
} else if (!isInsideComment && currentCharacter + (nextCharacter ?? require_base.EMPTY_STRING) === "/*") {
|
|
52
|
+
buffer += value.slice(offset, index);
|
|
53
|
+
offset = index;
|
|
54
|
+
isInsideComment = multiComment;
|
|
55
|
+
index++;
|
|
56
|
+
} else if (isInsideComment === multiComment && currentCharacter + (nextCharacter ?? require_base.EMPTY_STRING) === "*/") {
|
|
57
|
+
index++;
|
|
58
|
+
isInsideComment = false;
|
|
59
|
+
buffer += strip(value, offset, index + 1);
|
|
60
|
+
offset = index + 1;
|
|
61
|
+
} else if (trailingCommas && !isInsideComment) {
|
|
62
|
+
if (commaIndex !== -1) {
|
|
63
|
+
if (currentCharacter === "}" || currentCharacter === "]") {
|
|
64
|
+
buffer += value.slice(offset, index);
|
|
65
|
+
result += strip(buffer, 0, 1) + buffer.slice(1);
|
|
66
|
+
buffer = "";
|
|
67
|
+
offset = index;
|
|
68
|
+
commaIndex = -1;
|
|
69
|
+
} else if (currentCharacter !== " " && currentCharacter !== " " && currentCharacter !== "\r" && currentCharacter !== "\n") {
|
|
70
|
+
buffer += value.slice(offset, index);
|
|
71
|
+
offset = index;
|
|
72
|
+
commaIndex = -1;
|
|
73
|
+
}
|
|
74
|
+
} else if (currentCharacter === ",") {
|
|
75
|
+
result += buffer + value.slice(offset, index);
|
|
76
|
+
buffer = "";
|
|
77
|
+
offset = index;
|
|
78
|
+
commaIndex = index;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return result + buffer + (isInsideComment ? strip(value.slice(offset)) : value.slice(offset));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
//#endregion
|
|
86
|
+
exports.stripComments = stripComments;
|
|
@@ -1,5 +1,87 @@
|
|
|
1
|
-
import{EMPTY_STRING
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { EMPTY_STRING } from "../types/src/base.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/utils/strip-comments.ts
|
|
4
|
+
const singleComment = Symbol("singleComment");
|
|
5
|
+
const multiComment = Symbol("multiComment");
|
|
6
|
+
function stripWithoutWhitespace() {
|
|
7
|
+
return "";
|
|
8
|
+
}
|
|
9
|
+
function stripWithWhitespace(value, start, end) {
|
|
10
|
+
return value.slice(start, end).replace(/\S/g, " ");
|
|
11
|
+
}
|
|
12
|
+
function isEscaped(value, quotePosition) {
|
|
13
|
+
let index = quotePosition - 1;
|
|
14
|
+
let backslashCount = 0;
|
|
15
|
+
while (value[index] === "\\") {
|
|
16
|
+
index -= 1;
|
|
17
|
+
backslashCount += 1;
|
|
18
|
+
}
|
|
19
|
+
return Boolean(backslashCount % 2);
|
|
20
|
+
}
|
|
21
|
+
function stripComments(value, { whitespace = true, trailingCommas = false } = {}) {
|
|
22
|
+
if (typeof value !== "string") throw new TypeError(`Expected argument \`jsonString\` to be a \`string\`, got \`${typeof value}\``);
|
|
23
|
+
const strip = whitespace ? stripWithWhitespace : stripWithoutWhitespace;
|
|
24
|
+
let isInsideString = false;
|
|
25
|
+
let isInsideComment = false;
|
|
26
|
+
let offset = 0;
|
|
27
|
+
let buffer = "";
|
|
28
|
+
let result = "";
|
|
29
|
+
let commaIndex = -1;
|
|
30
|
+
for (let index = 0; index < value.length; index++) {
|
|
31
|
+
const currentCharacter = value[index];
|
|
32
|
+
const nextCharacter = value[index + 1];
|
|
33
|
+
if (!isInsideComment && currentCharacter === "\"") {
|
|
34
|
+
if (!isEscaped(value, index)) isInsideString = !isInsideString;
|
|
35
|
+
}
|
|
36
|
+
if (isInsideString) continue;
|
|
37
|
+
if (!isInsideComment && currentCharacter + (nextCharacter ?? EMPTY_STRING) === "//") {
|
|
38
|
+
buffer += value.slice(offset, index);
|
|
39
|
+
offset = index;
|
|
40
|
+
isInsideComment = singleComment;
|
|
41
|
+
index++;
|
|
42
|
+
} else if (isInsideComment === singleComment && currentCharacter + (nextCharacter ?? EMPTY_STRING) === "\r\n") {
|
|
43
|
+
index++;
|
|
44
|
+
isInsideComment = false;
|
|
45
|
+
buffer += strip(value, offset, index);
|
|
46
|
+
offset = index;
|
|
47
|
+
} else if (isInsideComment === singleComment && currentCharacter === "\n") {
|
|
48
|
+
isInsideComment = false;
|
|
49
|
+
buffer += strip(value, offset, index);
|
|
50
|
+
offset = index;
|
|
51
|
+
} else if (!isInsideComment && currentCharacter + (nextCharacter ?? EMPTY_STRING) === "/*") {
|
|
52
|
+
buffer += value.slice(offset, index);
|
|
53
|
+
offset = index;
|
|
54
|
+
isInsideComment = multiComment;
|
|
55
|
+
index++;
|
|
56
|
+
} else if (isInsideComment === multiComment && currentCharacter + (nextCharacter ?? EMPTY_STRING) === "*/") {
|
|
57
|
+
index++;
|
|
58
|
+
isInsideComment = false;
|
|
59
|
+
buffer += strip(value, offset, index + 1);
|
|
60
|
+
offset = index + 1;
|
|
61
|
+
} else if (trailingCommas && !isInsideComment) {
|
|
62
|
+
if (commaIndex !== -1) {
|
|
63
|
+
if (currentCharacter === "}" || currentCharacter === "]") {
|
|
64
|
+
buffer += value.slice(offset, index);
|
|
65
|
+
result += strip(buffer, 0, 1) + buffer.slice(1);
|
|
66
|
+
buffer = "";
|
|
67
|
+
offset = index;
|
|
68
|
+
commaIndex = -1;
|
|
69
|
+
} else if (currentCharacter !== " " && currentCharacter !== " " && currentCharacter !== "\r" && currentCharacter !== "\n") {
|
|
70
|
+
buffer += value.slice(offset, index);
|
|
71
|
+
offset = index;
|
|
72
|
+
commaIndex = -1;
|
|
73
|
+
}
|
|
74
|
+
} else if (currentCharacter === ",") {
|
|
75
|
+
result += buffer + value.slice(offset, index);
|
|
76
|
+
buffer = "";
|
|
77
|
+
offset = index;
|
|
78
|
+
commaIndex = index;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return result + buffer + (isInsideComment ? strip(value.slice(offset)) : value.slice(offset));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
//#endregion
|
|
86
|
+
export { stripComments };
|
|
5
87
|
//# sourceMappingURL=strip-comments.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"strip-comments.mjs","names":["isInsideString: boolean | symbol","isInsideComment: boolean | symbol"],"sources":["../../src/utils/strip-comments.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 { EMPTY_STRING } from \"@stryke/types/base\";\n\nconst singleComment = Symbol(\"singleComment\");\nconst multiComment = Symbol(\"multiComment\");\n\nfunction stripWithoutWhitespace() {\n return \"\";\n}\nfunction stripWithWhitespace(value: string, start?: number, end?: number) {\n return value.slice(start, end).replace(/\\S/g, \" \");\n}\n\nfunction isEscaped(value: string, quotePosition: number) {\n let index = quotePosition - 1;\n let backslashCount = 0;\n while (value[index] === \"\\\\\") {\n index -= 1;\n backslashCount += 1;\n }\n\n return Boolean(backslashCount % 2);\n}\n\nexport function stripComments(\n value: string,\n { whitespace = true, trailingCommas = false } = {}\n) {\n if (typeof value !== \"string\") {\n throw new TypeError(\n `Expected argument \\`jsonString\\` to be a \\`string\\`, got \\`${typeof value}\\``\n );\n }\n\n const strip = whitespace ? stripWithWhitespace : stripWithoutWhitespace;\n let isInsideString: boolean | symbol = false;\n let isInsideComment: boolean | symbol = false;\n let offset = 0;\n let buffer = \"\";\n let result = \"\";\n let commaIndex = -1;\n for (let index = 0; index < value.length; index++) {\n const currentCharacter = value[index];\n const nextCharacter = value[index + 1];\n if (!isInsideComment && currentCharacter === '\"') {\n const escaped = isEscaped(value, index);\n if (!escaped) {\n isInsideString = !isInsideString;\n }\n }\n if (isInsideString) {\n continue;\n }\n if (\n !isInsideComment &&\n currentCharacter + (nextCharacter ?? EMPTY_STRING) === \"//\"\n ) {\n buffer += value.slice(offset, index);\n offset = index;\n isInsideComment = singleComment;\n index++;\n } else if (\n isInsideComment === singleComment &&\n currentCharacter + (nextCharacter ?? EMPTY_STRING) === \"\\r\\n\"\n ) {\n index++;\n isInsideComment = false;\n buffer += strip(value, offset, index);\n offset = index;\n } else if (isInsideComment === singleComment && currentCharacter === \"\\n\") {\n isInsideComment = false;\n buffer += strip(value, offset, index);\n offset = index;\n } else if (\n !isInsideComment &&\n currentCharacter + (nextCharacter ?? EMPTY_STRING) === \"/*\"\n ) {\n buffer += value.slice(offset, index);\n offset = index;\n isInsideComment = multiComment;\n index++;\n } else if (\n isInsideComment === multiComment &&\n currentCharacter + (nextCharacter ?? EMPTY_STRING) === \"*/\"\n ) {\n index++;\n isInsideComment = false;\n buffer += strip(value, offset, index + 1);\n offset = index + 1;\n } else if (trailingCommas && !isInsideComment) {\n if (commaIndex !== -1) {\n if (currentCharacter === \"}\" || currentCharacter === \"]\") {\n buffer += value.slice(offset, index);\n result += strip(buffer, 0, 1) + buffer.slice(1);\n buffer = \"\";\n offset = index;\n commaIndex = -1;\n } else if (\n currentCharacter !== \" \" &&\n currentCharacter !== \"\t\" &&\n currentCharacter !== \"\\r\" &&\n currentCharacter !== \"\\n\"\n ) {\n buffer += value.slice(offset, index);\n offset = index;\n commaIndex = -1;\n }\n } else if (currentCharacter === \",\") {\n result += buffer + value.slice(offset, index);\n buffer = \"\";\n offset = index;\n commaIndex = index;\n }\n }\n }\n\n return (\n result +\n buffer +\n (isInsideComment ? strip(value.slice(offset)) : value.slice(offset))\n );\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"strip-comments.mjs","names":["isInsideString: boolean | symbol","isInsideComment: boolean | symbol"],"sources":["../../src/utils/strip-comments.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 { EMPTY_STRING } from \"@stryke/types/base\";\n\nconst singleComment = Symbol(\"singleComment\");\nconst multiComment = Symbol(\"multiComment\");\n\nfunction stripWithoutWhitespace() {\n return \"\";\n}\nfunction stripWithWhitespace(value: string, start?: number, end?: number) {\n return value.slice(start, end).replace(/\\S/g, \" \");\n}\n\nfunction isEscaped(value: string, quotePosition: number) {\n let index = quotePosition - 1;\n let backslashCount = 0;\n while (value[index] === \"\\\\\") {\n index -= 1;\n backslashCount += 1;\n }\n\n return Boolean(backslashCount % 2);\n}\n\nexport function stripComments(\n value: string,\n { whitespace = true, trailingCommas = false } = {}\n) {\n if (typeof value !== \"string\") {\n throw new TypeError(\n `Expected argument \\`jsonString\\` to be a \\`string\\`, got \\`${typeof value}\\``\n );\n }\n\n const strip = whitespace ? stripWithWhitespace : stripWithoutWhitespace;\n let isInsideString: boolean | symbol = false;\n let isInsideComment: boolean | symbol = false;\n let offset = 0;\n let buffer = \"\";\n let result = \"\";\n let commaIndex = -1;\n for (let index = 0; index < value.length; index++) {\n const currentCharacter = value[index];\n const nextCharacter = value[index + 1];\n if (!isInsideComment && currentCharacter === '\"') {\n const escaped = isEscaped(value, index);\n if (!escaped) {\n isInsideString = !isInsideString;\n }\n }\n if (isInsideString) {\n continue;\n }\n if (\n !isInsideComment &&\n currentCharacter + (nextCharacter ?? EMPTY_STRING) === \"//\"\n ) {\n buffer += value.slice(offset, index);\n offset = index;\n isInsideComment = singleComment;\n index++;\n } else if (\n isInsideComment === singleComment &&\n currentCharacter + (nextCharacter ?? EMPTY_STRING) === \"\\r\\n\"\n ) {\n index++;\n isInsideComment = false;\n buffer += strip(value, offset, index);\n offset = index;\n } else if (isInsideComment === singleComment && currentCharacter === \"\\n\") {\n isInsideComment = false;\n buffer += strip(value, offset, index);\n offset = index;\n } else if (\n !isInsideComment &&\n currentCharacter + (nextCharacter ?? EMPTY_STRING) === \"/*\"\n ) {\n buffer += value.slice(offset, index);\n offset = index;\n isInsideComment = multiComment;\n index++;\n } else if (\n isInsideComment === multiComment &&\n currentCharacter + (nextCharacter ?? EMPTY_STRING) === \"*/\"\n ) {\n index++;\n isInsideComment = false;\n buffer += strip(value, offset, index + 1);\n offset = index + 1;\n } else if (trailingCommas && !isInsideComment) {\n if (commaIndex !== -1) {\n if (currentCharacter === \"}\" || currentCharacter === \"]\") {\n buffer += value.slice(offset, index);\n result += strip(buffer, 0, 1) + buffer.slice(1);\n buffer = \"\";\n offset = index;\n commaIndex = -1;\n } else if (\n currentCharacter !== \" \" &&\n currentCharacter !== \"\t\" &&\n currentCharacter !== \"\\r\" &&\n currentCharacter !== \"\\n\"\n ) {\n buffer += value.slice(offset, index);\n offset = index;\n commaIndex = -1;\n }\n } else if (currentCharacter === \",\") {\n result += buffer + value.slice(offset, index);\n buffer = \"\";\n offset = index;\n commaIndex = index;\n }\n }\n }\n\n return (\n result +\n buffer +\n (isInsideComment ? strip(value.slice(offset)) : value.slice(offset))\n );\n}\n"],"mappings":";;;AAoBA,MAAM,gBAAgB,OAAO,gBAAgB;AAC7C,MAAM,eAAe,OAAO,eAAe;AAE3C,SAAS,yBAAyB;AAChC,QAAO;;AAET,SAAS,oBAAoB,OAAe,OAAgB,KAAc;AACxE,QAAO,MAAM,MAAM,OAAO,IAAI,CAAC,QAAQ,OAAO,IAAI;;AAGpD,SAAS,UAAU,OAAe,eAAuB;CACvD,IAAI,QAAQ,gBAAgB;CAC5B,IAAI,iBAAiB;AACrB,QAAO,MAAM,WAAW,MAAM;AAC5B,WAAS;AACT,oBAAkB;;AAGpB,QAAO,QAAQ,iBAAiB,EAAE;;AAGpC,SAAgB,cACd,OACA,EAAE,aAAa,MAAM,iBAAiB,UAAU,EAAE,EAClD;AACA,KAAI,OAAO,UAAU,SACnB,OAAM,IAAI,UACR,8DAA8D,OAAO,MAAM,IAC5E;CAGH,MAAM,QAAQ,aAAa,sBAAsB;CACjD,IAAIA,iBAAmC;CACvC,IAAIC,kBAAoC;CACxC,IAAI,SAAS;CACb,IAAI,SAAS;CACb,IAAI,SAAS;CACb,IAAI,aAAa;AACjB,MAAK,IAAI,QAAQ,GAAG,QAAQ,MAAM,QAAQ,SAAS;EACjD,MAAM,mBAAmB,MAAM;EAC/B,MAAM,gBAAgB,MAAM,QAAQ;AACpC,MAAI,CAAC,mBAAmB,qBAAqB,MAE3C;OAAI,CADY,UAAU,OAAO,MAAM,CAErC,kBAAiB,CAAC;;AAGtB,MAAI,eACF;AAEF,MACE,CAAC,mBACD,oBAAoB,iBAAiB,kBAAkB,MACvD;AACA,aAAU,MAAM,MAAM,QAAQ,MAAM;AACpC,YAAS;AACT,qBAAkB;AAClB;aAEA,oBAAoB,iBACpB,oBAAoB,iBAAiB,kBAAkB,QACvD;AACA;AACA,qBAAkB;AAClB,aAAU,MAAM,OAAO,QAAQ,MAAM;AACrC,YAAS;aACA,oBAAoB,iBAAiB,qBAAqB,MAAM;AACzE,qBAAkB;AAClB,aAAU,MAAM,OAAO,QAAQ,MAAM;AACrC,YAAS;aAET,CAAC,mBACD,oBAAoB,iBAAiB,kBAAkB,MACvD;AACA,aAAU,MAAM,MAAM,QAAQ,MAAM;AACpC,YAAS;AACT,qBAAkB;AAClB;aAEA,oBAAoB,gBACpB,oBAAoB,iBAAiB,kBAAkB,MACvD;AACA;AACA,qBAAkB;AAClB,aAAU,MAAM,OAAO,QAAQ,QAAQ,EAAE;AACzC,YAAS,QAAQ;aACR,kBAAkB,CAAC,iBAC5B;OAAI,eAAe,IACjB;QAAI,qBAAqB,OAAO,qBAAqB,KAAK;AACxD,eAAU,MAAM,MAAM,QAAQ,MAAM;AACpC,eAAU,MAAM,QAAQ,GAAG,EAAE,GAAG,OAAO,MAAM,EAAE;AAC/C,cAAS;AACT,cAAS;AACT,kBAAa;eAEb,qBAAqB,OACrB,qBAAqB,OACrB,qBAAqB,QACrB,qBAAqB,MACrB;AACA,eAAU,MAAM,MAAM,QAAQ,MAAM;AACpC,cAAS;AACT,kBAAa;;cAEN,qBAAqB,KAAK;AACnC,cAAU,SAAS,MAAM,MAAM,QAAQ,MAAM;AAC7C,aAAS;AACT,aAAS;AACT,iBAAa;;;;AAKnB,QACE,SACA,UACC,kBAAkB,MAAM,MAAM,MAAM,OAAO,CAAC,GAAG,MAAM,MAAM,OAAO"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stryke/json",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.37",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A package containing JSON parsing/stringify utilities used by Storm Software.",
|
|
6
6
|
"repository": {
|
|
@@ -64,5 +64,5 @@
|
|
|
64
64
|
},
|
|
65
65
|
"devDependencies": { "@types/node": "^24.10.9", "tsdown": "^0.17.2" },
|
|
66
66
|
"publishConfig": { "access": "public" },
|
|
67
|
-
"gitHead": "
|
|
67
|
+
"gitHead": "9c02f25b3bf83a1228a94fc2ef3fcd816dc0f707"
|
|
68
68
|
}
|