@stryke/url 0.3.26 → 0.3.28
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 +14 -0
- package/dist/_virtual/rolldown_runtime.cjs +29 -1
- package/dist/helpers.cjs +28 -1
- package/dist/helpers.mjs +26 -1
- package/dist/helpers.mjs.map +1 -1
- package/dist/index.cjs +5 -1
- package/dist/index.mjs +4 -1
- package/dist/json/src/storm-json.cjs +117 -1
- package/dist/json/src/storm-json.mjs +115 -1
- package/dist/json/src/storm-json.mjs.map +1 -1
- package/dist/json/src/utils/code-frames.cjs +77 -3
- package/dist/json/src/utils/code-frames.mjs +76 -3
- package/dist/json/src/utils/code-frames.mjs.map +1 -1
- package/dist/json/src/utils/parse-error.cjs +34 -1
- package/dist/json/src/utils/parse-error.mjs +33 -1
- package/dist/json/src/utils/parse-error.mjs.map +1 -1
- package/dist/json/src/utils/parse.cjs +45 -1
- package/dist/json/src/utils/parse.mjs +45 -1
- package/dist/json/src/utils/parse.mjs.map +1 -1
- package/dist/json/src/utils/stringify.cjs +67 -1
- package/dist/json/src/utils/stringify.mjs +67 -1
- package/dist/json/src/utils/stringify.mjs.map +1 -1
- package/dist/json/src/utils/strip-comments.cjs +86 -4
- package/dist/json/src/utils/strip-comments.mjs +86 -4
- package/dist/json/src/utils/strip-comments.mjs.map +1 -1
- package/dist/path/src/is-type.cjs +28 -1
- package/dist/path/src/is-type.mjs +28 -1
- package/dist/path/src/is-type.mjs.map +1 -1
- package/dist/path/src/join-paths.cjs +106 -1
- package/dist/path/src/join-paths.mjs +106 -1
- package/dist/path/src/join-paths.mjs.map +1 -1
- package/dist/path/src/regex.cjs +12 -1
- package/dist/path/src/regex.mjs +8 -1
- package/dist/path/src/regex.mjs.map +1 -1
- package/dist/path/src/slash.cjs +15 -1
- package/dist/path/src/slash.mjs +14 -1
- package/dist/path/src/slash.mjs.map +1 -1
- package/dist/storm-url.cjs +227 -1
- package/dist/storm-url.mjs +226 -1
- package/dist/storm-url.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-integer.cjs +14 -1
- package/dist/type-checks/src/is-integer.mjs +14 -1
- package/dist/type-checks/src/is-integer.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-string.cjs +20 -1
- package/dist/type-checks/src/is-set-string.mjs +20 -1
- package/dist/type-checks/src/is-set-string.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.cjs +6 -1
- package/dist/types.mjs +5 -1
- package/dist/types.mjs.map +1 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
# Changelog for Stryke - URL
|
|
4
4
|
|
|
5
|
+
## [0.3.27](https://github.com/storm-software/stryke/releases/tag/url%400.3.27) (01/16/2026)
|
|
6
|
+
|
|
7
|
+
### Updated Dependencies
|
|
8
|
+
|
|
9
|
+
- Updated **type-checks** to **v0.5.19**
|
|
10
|
+
- Updated **json** to **v0.9.37**
|
|
11
|
+
- Updated **path** to **v0.25.1**
|
|
12
|
+
|
|
13
|
+
## [0.3.26](https://github.com/storm-software/stryke/releases/tag/url%400.3.26) (01/16/2026)
|
|
14
|
+
|
|
15
|
+
### Updated Dependencies
|
|
16
|
+
|
|
17
|
+
- Updated **path** to **v0.25.0**
|
|
18
|
+
|
|
5
19
|
## [0.3.25](https://github.com/storm-software/stryke/releases/tag/url%400.3.25) (01/16/2026)
|
|
6
20
|
|
|
7
21
|
### Updated Dependencies
|
|
@@ -1 +1,29 @@
|
|
|
1
|
-
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
13
|
+
__defProp(to, key, {
|
|
14
|
+
get: ((k) => from[k]).bind(null, key),
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
23
|
+
value: mod,
|
|
24
|
+
enumerable: true
|
|
25
|
+
}) : target, mod));
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
|
|
29
|
+
exports.__toESM = __toESM;
|
package/dist/helpers.cjs
CHANGED
|
@@ -1 +1,28 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
|
+
//#region src/helpers.ts
|
|
3
|
+
function formatLocalePath(locale) {
|
|
4
|
+
let result = locale;
|
|
5
|
+
if (result.includes("_")) result = result.replace(/_/g, "-");
|
|
6
|
+
if (result.includes("-")) {
|
|
7
|
+
const parts = result.split("-");
|
|
8
|
+
if (parts.length > 1) {
|
|
9
|
+
const lang = parts[0];
|
|
10
|
+
const region = parts[1];
|
|
11
|
+
if (lang && region) result = `${lang}-${region}`;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return result.toLowerCase();
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Check if a string has a valid URL format.
|
|
18
|
+
*
|
|
19
|
+
* @param str - The string to check.
|
|
20
|
+
* @returns `true` if the string is a valid URL, otherwise `false`.
|
|
21
|
+
*/
|
|
22
|
+
function isValidURL(str) {
|
|
23
|
+
return /(?:https?:\/\/.)?(?:www\.)?[-\w@:%.+~#=]{2,256}\.[a-z]{2,6}\b[-\w@:%+.~#?&/=]*/.test(str);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
//#endregion
|
|
27
|
+
exports.formatLocalePath = formatLocalePath;
|
|
28
|
+
exports.isValidURL = isValidURL;
|
package/dist/helpers.mjs
CHANGED
|
@@ -1,2 +1,27 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/helpers.ts
|
|
2
|
+
function formatLocalePath(locale) {
|
|
3
|
+
let result = locale;
|
|
4
|
+
if (result.includes("_")) result = result.replace(/_/g, "-");
|
|
5
|
+
if (result.includes("-")) {
|
|
6
|
+
const parts = result.split("-");
|
|
7
|
+
if (parts.length > 1) {
|
|
8
|
+
const lang = parts[0];
|
|
9
|
+
const region = parts[1];
|
|
10
|
+
if (lang && region) result = `${lang}-${region}`;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return result.toLowerCase();
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Check if a string has a valid URL format.
|
|
17
|
+
*
|
|
18
|
+
* @param str - The string to check.
|
|
19
|
+
* @returns `true` if the string is a valid URL, otherwise `false`.
|
|
20
|
+
*/
|
|
21
|
+
function isValidURL(str) {
|
|
22
|
+
return /(?:https?:\/\/.)?(?:www\.)?[-\w@:%.+~#=]{2,256}\.[a-z]{2,6}\b[-\w@:%+.~#?&/=]*/.test(str);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
//#endregion
|
|
26
|
+
export { formatLocalePath, isValidURL };
|
|
2
27
|
//# sourceMappingURL=helpers.mjs.map
|
package/dist/helpers.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.mjs","names":[],"sources":["../src/helpers.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\nexport function formatLocalePath(locale: string) {\n let result = locale;\n if (result.includes(\"_\")) {\n result = result.replace(/_/g, \"-\");\n }\n\n if (result.includes(\"-\")) {\n const parts = result.split(\"-\");\n if (parts.length > 1) {\n const lang = parts[0];\n const region = parts[1];\n if (lang && region) {\n result = `${lang}-${region}`;\n }\n }\n }\n\n return result.toLowerCase();\n}\n\n/**\n * Check if a string has a valid URL format.\n *\n * @param str - The string to check.\n * @returns `true` if the string is a valid URL, otherwise `false`.\n */\nexport function isValidURL(str: string): boolean {\n return /(?:https?:\\/\\/.)?(?:www\\.)?[-\\w@:%.+~#=]{2,256}\\.[a-z]{2,6}\\b[-\\w@:%+.~#?&/=]*/.test(\n str\n );\n}\n"],"mappings":"AAkBA,SAAgB,
|
|
1
|
+
{"version":3,"file":"helpers.mjs","names":[],"sources":["../src/helpers.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\nexport function formatLocalePath(locale: string) {\n let result = locale;\n if (result.includes(\"_\")) {\n result = result.replace(/_/g, \"-\");\n }\n\n if (result.includes(\"-\")) {\n const parts = result.split(\"-\");\n if (parts.length > 1) {\n const lang = parts[0];\n const region = parts[1];\n if (lang && region) {\n result = `${lang}-${region}`;\n }\n }\n }\n\n return result.toLowerCase();\n}\n\n/**\n * Check if a string has a valid URL format.\n *\n * @param str - The string to check.\n * @returns `true` if the string is a valid URL, otherwise `false`.\n */\nexport function isValidURL(str: string): boolean {\n return /(?:https?:\\/\\/.)?(?:www\\.)?[-\\w@:%.+~#=]{2,256}\\.[a-z]{2,6}\\b[-\\w@:%+.~#?&/=]*/.test(\n str\n );\n}\n"],"mappings":";AAkBA,SAAgB,iBAAiB,QAAgB;CAC/C,IAAI,SAAS;AACb,KAAI,OAAO,SAAS,IAAI,CACtB,UAAS,OAAO,QAAQ,MAAM,IAAI;AAGpC,KAAI,OAAO,SAAS,IAAI,EAAE;EACxB,MAAM,QAAQ,OAAO,MAAM,IAAI;AAC/B,MAAI,MAAM,SAAS,GAAG;GACpB,MAAM,OAAO,MAAM;GACnB,MAAM,SAAS,MAAM;AACrB,OAAI,QAAQ,OACV,UAAS,GAAG,KAAK,GAAG;;;AAK1B,QAAO,OAAO,aAAa;;;;;;;;AAS7B,SAAgB,WAAW,KAAsB;AAC/C,QAAO,iFAAiF,KACtF,IACD"}
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1,5 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_storm_url = require('./storm-url.cjs');
|
|
2
|
+
const require_types = require('./types.cjs');
|
|
3
|
+
|
|
4
|
+
exports.PROTOCOL_RELATIVE_SYMBOL = require_types.PROTOCOL_RELATIVE_SYMBOL;
|
|
5
|
+
exports.StormURL = require_storm_url.StormURL;
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1,4 @@
|
|
|
1
|
-
import{StormURL
|
|
1
|
+
import { StormURL } from "./storm-url.mjs";
|
|
2
|
+
import { PROTOCOL_RELATIVE_SYMBOL } from "./types.mjs";
|
|
3
|
+
|
|
4
|
+
export { PROTOCOL_RELATIVE_SYMBOL, StormURL };
|
|
@@ -1 +1,117 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_is_object = require('../../type-checks/src/is-object.cjs');
|
|
3
|
+
const require_is_string = require('../../type-checks/src/is-string.cjs');
|
|
4
|
+
const require_parse = require('./utils/parse.cjs');
|
|
5
|
+
const require_parse_error = require('./utils/parse-error.cjs');
|
|
6
|
+
const require_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 ../json/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_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_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_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;
|
|
@@ -1,2 +1,116 @@
|
|
|
1
|
-
import{isObject
|
|
1
|
+
import { isObject } from "../../type-checks/src/is-object.mjs";
|
|
2
|
+
import { isString } from "../../type-checks/src/is-string.mjs";
|
|
3
|
+
import { parse as parse$1 } from "./utils/parse.mjs";
|
|
4
|
+
import { formatParseError } from "./utils/parse-error.mjs";
|
|
5
|
+
import { stringify } from "./utils/stringify.mjs";
|
|
6
|
+
import { parse } from "jsonc-parser";
|
|
7
|
+
import { Buffer } from "node:buffer";
|
|
8
|
+
import SuperJSON from "superjson";
|
|
9
|
+
|
|
10
|
+
//#region ../json/src/storm-json.ts
|
|
11
|
+
/**
|
|
12
|
+
* A static JSON parser class used by Storm Software to serialize and deserialize JSON data
|
|
13
|
+
*
|
|
14
|
+
* @remarks
|
|
15
|
+
* This class uses the [SuperJSON](https://github.com/blitz-js/superjson) library under the hood.
|
|
16
|
+
*/
|
|
17
|
+
var StormJSON = class StormJSON extends SuperJSON {
|
|
18
|
+
static #instance;
|
|
19
|
+
static get instance() {
|
|
20
|
+
if (!StormJSON.#instance) StormJSON.#instance = new StormJSON();
|
|
21
|
+
return StormJSON.#instance;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Deserialize the given value with superjson using the given metadata
|
|
25
|
+
*/
|
|
26
|
+
static deserialize(payload) {
|
|
27
|
+
return StormJSON.instance.deserialize(payload);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Serialize the given value with superjson
|
|
31
|
+
*/
|
|
32
|
+
static serialize(object) {
|
|
33
|
+
return StormJSON.instance.serialize(object);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Parse the given string value with superjson using the given metadata
|
|
37
|
+
*
|
|
38
|
+
* @param value - The string value to parse
|
|
39
|
+
* @returns The parsed data
|
|
40
|
+
*/
|
|
41
|
+
static parse(value) {
|
|
42
|
+
return parse$1(value);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Serializes the given data to a JSON string.
|
|
46
|
+
* By default the JSON string is formatted with a 2 space indentation to be easy readable.
|
|
47
|
+
*
|
|
48
|
+
* @param value - Object which should be serialized to JSON
|
|
49
|
+
* @param _options - JSON serialize options
|
|
50
|
+
* @returns the formatted JSON representation of the object
|
|
51
|
+
*/
|
|
52
|
+
static stringify(value, _options) {
|
|
53
|
+
const customTransformer = StormJSON.instance.customTransformerRegistry.findApplicable(value);
|
|
54
|
+
let result = value;
|
|
55
|
+
if (customTransformer && customTransformer.isApplicable(value)) result = customTransformer.serialize(result);
|
|
56
|
+
return stringify(result);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Parses the given JSON string and returns the object the JSON content represents.
|
|
60
|
+
* By default javascript-style comments and trailing commas are allowed.
|
|
61
|
+
*
|
|
62
|
+
* @param strData - JSON content as string
|
|
63
|
+
* @param options - JSON parse options
|
|
64
|
+
* @returns Object the JSON content represents
|
|
65
|
+
*/
|
|
66
|
+
static parseJson(strData, options) {
|
|
67
|
+
try {
|
|
68
|
+
if (options?.expectComments === false) return StormJSON.instance.parse(strData);
|
|
69
|
+
} catch {}
|
|
70
|
+
const errors = [];
|
|
71
|
+
const result = parse(strData, errors, {
|
|
72
|
+
allowTrailingComma: true,
|
|
73
|
+
...options
|
|
74
|
+
});
|
|
75
|
+
if (errors.length > 0 && errors[0]) throw new Error(formatParseError(strData, errors[0]));
|
|
76
|
+
return result;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Register a custom schema with superjson
|
|
80
|
+
*
|
|
81
|
+
* @param name - The name of the schema
|
|
82
|
+
* @param serialize - The function to serialize the schema
|
|
83
|
+
* @param deserialize - The function to deserialize the schema
|
|
84
|
+
* @param isApplicable - The function to check if the schema is applicable
|
|
85
|
+
*/
|
|
86
|
+
static register(name, serialize, deserialize, isApplicable) {
|
|
87
|
+
StormJSON.instance.registerCustom({
|
|
88
|
+
isApplicable,
|
|
89
|
+
serialize,
|
|
90
|
+
deserialize
|
|
91
|
+
}, name);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Register a class with superjson
|
|
95
|
+
*
|
|
96
|
+
* @param classConstructor - The class constructor to register
|
|
97
|
+
*/
|
|
98
|
+
static registerClass(classConstructor, options) {
|
|
99
|
+
StormJSON.instance.registerClass(classConstructor, {
|
|
100
|
+
identifier: isString(options) ? options : options?.identifier || classConstructor.name,
|
|
101
|
+
allowProps: options && isObject(options) && options?.allowProps && Array.isArray(options.allowProps) ? options.allowProps : ["__typename"]
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
constructor() {
|
|
105
|
+
super({ dedupe: true });
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
StormJSON.instance.registerCustom({
|
|
109
|
+
isApplicable: (v) => Buffer.isBuffer(v),
|
|
110
|
+
serialize: (v) => v.toString("base64"),
|
|
111
|
+
deserialize: (v) => Buffer.from(v, "base64")
|
|
112
|
+
}, "Bytes");
|
|
113
|
+
|
|
114
|
+
//#endregion
|
|
115
|
+
export { StormJSON };
|
|
2
116
|
//# sourceMappingURL=storm-json.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"storm-json.mjs","names":["#instance","parseValue","stringifyValue","errors: ParseError[]"],"sources":["../../../../json/src/storm-json.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 type { ParseError } from \"jsonc-parser\";\nimport type {\n Class,\n JsonParseOptions,\n JsonParserResult,\n JsonSerializeOptions,\n JsonValue\n} from \"./types\";\n// import { Decimal } from \"decimal.js\";\nimport { isObject } from \"@stryke/type-checks/is-object\";\nimport { isString } from \"@stryke/type-checks/is-string\";\nimport { parse } from \"jsonc-parser\";\nimport { Buffer } from \"node:buffer\";\nimport SuperJSON from \"superjson\";\nimport { parse as parseValue } from \"./utils/parse\";\nimport { formatParseError } from \"./utils/parse-error\";\nimport { stringify as stringifyValue } from \"./utils/stringify\";\n\n/**\n * A static JSON parser class used by Storm Software to serialize and deserialize JSON data\n *\n * @remarks\n * This class uses the [SuperJSON](https://github.com/blitz-js/superjson) library under the hood.\n */\nexport class StormJSON extends SuperJSON {\n static #instance: StormJSON;\n\n public static get instance(): StormJSON {\n if (!StormJSON.#instance) {\n StormJSON.#instance = new StormJSON();\n }\n\n return StormJSON.#instance;\n }\n\n /**\n * Deserialize the given value with superjson using the given metadata\n */\n public static override deserialize<TData = unknown>(\n payload: JsonParserResult\n ): TData {\n return StormJSON.instance.deserialize(payload);\n }\n\n /**\n * Serialize the given value with superjson\n */\n public static override serialize(object: JsonValue): JsonParserResult {\n return StormJSON.instance.serialize(object);\n }\n\n /**\n * Parse the given string value with superjson using the given metadata\n *\n * @param value - The string value to parse\n * @returns The parsed data\n */\n public static override parse<TData = unknown>(value: string): TData {\n return parseValue(value);\n }\n\n /**\n * Serializes the given data to a JSON string.\n * By default the JSON string is formatted with a 2 space indentation to be easy readable.\n *\n * @param value - Object which should be serialized to JSON\n * @param _options - JSON serialize options\n * @returns the formatted JSON representation of the object\n */\n public static override stringify<T>(\n value: T,\n _options?: JsonSerializeOptions\n ): string {\n const customTransformer =\n StormJSON.instance.customTransformerRegistry.findApplicable(value);\n\n let result = value;\n if (customTransformer && customTransformer.isApplicable(value)) {\n result = customTransformer.serialize(result) as T;\n }\n\n return stringifyValue(result);\n }\n\n /**\n * Parses the given JSON string and returns the object the JSON content represents.\n * By default javascript-style comments and trailing commas are allowed.\n *\n * @param strData - JSON content as string\n * @param options - JSON parse options\n * @returns Object the JSON content represents\n */\n public static parseJson<TData = unknown>(\n strData: string,\n options?: JsonParseOptions\n ): TData {\n try {\n if (options?.expectComments === false) {\n return StormJSON.instance.parse(strData);\n }\n } catch {\n // Do nothing\n }\n\n const errors: ParseError[] = [];\n const opts = {\n allowTrailingComma: true,\n ...options\n };\n const result = parse(strData, errors, opts) as TData;\n\n if (errors.length > 0 && errors[0]) {\n throw new Error(formatParseError(strData, errors[0]));\n }\n\n return result;\n }\n\n /**\n * Register a custom schema with superjson\n *\n * @param name - The name of the schema\n * @param serialize - The function to serialize the schema\n * @param deserialize - The function to deserialize the schema\n * @param isApplicable - The function to check if the schema is applicable\n */\n public static register<\n TData = any,\n TJsonObject extends JsonValue = JsonValue\n >(\n name: string,\n serialize: (data: TData) => TJsonObject,\n deserialize: (json: TJsonObject) => TData,\n isApplicable: (data: any) => data is TData\n ) {\n StormJSON.instance.registerCustom<TData, TJsonObject>(\n {\n isApplicable,\n serialize,\n deserialize\n },\n name\n );\n }\n\n /**\n * Register a class with superjson\n *\n * @param classConstructor - The class constructor to register\n */\n public static override registerClass(\n classConstructor: Class,\n options?: { identifier?: string; allowProps?: string[] } | string\n ) {\n StormJSON.instance.registerClass(classConstructor, {\n identifier: isString(options)\n ? options\n : options?.identifier || classConstructor.name,\n allowProps:\n options &&\n isObject(options) &&\n options?.allowProps &&\n Array.isArray(options.allowProps)\n ? options.allowProps\n : [\"__typename\"]\n });\n }\n\n private constructor() {\n super({ dedupe: true });\n }\n}\n\nStormJSON.instance.registerCustom<Buffer, string>(\n {\n isApplicable: (v): v is Buffer => Buffer.isBuffer(v),\n serialize: v => v.toString(\"base64\"),\n deserialize: v => Buffer.from(v, \"base64\")\n },\n \"Bytes\"\n);\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"storm-json.mjs","names":["#instance","parseValue","stringifyValue","errors: ParseError[]"],"sources":["../../../../json/src/storm-json.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 type { ParseError } from \"jsonc-parser\";\nimport type {\n Class,\n JsonParseOptions,\n JsonParserResult,\n JsonSerializeOptions,\n JsonValue\n} from \"./types\";\n// import { Decimal } from \"decimal.js\";\nimport { isObject } from \"@stryke/type-checks/is-object\";\nimport { isString } from \"@stryke/type-checks/is-string\";\nimport { parse } from \"jsonc-parser\";\nimport { Buffer } from \"node:buffer\";\nimport SuperJSON from \"superjson\";\nimport { parse as parseValue } from \"./utils/parse\";\nimport { formatParseError } from \"./utils/parse-error\";\nimport { stringify as stringifyValue } from \"./utils/stringify\";\n\n/**\n * A static JSON parser class used by Storm Software to serialize and deserialize JSON data\n *\n * @remarks\n * This class uses the [SuperJSON](https://github.com/blitz-js/superjson) library under the hood.\n */\nexport class StormJSON extends SuperJSON {\n static #instance: StormJSON;\n\n public static get instance(): StormJSON {\n if (!StormJSON.#instance) {\n StormJSON.#instance = new StormJSON();\n }\n\n return StormJSON.#instance;\n }\n\n /**\n * Deserialize the given value with superjson using the given metadata\n */\n public static override deserialize<TData = unknown>(\n payload: JsonParserResult\n ): TData {\n return StormJSON.instance.deserialize(payload);\n }\n\n /**\n * Serialize the given value with superjson\n */\n public static override serialize(object: JsonValue): JsonParserResult {\n return StormJSON.instance.serialize(object);\n }\n\n /**\n * Parse the given string value with superjson using the given metadata\n *\n * @param value - The string value to parse\n * @returns The parsed data\n */\n public static override parse<TData = unknown>(value: string): TData {\n return parseValue(value);\n }\n\n /**\n * Serializes the given data to a JSON string.\n * By default the JSON string is formatted with a 2 space indentation to be easy readable.\n *\n * @param value - Object which should be serialized to JSON\n * @param _options - JSON serialize options\n * @returns the formatted JSON representation of the object\n */\n public static override stringify<T>(\n value: T,\n _options?: JsonSerializeOptions\n ): string {\n const customTransformer =\n StormJSON.instance.customTransformerRegistry.findApplicable(value);\n\n let result = value;\n if (customTransformer && customTransformer.isApplicable(value)) {\n result = customTransformer.serialize(result) as T;\n }\n\n return stringifyValue(result);\n }\n\n /**\n * Parses the given JSON string and returns the object the JSON content represents.\n * By default javascript-style comments and trailing commas are allowed.\n *\n * @param strData - JSON content as string\n * @param options - JSON parse options\n * @returns Object the JSON content represents\n */\n public static parseJson<TData = unknown>(\n strData: string,\n options?: JsonParseOptions\n ): TData {\n try {\n if (options?.expectComments === false) {\n return StormJSON.instance.parse(strData);\n }\n } catch {\n // Do nothing\n }\n\n const errors: ParseError[] = [];\n const opts = {\n allowTrailingComma: true,\n ...options\n };\n const result = parse(strData, errors, opts) as TData;\n\n if (errors.length > 0 && errors[0]) {\n throw new Error(formatParseError(strData, errors[0]));\n }\n\n return result;\n }\n\n /**\n * Register a custom schema with superjson\n *\n * @param name - The name of the schema\n * @param serialize - The function to serialize the schema\n * @param deserialize - The function to deserialize the schema\n * @param isApplicable - The function to check if the schema is applicable\n */\n public static register<\n TData = any,\n TJsonObject extends JsonValue = JsonValue\n >(\n name: string,\n serialize: (data: TData) => TJsonObject,\n deserialize: (json: TJsonObject) => TData,\n isApplicable: (data: any) => data is TData\n ) {\n StormJSON.instance.registerCustom<TData, TJsonObject>(\n {\n isApplicable,\n serialize,\n deserialize\n },\n name\n );\n }\n\n /**\n * Register a class with superjson\n *\n * @param classConstructor - The class constructor to register\n */\n public static override registerClass(\n classConstructor: Class,\n options?: { identifier?: string; allowProps?: string[] } | string\n ) {\n StormJSON.instance.registerClass(classConstructor, {\n identifier: isString(options)\n ? options\n : options?.identifier || classConstructor.name,\n allowProps:\n options &&\n isObject(options) &&\n options?.allowProps &&\n Array.isArray(options.allowProps)\n ? options.allowProps\n : [\"__typename\"]\n });\n }\n\n private constructor() {\n super({ dedupe: true });\n }\n}\n\nStormJSON.instance.registerCustom<Buffer, string>(\n {\n isApplicable: (v): v is Buffer => Buffer.isBuffer(v),\n serialize: v => v.toString(\"base64\"),\n deserialize: v => Buffer.from(v, \"base64\")\n },\n \"Bytes\"\n);\n"],"mappings":";;;;;;;;;;;;;;;;AA0CA,IAAa,YAAb,MAAa,kBAAkB,UAAU;CACvC,QAAOA;CAEP,WAAkB,WAAsB;AACtC,MAAI,CAAC,WAAUA,SACb,YAAUA,WAAY,IAAI,WAAW;AAGvC,SAAO,WAAUA;;;;;CAMnB,OAAuB,YACrB,SACO;AACP,SAAO,UAAU,SAAS,YAAY,QAAQ;;;;;CAMhD,OAAuB,UAAU,QAAqC;AACpE,SAAO,UAAU,SAAS,UAAU,OAAO;;;;;;;;CAS7C,OAAuB,MAAuB,OAAsB;AAClE,SAAOC,QAAW,MAAM;;;;;;;;;;CAW1B,OAAuB,UACrB,OACA,UACQ;EACR,MAAM,oBACJ,UAAU,SAAS,0BAA0B,eAAe,MAAM;EAEpE,IAAI,SAAS;AACb,MAAI,qBAAqB,kBAAkB,aAAa,MAAM,CAC5D,UAAS,kBAAkB,UAAU,OAAO;AAG9C,SAAOC,UAAe,OAAO;;;;;;;;;;CAW/B,OAAc,UACZ,SACA,SACO;AACP,MAAI;AACF,OAAI,SAAS,mBAAmB,MAC9B,QAAO,UAAU,SAAS,MAAM,QAAQ;UAEpC;EAIR,MAAMC,SAAuB,EAAE;EAK/B,MAAM,SAAS,MAAM,SAAS,QAJjB;GACX,oBAAoB;GACpB,GAAG;GACJ,CAC0C;AAE3C,MAAI,OAAO,SAAS,KAAK,OAAO,GAC9B,OAAM,IAAI,MAAM,iBAAiB,SAAS,OAAO,GAAG,CAAC;AAGvD,SAAO;;;;;;;;;;CAWT,OAAc,SAIZ,MACA,WACA,aACA,cACA;AACA,YAAU,SAAS,eACjB;GACE;GACA;GACA;GACD,EACD,KACD;;;;;;;CAQH,OAAuB,cACrB,kBACA,SACA;AACA,YAAU,SAAS,cAAc,kBAAkB;GACjD,YAAY,SAAS,QAAQ,GACzB,UACA,SAAS,cAAc,iBAAiB;GAC5C,YACE,WACA,SAAS,QAAQ,IACjB,SAAS,cACT,MAAM,QAAQ,QAAQ,WAAW,GAC7B,QAAQ,aACR,CAAC,aAAa;GACrB,CAAC;;CAGJ,AAAQ,cAAc;AACpB,QAAM,EAAE,QAAQ,MAAM,CAAC;;;AAI3B,UAAU,SAAS,eACjB;CACE,eAAe,MAAmB,OAAO,SAAS,EAAE;CACpD,YAAW,MAAK,EAAE,SAAS,SAAS;CACpC,cAAa,MAAK,OAAO,KAAK,GAAG,SAAS;CAC3C,EACD,QACD"}
|
|
@@ -1,3 +1,77 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
|
|
2
|
+
//#region ../json/src/utils/code-frames.ts
|
|
3
|
+
/**
|
|
4
|
+
* RegExp to test for newlines in terminal.
|
|
5
|
+
*/
|
|
6
|
+
const NEWLINE = /\r\n|[\n\r\u2028\u2029]/;
|
|
7
|
+
/**
|
|
8
|
+
* Extract what lines should be marked and highlighted.
|
|
9
|
+
*/
|
|
10
|
+
function getMarkerLines(loc, source, opts = {}) {
|
|
11
|
+
const startLoc = {
|
|
12
|
+
column: 0,
|
|
13
|
+
line: -1,
|
|
14
|
+
...loc.start
|
|
15
|
+
};
|
|
16
|
+
const endLoc = {
|
|
17
|
+
...startLoc,
|
|
18
|
+
...loc.end
|
|
19
|
+
};
|
|
20
|
+
const { linesAbove = 2, linesBelow = 3 } = opts || {};
|
|
21
|
+
const startLine = startLoc.line;
|
|
22
|
+
const startColumn = startLoc.column;
|
|
23
|
+
const endLine = endLoc.line;
|
|
24
|
+
const endColumn = endLoc.column;
|
|
25
|
+
let start = Math.max(startLine - (linesAbove + 1), 0);
|
|
26
|
+
let end = Math.min(source.length, endLine + linesBelow);
|
|
27
|
+
if (startLine === -1) start = 0;
|
|
28
|
+
if (endLine === -1) end = source.length;
|
|
29
|
+
const lineDiff = endLine - startLine;
|
|
30
|
+
const markerLines = {};
|
|
31
|
+
if (lineDiff) for (let i = 0; i <= lineDiff; i++) {
|
|
32
|
+
const lineNumber = i + startLine;
|
|
33
|
+
if (!startColumn) markerLines[lineNumber] = true;
|
|
34
|
+
else if (i === 0) markerLines[lineNumber] = [startColumn, (source[lineNumber - 1]?.length ?? 0) - startColumn + 1];
|
|
35
|
+
else if (i === lineDiff) markerLines[lineNumber] = [0, endColumn];
|
|
36
|
+
else markerLines[lineNumber] = [0, source[lineNumber - i]?.length ?? 0];
|
|
37
|
+
}
|
|
38
|
+
else if (startColumn === endColumn) markerLines[startLine] = startColumn ? [startColumn, 0] : true;
|
|
39
|
+
else markerLines[startLine] = [startColumn, endColumn - startColumn];
|
|
40
|
+
return {
|
|
41
|
+
start,
|
|
42
|
+
end,
|
|
43
|
+
markerLines
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function codeFrameColumns(rawLines, loc, opts = {}) {
|
|
47
|
+
const { start, end, markerLines } = getMarkerLines(loc, rawLines.split(NEWLINE), opts);
|
|
48
|
+
const numberMaxWidth = String(end).length;
|
|
49
|
+
return (opts.highlight ? opts.highlight(rawLines) : rawLines).split(NEWLINE).slice(start, end).map((line, index) => {
|
|
50
|
+
const number = start + 1 + index;
|
|
51
|
+
const gutter = ` ${` ${number}`.slice(-numberMaxWidth)} | `;
|
|
52
|
+
const hasMarker = Boolean(markerLines[number] ?? false);
|
|
53
|
+
if (hasMarker) {
|
|
54
|
+
let markerLine = "";
|
|
55
|
+
if (Array.isArray(hasMarker)) {
|
|
56
|
+
const markerSpacing = line.slice(0, Math.max(hasMarker[0] - 1, 0)).replace(/[^\t]/g, " ");
|
|
57
|
+
const numberOfMarkers = hasMarker[1] || 1;
|
|
58
|
+
markerLine = [
|
|
59
|
+
"\n ",
|
|
60
|
+
gutter.replace(/\d/g, " "),
|
|
61
|
+
markerSpacing,
|
|
62
|
+
"^".repeat(numberOfMarkers)
|
|
63
|
+
].join("");
|
|
64
|
+
}
|
|
65
|
+
return [
|
|
66
|
+
">",
|
|
67
|
+
gutter,
|
|
68
|
+
line,
|
|
69
|
+
markerLine
|
|
70
|
+
].join("");
|
|
71
|
+
}
|
|
72
|
+
return ` ${gutter}${line}`;
|
|
73
|
+
}).join("\n");
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
//#endregion
|
|
77
|
+
exports.codeFrameColumns = codeFrameColumns;
|
|
@@ -1,4 +1,77 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
//#region ../json/src/utils/code-frames.ts
|
|
2
|
+
/**
|
|
3
|
+
* RegExp to test for newlines in terminal.
|
|
4
|
+
*/
|
|
5
|
+
const NEWLINE = /\r\n|[\n\r\u2028\u2029]/;
|
|
6
|
+
/**
|
|
7
|
+
* Extract what lines should be marked and highlighted.
|
|
8
|
+
*/
|
|
9
|
+
function getMarkerLines(loc, source, opts = {}) {
|
|
10
|
+
const startLoc = {
|
|
11
|
+
column: 0,
|
|
12
|
+
line: -1,
|
|
13
|
+
...loc.start
|
|
14
|
+
};
|
|
15
|
+
const endLoc = {
|
|
16
|
+
...startLoc,
|
|
17
|
+
...loc.end
|
|
18
|
+
};
|
|
19
|
+
const { linesAbove = 2, linesBelow = 3 } = opts || {};
|
|
20
|
+
const startLine = startLoc.line;
|
|
21
|
+
const startColumn = startLoc.column;
|
|
22
|
+
const endLine = endLoc.line;
|
|
23
|
+
const endColumn = endLoc.column;
|
|
24
|
+
let start = Math.max(startLine - (linesAbove + 1), 0);
|
|
25
|
+
let end = Math.min(source.length, endLine + linesBelow);
|
|
26
|
+
if (startLine === -1) start = 0;
|
|
27
|
+
if (endLine === -1) end = source.length;
|
|
28
|
+
const lineDiff = endLine - startLine;
|
|
29
|
+
const markerLines = {};
|
|
30
|
+
if (lineDiff) for (let i = 0; i <= lineDiff; i++) {
|
|
31
|
+
const lineNumber = i + startLine;
|
|
32
|
+
if (!startColumn) markerLines[lineNumber] = true;
|
|
33
|
+
else if (i === 0) markerLines[lineNumber] = [startColumn, (source[lineNumber - 1]?.length ?? 0) - startColumn + 1];
|
|
34
|
+
else if (i === lineDiff) markerLines[lineNumber] = [0, endColumn];
|
|
35
|
+
else markerLines[lineNumber] = [0, source[lineNumber - i]?.length ?? 0];
|
|
36
|
+
}
|
|
37
|
+
else if (startColumn === endColumn) markerLines[startLine] = startColumn ? [startColumn, 0] : true;
|
|
38
|
+
else markerLines[startLine] = [startColumn, endColumn - startColumn];
|
|
39
|
+
return {
|
|
40
|
+
start,
|
|
41
|
+
end,
|
|
42
|
+
markerLines
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function codeFrameColumns(rawLines, loc, opts = {}) {
|
|
46
|
+
const { start, end, markerLines } = getMarkerLines(loc, rawLines.split(NEWLINE), opts);
|
|
47
|
+
const numberMaxWidth = String(end).length;
|
|
48
|
+
return (opts.highlight ? opts.highlight(rawLines) : rawLines).split(NEWLINE).slice(start, end).map((line, index) => {
|
|
49
|
+
const number = start + 1 + index;
|
|
50
|
+
const gutter = ` ${` ${number}`.slice(-numberMaxWidth)} | `;
|
|
51
|
+
const hasMarker = Boolean(markerLines[number] ?? false);
|
|
52
|
+
if (hasMarker) {
|
|
53
|
+
let markerLine = "";
|
|
54
|
+
if (Array.isArray(hasMarker)) {
|
|
55
|
+
const markerSpacing = line.slice(0, Math.max(hasMarker[0] - 1, 0)).replace(/[^\t]/g, " ");
|
|
56
|
+
const numberOfMarkers = hasMarker[1] || 1;
|
|
57
|
+
markerLine = [
|
|
58
|
+
"\n ",
|
|
59
|
+
gutter.replace(/\d/g, " "),
|
|
60
|
+
markerSpacing,
|
|
61
|
+
"^".repeat(numberOfMarkers)
|
|
62
|
+
].join("");
|
|
63
|
+
}
|
|
64
|
+
return [
|
|
65
|
+
">",
|
|
66
|
+
gutter,
|
|
67
|
+
line,
|
|
68
|
+
markerLine
|
|
69
|
+
].join("");
|
|
70
|
+
}
|
|
71
|
+
return ` ${gutter}${line}`;
|
|
72
|
+
}).join("\n");
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
//#endregion
|
|
76
|
+
export { codeFrameColumns };
|
|
4
77
|
//# sourceMappingURL=code-frames.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"code-frames.mjs","names":["startLoc: Location","endLoc: Location"],"sources":["../../../../../json/src/utils/code-frames.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\ninterface Location {\n column: number;\n line: number;\n}\n\ninterface NodeLocation {\n end?: Location;\n start?: Location;\n}\n\n// Adapted from https://raw.githubusercontent.com/babel/babel/4108524/packages/babel-code-frame/src/index.js\n\n/**\n * RegExp to test for newlines in terminal.\n */\nconst NEWLINE = /\\r\\n|[\\n\\r\\u2028\\u2029]/;\n\n/**\n * Extract what lines should be marked and highlighted.\n */\nfunction getMarkerLines(\n loc: NodeLocation,\n source: string[],\n opts: { linesAbove?: number; linesBelow?: number } = {}\n): {\n start: number;\n end: number;\n markerLines: Record<number, boolean | [number, number]>;\n} {\n const startLoc: Location = {\n column: 0,\n line: -1,\n ...loc.start\n };\n const endLoc: Location = {\n ...startLoc,\n ...loc.end\n };\n const { linesAbove = 2, linesBelow = 3 } = opts || {};\n const startLine = startLoc.line;\n const startColumn = startLoc.column;\n const endLine = endLoc.line;\n const endColumn = endLoc.column;\n\n let start = Math.max(startLine - (linesAbove + 1), 0);\n let end = Math.min(source.length, endLine + linesBelow);\n\n if (startLine === -1) {\n start = 0;\n }\n\n if (endLine === -1) {\n end = source.length;\n }\n\n const lineDiff = endLine - startLine;\n const markerLines = {} as Record<number, boolean | [number, number]>;\n\n if (lineDiff) {\n for (let i = 0; i <= lineDiff; i++) {\n const lineNumber = i + startLine;\n\n if (!startColumn) {\n markerLines[lineNumber] = true;\n } else if (i === 0) {\n const sourceLength = source[lineNumber - 1]?.length ?? 0;\n\n markerLines[lineNumber] = [startColumn, sourceLength - startColumn + 1];\n } else if (i === lineDiff) {\n markerLines[lineNumber] = [0, endColumn];\n } else {\n const sourceLength = source[lineNumber - i]?.length ?? 0;\n\n markerLines[lineNumber] = [0, sourceLength];\n }\n }\n } else if (startColumn === endColumn) {\n markerLines[startLine] = startColumn ? [startColumn, 0] : true;\n } else {\n markerLines[startLine] = [startColumn, endColumn - startColumn];\n }\n\n return {\n start,\n end,\n markerLines\n };\n}\n\nexport function codeFrameColumns(\n rawLines: string,\n loc: NodeLocation,\n opts: {\n linesAbove?: number;\n linesBelow?: number;\n highlight?: (rawLines: string) => string;\n } = {}\n): string {\n const lines = rawLines.split(NEWLINE);\n const { start, end, markerLines } = getMarkerLines(loc, lines, opts);\n\n const numberMaxWidth = String(end).length;\n const highlightedLines = opts.highlight ? opts.highlight(rawLines) : rawLines;\n\n const frame = highlightedLines\n .split(NEWLINE)\n .slice(start, end)\n .map((line, index) => {\n const number = start + 1 + index;\n const paddedNumber = ` ${number}`.slice(-numberMaxWidth);\n const gutter = ` ${paddedNumber} | `;\n const hasMarker = Boolean(markerLines[number] ?? false);\n if (hasMarker) {\n let markerLine = \"\";\n if (Array.isArray(hasMarker)) {\n const markerSpacing = line\n .slice(0, Math.max(hasMarker[0] - 1, 0))\n .replace(/[^\\t]/g, \" \");\n const numberOfMarkers = hasMarker[1] || 1;\n\n markerLine = [\n \"\\n \",\n gutter.replace(/\\d/g, \" \"),\n markerSpacing,\n \"^\".repeat(numberOfMarkers)\n ].join(\"\");\n }\n return [\">\", gutter, line, markerLine].join(\"\");\n }\n return ` ${gutter}${line}`;\n })\n .join(\"\\n\");\n\n return frame;\n}\n"],"mappings":"AAiCA,MAAM,
|
|
1
|
+
{"version":3,"file":"code-frames.mjs","names":["startLoc: Location","endLoc: Location"],"sources":["../../../../../json/src/utils/code-frames.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\ninterface Location {\n column: number;\n line: number;\n}\n\ninterface NodeLocation {\n end?: Location;\n start?: Location;\n}\n\n// Adapted from https://raw.githubusercontent.com/babel/babel/4108524/packages/babel-code-frame/src/index.js\n\n/**\n * RegExp to test for newlines in terminal.\n */\nconst NEWLINE = /\\r\\n|[\\n\\r\\u2028\\u2029]/;\n\n/**\n * Extract what lines should be marked and highlighted.\n */\nfunction getMarkerLines(\n loc: NodeLocation,\n source: string[],\n opts: { linesAbove?: number; linesBelow?: number } = {}\n): {\n start: number;\n end: number;\n markerLines: Record<number, boolean | [number, number]>;\n} {\n const startLoc: Location = {\n column: 0,\n line: -1,\n ...loc.start\n };\n const endLoc: Location = {\n ...startLoc,\n ...loc.end\n };\n const { linesAbove = 2, linesBelow = 3 } = opts || {};\n const startLine = startLoc.line;\n const startColumn = startLoc.column;\n const endLine = endLoc.line;\n const endColumn = endLoc.column;\n\n let start = Math.max(startLine - (linesAbove + 1), 0);\n let end = Math.min(source.length, endLine + linesBelow);\n\n if (startLine === -1) {\n start = 0;\n }\n\n if (endLine === -1) {\n end = source.length;\n }\n\n const lineDiff = endLine - startLine;\n const markerLines = {} as Record<number, boolean | [number, number]>;\n\n if (lineDiff) {\n for (let i = 0; i <= lineDiff; i++) {\n const lineNumber = i + startLine;\n\n if (!startColumn) {\n markerLines[lineNumber] = true;\n } else if (i === 0) {\n const sourceLength = source[lineNumber - 1]?.length ?? 0;\n\n markerLines[lineNumber] = [startColumn, sourceLength - startColumn + 1];\n } else if (i === lineDiff) {\n markerLines[lineNumber] = [0, endColumn];\n } else {\n const sourceLength = source[lineNumber - i]?.length ?? 0;\n\n markerLines[lineNumber] = [0, sourceLength];\n }\n }\n } else if (startColumn === endColumn) {\n markerLines[startLine] = startColumn ? [startColumn, 0] : true;\n } else {\n markerLines[startLine] = [startColumn, endColumn - startColumn];\n }\n\n return {\n start,\n end,\n markerLines\n };\n}\n\nexport function codeFrameColumns(\n rawLines: string,\n loc: NodeLocation,\n opts: {\n linesAbove?: number;\n linesBelow?: number;\n highlight?: (rawLines: string) => string;\n } = {}\n): string {\n const lines = rawLines.split(NEWLINE);\n const { start, end, markerLines } = getMarkerLines(loc, lines, opts);\n\n const numberMaxWidth = String(end).length;\n const highlightedLines = opts.highlight ? opts.highlight(rawLines) : rawLines;\n\n const frame = highlightedLines\n .split(NEWLINE)\n .slice(start, end)\n .map((line, index) => {\n const number = start + 1 + index;\n const paddedNumber = ` ${number}`.slice(-numberMaxWidth);\n const gutter = ` ${paddedNumber} | `;\n const hasMarker = Boolean(markerLines[number] ?? false);\n if (hasMarker) {\n let markerLine = \"\";\n if (Array.isArray(hasMarker)) {\n const markerSpacing = line\n .slice(0, Math.max(hasMarker[0] - 1, 0))\n .replace(/[^\\t]/g, \" \");\n const numberOfMarkers = hasMarker[1] || 1;\n\n markerLine = [\n \"\\n \",\n gutter.replace(/\\d/g, \" \"),\n markerSpacing,\n \"^\".repeat(numberOfMarkers)\n ].join(\"\");\n }\n return [\">\", gutter, line, markerLine].join(\"\");\n }\n return ` ${gutter}${line}`;\n })\n .join(\"\\n\");\n\n return frame;\n}\n"],"mappings":";;;;AAiCA,MAAM,UAAU;;;;AAKhB,SAAS,eACP,KACA,QACA,OAAqD,EAAE,EAKvD;CACA,MAAMA,WAAqB;EACzB,QAAQ;EACR,MAAM;EACN,GAAG,IAAI;EACR;CACD,MAAMC,SAAmB;EACvB,GAAG;EACH,GAAG,IAAI;EACR;CACD,MAAM,EAAE,aAAa,GAAG,aAAa,MAAM,QAAQ,EAAE;CACrD,MAAM,YAAY,SAAS;CAC3B,MAAM,cAAc,SAAS;CAC7B,MAAM,UAAU,OAAO;CACvB,MAAM,YAAY,OAAO;CAEzB,IAAI,QAAQ,KAAK,IAAI,aAAa,aAAa,IAAI,EAAE;CACrD,IAAI,MAAM,KAAK,IAAI,OAAO,QAAQ,UAAU,WAAW;AAEvD,KAAI,cAAc,GAChB,SAAQ;AAGV,KAAI,YAAY,GACd,OAAM,OAAO;CAGf,MAAM,WAAW,UAAU;CAC3B,MAAM,cAAc,EAAE;AAEtB,KAAI,SACF,MAAK,IAAI,IAAI,GAAG,KAAK,UAAU,KAAK;EAClC,MAAM,aAAa,IAAI;AAEvB,MAAI,CAAC,YACH,aAAY,cAAc;WACjB,MAAM,EAGf,aAAY,cAAc,CAAC,cAFN,OAAO,aAAa,IAAI,UAAU,KAEA,cAAc,EAAE;WAC9D,MAAM,SACf,aAAY,cAAc,CAAC,GAAG,UAAU;MAIxC,aAAY,cAAc,CAAC,GAFN,OAAO,aAAa,IAAI,UAAU,EAEZ;;UAGtC,gBAAgB,UACzB,aAAY,aAAa,cAAc,CAAC,aAAa,EAAE,GAAG;KAE1D,aAAY,aAAa,CAAC,aAAa,YAAY,YAAY;AAGjE,QAAO;EACL;EACA;EACA;EACD;;AAGH,SAAgB,iBACd,UACA,KACA,OAII,EAAE,EACE;CAER,MAAM,EAAE,OAAO,KAAK,gBAAgB,eAAe,KADrC,SAAS,MAAM,QAAQ,EAC0B,KAAK;CAEpE,MAAM,iBAAiB,OAAO,IAAI,CAAC;AAgCnC,SA/ByB,KAAK,YAAY,KAAK,UAAU,SAAS,GAAG,UAGlE,MAAM,QAAQ,CACd,MAAM,OAAO,IAAI,CACjB,KAAK,MAAM,UAAU;EACpB,MAAM,SAAS,QAAQ,IAAI;EAE3B,MAAM,SAAS,IADM,IAAI,SAAS,MAAM,CAAC,eAAe,CACxB;EAChC,MAAM,YAAY,QAAQ,YAAY,WAAW,MAAM;AACvD,MAAI,WAAW;GACb,IAAI,aAAa;AACjB,OAAI,MAAM,QAAQ,UAAU,EAAE;IAC5B,MAAM,gBAAgB,KACnB,MAAM,GAAG,KAAK,IAAI,UAAU,KAAK,GAAG,EAAE,CAAC,CACvC,QAAQ,UAAU,IAAI;IACzB,MAAM,kBAAkB,UAAU,MAAM;AAExC,iBAAa;KACX;KACA,OAAO,QAAQ,OAAO,IAAI;KAC1B;KACA,IAAI,OAAO,gBAAgB;KAC5B,CAAC,KAAK,GAAG;;AAEZ,UAAO;IAAC;IAAK;IAAQ;IAAM;IAAW,CAAC,KAAK,GAAG;;AAEjD,SAAO,IAAI,SAAS;GACpB,CACD,KAAK,KAAK"}
|