@stryke/json 0.0.1
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/LICENSE +201 -0
- package/README.md +296 -0
- package/dist/index.cjs +49 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.mjs +1 -0
- package/dist/pointer/find-reference.cjs +39 -0
- package/dist/pointer/find-reference.d.ts +48 -0
- package/dist/pointer/find-reference.mjs +1 -0
- package/dist/pointer/index.cjs +27 -0
- package/dist/pointer/index.d.ts +2 -0
- package/dist/pointer/index.mjs +1 -0
- package/dist/pointer/parse.cjs +54 -0
- package/dist/pointer/parse.d.ts +38 -0
- package/dist/pointer/parse.mjs +1 -0
- package/dist/storm-json.cjs +76 -0
- package/dist/storm-json.d.ts +71 -0
- package/dist/storm-json.mjs +1 -0
- package/dist/types.cjs +1 -0
- package/dist/types.d.ts +64 -0
- package/dist/types.mjs +0 -0
- package/dist/utils/code-frames.cjs +72 -0
- package/dist/utils/code-frames.d.ts +14 -0
- package/dist/utils/code-frames.mjs +3 -0
- package/dist/utils/index.cjs +49 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.mjs +1 -0
- package/dist/utils/parse-error.cjs +31 -0
- package/dist/utils/parse-error.d.ts +9 -0
- package/dist/utils/parse-error.mjs +3 -0
- package/dist/utils/stringify.cjs +35 -0
- package/dist/utils/stringify.d.ts +8 -0
- package/dist/utils/stringify.mjs +1 -0
- package/dist/utils/strip-comments.cjs +12 -0
- package/dist/utils/strip-comments.d.ts +1 -0
- package/dist/utils/strip-comments.mjs +1 -0
- package/package.json +248 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
var _findReference = require("./find-reference.cjs");
|
|
7
|
+
Object.keys(_findReference).forEach(function (key) {
|
|
8
|
+
if (key === "default" || key === "__esModule") return;
|
|
9
|
+
if (key in exports && exports[key] === _findReference[key]) return;
|
|
10
|
+
Object.defineProperty(exports, key, {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
get: function () {
|
|
13
|
+
return _findReference[key];
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
var _parse = require("./parse.cjs");
|
|
18
|
+
Object.keys(_parse).forEach(function (key) {
|
|
19
|
+
if (key === "default" || key === "__esModule") return;
|
|
20
|
+
if (key in exports && exports[key] === _parse[key]) return;
|
|
21
|
+
Object.defineProperty(exports, key, {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
get: function () {
|
|
24
|
+
return _parse[key];
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export*from"./find-reference";export*from"./parse";
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.escapePointerSegment = escapePointerSegment;
|
|
7
|
+
exports.formatJsonPointer = formatJsonPointer;
|
|
8
|
+
exports.isRoot = exports.isInteger = void 0;
|
|
9
|
+
exports.isValidIndex = isValidIndex;
|
|
10
|
+
exports.parent = parent;
|
|
11
|
+
exports.parseJsonPointer = parseJsonPointer;
|
|
12
|
+
exports.unescapePointerSegment = unescapePointerSegment;
|
|
13
|
+
var _isNumber = require("@stryke/types/type-checks/is-number");
|
|
14
|
+
const i = /~1/g,
|
|
15
|
+
s = /~0/g,
|
|
16
|
+
u = /~/g,
|
|
17
|
+
c = /\//g;
|
|
18
|
+
function escapePointerSegment(n) {
|
|
19
|
+
return !n.includes("/") && !n.includes("~") ? n : n.replace(u, "~0").replace(c, "~1");
|
|
20
|
+
}
|
|
21
|
+
function unescapePointerSegment(n) {
|
|
22
|
+
return n.includes("~") ? n.replace(i, "/").replace(s, "~") : n;
|
|
23
|
+
}
|
|
24
|
+
function parseJsonPointer(n) {
|
|
25
|
+
return n ? n.slice(1).split("/").map(t => unescapePointerSegment(t)) : [];
|
|
26
|
+
}
|
|
27
|
+
function formatJsonPointer(n) {
|
|
28
|
+
return isRoot(n) ? "" : "/" + n.map(t => escapePointerSegment(String(t))).join("/");
|
|
29
|
+
}
|
|
30
|
+
const isRoot = n => n.length === 0;
|
|
31
|
+
exports.isRoot = isRoot;
|
|
32
|
+
function parent(n) {
|
|
33
|
+
if (n.length === 0) throw new Error("NO_PARENT");
|
|
34
|
+
return n.slice(0, -1);
|
|
35
|
+
}
|
|
36
|
+
function isValidIndex(n) {
|
|
37
|
+
if ((0, _isNumber.isNumber)(n)) return !0;
|
|
38
|
+
const t = Number.parseInt(n, 10);
|
|
39
|
+
return String(t) === n && t >= 0;
|
|
40
|
+
}
|
|
41
|
+
const isInteger = n => {
|
|
42
|
+
const t = n.length;
|
|
43
|
+
let r = 0,
|
|
44
|
+
e;
|
|
45
|
+
for (; r < t;) {
|
|
46
|
+
if (e = n.codePointAt(r), e >= 48 && e <= 57) {
|
|
47
|
+
r++;
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
return !1;
|
|
51
|
+
}
|
|
52
|
+
return !0;
|
|
53
|
+
};
|
|
54
|
+
exports.isInteger = isInteger;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { JsonPointerPath } from "@stryke/types/utility-types/json";
|
|
2
|
+
/**
|
|
3
|
+
* Escapes a JSON pointer path segment.
|
|
4
|
+
*
|
|
5
|
+
* @param segment - JSON pointer path segment.
|
|
6
|
+
* @returns Escaped JSON pointer path segment.
|
|
7
|
+
*/
|
|
8
|
+
export declare function escapePointerSegment(segment: string): string;
|
|
9
|
+
/**
|
|
10
|
+
* Unescapes a JSON pointer path segment.
|
|
11
|
+
*
|
|
12
|
+
* @param segment - JSON pointer path segment.
|
|
13
|
+
* @returns Unescaped JSON pointer path segment.
|
|
14
|
+
*/
|
|
15
|
+
export declare function unescapePointerSegment(segment: string): string;
|
|
16
|
+
/**
|
|
17
|
+
* Convert JSON pointer like "/foo/bar" to array like ["", "foo", "bar"], while
|
|
18
|
+
* also un-escaping reserved characters.
|
|
19
|
+
*/
|
|
20
|
+
export declare function parseJsonPointer(pointer: string): JsonPointerPath;
|
|
21
|
+
/**
|
|
22
|
+
* Escape and format a path array like ["", "foo", "bar"] to JSON pointer
|
|
23
|
+
* like "/foo/bar".
|
|
24
|
+
*/
|
|
25
|
+
export declare function formatJsonPointer(path: JsonPointerPath): string;
|
|
26
|
+
/**
|
|
27
|
+
* Returns true if JSON Pointer points to root value, false otherwise.
|
|
28
|
+
*/
|
|
29
|
+
export declare const isRoot: (path: string | number) => boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Returns parent path, e.g. for ['foo', 'bar', 'baz'] returns ['foo', 'bar'].
|
|
32
|
+
*/
|
|
33
|
+
export declare function parent(path: JsonPointerPath): JsonPointerPath;
|
|
34
|
+
/**
|
|
35
|
+
* Check if path component can be a valid array index.
|
|
36
|
+
*/
|
|
37
|
+
export declare function isValidIndex(index: string | number): boolean;
|
|
38
|
+
export declare const isInteger: (str: string) => boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{isNumber as o}from"@stryke/types/type-checks/is-number";const i=/~1/g,s=/~0/g,u=/~/g,c=/\//g;export function escapePointerSegment(n){return!n.includes("/")&&!n.includes("~")?n:n.replace(u,"~0").replace(c,"~1")}export function unescapePointerSegment(n){return n.includes("~")?n.replace(i,"/").replace(s,"~"):n}export function parseJsonPointer(n){return n?n.slice(1).split("/").map(t=>unescapePointerSegment(t)):[]}export function formatJsonPointer(n){return isRoot(n)?"":"/"+n.map(t=>escapePointerSegment(String(t))).join("/")}export const isRoot=n=>n.length===0;export function parent(n){if(n.length===0)throw new Error("NO_PARENT");return n.slice(0,-1)}export function isValidIndex(n){if(o(n))return!0;const t=Number.parseInt(n,10);return String(t)===n&&t>=0}export const isInteger=n=>{const t=n.length;let r=0,e;for(;r<t;){if(e=n.codePointAt(r),e>=48&&e<=57){r++;continue}return!1}return!0};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.StormJSON = void 0;
|
|
7
|
+
var _isObject = require("@stryke/types/type-checks/is-object");
|
|
8
|
+
var _isString = require("@stryke/types/type-checks/is-string");
|
|
9
|
+
var _buffer = require("buffer/");
|
|
10
|
+
var _jsoncParser = require("jsonc-parser");
|
|
11
|
+
var _superjson = _interopRequireDefault(require("superjson"));
|
|
12
|
+
var _parseError = require("./utils/parse-error.cjs");
|
|
13
|
+
var _stringify = require("./utils/stringify.cjs");
|
|
14
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
15
|
+
class StormJSON extends _superjson.default {
|
|
16
|
+
static #e;
|
|
17
|
+
static get instance() {
|
|
18
|
+
return StormJSON.#e || (StormJSON.#e = new StormJSON()), StormJSON.#e;
|
|
19
|
+
}
|
|
20
|
+
static deserialize(e) {
|
|
21
|
+
return StormJSON.instance.deserialize(e);
|
|
22
|
+
}
|
|
23
|
+
static serialize(e) {
|
|
24
|
+
return StormJSON.instance.serialize(e);
|
|
25
|
+
}
|
|
26
|
+
static parse(e) {
|
|
27
|
+
return StormJSON.instance.parse(e);
|
|
28
|
+
}
|
|
29
|
+
static stringify(e) {
|
|
30
|
+
const r = StormJSON.instance.customTransformerRegistry.findApplicable(e);
|
|
31
|
+
let s = e;
|
|
32
|
+
return r && (s = r.serialize(s)), (0, _stringify.stringify)(s);
|
|
33
|
+
}
|
|
34
|
+
static stringifyJson(e, r) {
|
|
35
|
+
const s = StormJSON.instance.customTransformerRegistry.findApplicable(e);
|
|
36
|
+
let t = e;
|
|
37
|
+
return s && (t = s.serialize(t)), (0, _stringify.stringify)(t, r?.spaces ?? 2);
|
|
38
|
+
}
|
|
39
|
+
static parseJson(e, r) {
|
|
40
|
+
try {
|
|
41
|
+
if (r?.expectComments === !1) return StormJSON.instance.parse(e);
|
|
42
|
+
} catch {}
|
|
43
|
+
const s = [],
|
|
44
|
+
t = {
|
|
45
|
+
allowTrailingComma: !0,
|
|
46
|
+
...r
|
|
47
|
+
},
|
|
48
|
+
o = (0, _jsoncParser.parse)(e, s, t);
|
|
49
|
+
if (s.length > 0 && s[0]) throw new Error((0, _parseError.formatParseError)(e, s[0]));
|
|
50
|
+
return o;
|
|
51
|
+
}
|
|
52
|
+
static register(e, r, s, t) {
|
|
53
|
+
StormJSON.instance.registerCustom({
|
|
54
|
+
isApplicable: t,
|
|
55
|
+
serialize: r,
|
|
56
|
+
deserialize: s
|
|
57
|
+
}, e);
|
|
58
|
+
}
|
|
59
|
+
static registerClass(e, r) {
|
|
60
|
+
StormJSON.instance.registerClass(e, {
|
|
61
|
+
identifier: (0, _isString.isString)(r) ? r : r?.identifier || e.name,
|
|
62
|
+
allowProps: r && (0, _isObject.isObject)(r) && r?.allowProps && Array.isArray(r.allowProps) ? r.allowProps : ["__typename"]
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
constructor() {
|
|
66
|
+
super({
|
|
67
|
+
dedupe: !0
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
exports.StormJSON = StormJSON;
|
|
72
|
+
StormJSON.instance.registerCustom({
|
|
73
|
+
isApplicable: i => _buffer.Buffer.isBuffer(i),
|
|
74
|
+
serialize: i => i.toString("base64"),
|
|
75
|
+
deserialize: i => _buffer.Buffer.from(i, "base64")
|
|
76
|
+
}, "Bytes");
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import SuperJSON from "superjson";
|
|
2
|
+
import type { Class, JsonParseOptions, JsonParserResult, JsonSerializeOptions, JsonValue } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* A static JSON parser class used by Storm Software to serialize and deserialize JSON data
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* This class uses the [SuperJSON](https://github.com/blitz-js/superjson) library under the hood.
|
|
8
|
+
*/
|
|
9
|
+
export declare class StormJSON extends SuperJSON {
|
|
10
|
+
#private;
|
|
11
|
+
static get instance(): StormJSON;
|
|
12
|
+
/**
|
|
13
|
+
* Deserialize the given value with superjson using the given metadata
|
|
14
|
+
*/
|
|
15
|
+
static deserialize<TData = unknown>(payload: JsonParserResult): TData;
|
|
16
|
+
/**
|
|
17
|
+
* Serialize the given value with superjson
|
|
18
|
+
*
|
|
19
|
+
*
|
|
20
|
+
*/
|
|
21
|
+
static serialize(object: JsonValue): JsonParserResult;
|
|
22
|
+
/**
|
|
23
|
+
* Parse the given string value with superjson using the given metadata
|
|
24
|
+
*
|
|
25
|
+
* @param value - The string value to parse
|
|
26
|
+
* @returns The parsed data
|
|
27
|
+
*/
|
|
28
|
+
static parse<TData = unknown>(value: string): TData;
|
|
29
|
+
/**
|
|
30
|
+
* Stringify the given value with superjson
|
|
31
|
+
*
|
|
32
|
+
*/
|
|
33
|
+
static stringify(obj: any): string;
|
|
34
|
+
/**
|
|
35
|
+
* Serializes the given data to a JSON string.
|
|
36
|
+
* By default the JSON string is formatted with a 2 space indentation to be easy readable.
|
|
37
|
+
*
|
|
38
|
+
* @param json - Object which should be serialized to JSON
|
|
39
|
+
* @param options - JSON serialize options
|
|
40
|
+
* @returns the formatted JSON representation of the object
|
|
41
|
+
*/
|
|
42
|
+
static stringifyJson(json: any, options?: JsonSerializeOptions): string;
|
|
43
|
+
/**
|
|
44
|
+
* Parses the given JSON string and returns the object the JSON content represents.
|
|
45
|
+
* By default javascript-style comments and trailing commas are allowed.
|
|
46
|
+
*
|
|
47
|
+
* @param strData - JSON content as string
|
|
48
|
+
* @param options - JSON parse options
|
|
49
|
+
* @returns Object the JSON content represents
|
|
50
|
+
*/
|
|
51
|
+
static parseJson<TData = unknown>(strData: string, options?: JsonParseOptions): TData;
|
|
52
|
+
/**
|
|
53
|
+
* Register a custom schema with superjson
|
|
54
|
+
*
|
|
55
|
+
* @param name - The name of the schema
|
|
56
|
+
* @param serialize - The function to serialize the schema
|
|
57
|
+
* @param deserialize - The function to deserialize the schema
|
|
58
|
+
* @param isApplicable - The function to check if the schema is applicable
|
|
59
|
+
*/
|
|
60
|
+
static register<TData = any, TJsonObject extends JsonValue = JsonValue>(name: string, serialize: (data: TData) => TJsonObject, deserialize: (json: TJsonObject) => TData, isApplicable: (data: any) => data is TData): void;
|
|
61
|
+
/**
|
|
62
|
+
* Register a class with superjson
|
|
63
|
+
*
|
|
64
|
+
* @param classConstructor - The class constructor to register
|
|
65
|
+
*/
|
|
66
|
+
static registerClass(classConstructor: Class, options?: {
|
|
67
|
+
identifier?: string;
|
|
68
|
+
allowProps?: string[];
|
|
69
|
+
} | string): void;
|
|
70
|
+
private constructor();
|
|
71
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{isObject as l}from"@stryke/types/type-checks/is-object";import{isString as c}from"@stryke/types/type-checks/is-string";import{Buffer as a}from"buffer/";import{parse as u}from"jsonc-parser";import p from"superjson";import{formatParseError as f}from"./utils/parse-error";import{stringify as n}from"./utils/stringify";export class StormJSON extends p{static#e;static get instance(){return StormJSON.#e||(StormJSON.#e=new StormJSON),StormJSON.#e}static deserialize(e){return StormJSON.instance.deserialize(e)}static serialize(e){return StormJSON.instance.serialize(e)}static parse(e){return StormJSON.instance.parse(e)}static stringify(e){const r=StormJSON.instance.customTransformerRegistry.findApplicable(e);let s=e;return r&&(s=r.serialize(s)),n(s)}static stringifyJson(e,r){const s=StormJSON.instance.customTransformerRegistry.findApplicable(e);let t=e;return s&&(t=s.serialize(t)),n(t,r?.spaces??2)}static parseJson(e,r){try{if(r?.expectComments===!1)return StormJSON.instance.parse(e)}catch{}const s=[],t={allowTrailingComma:!0,...r},o=u(e,s,t);if(s.length>0&&s[0])throw new Error(f(e,s[0]));return o}static register(e,r,s,t){StormJSON.instance.registerCustom({isApplicable:t,serialize:r,deserialize:s},e)}static registerClass(e,r){StormJSON.instance.registerClass(e,{identifier:c(r)?r:r?.identifier||e.name,allowProps:r&&l(r)&&r?.allowProps&&Array.isArray(r.allowProps)?r.allowProps:["__typename"]})}constructor(){super({dedupe:!0})}}StormJSON.instance.registerCustom({isApplicable:i=>a.isBuffer(i),serialize:i=>i.toString("base64"),deserialize:i=>a.from(i,"base64")},"Bytes");
|
package/dist/types.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { ParseOptions } from "jsonc-parser";
|
|
2
|
+
export type PrimitiveJsonValue = string | number | boolean | undefined | null;
|
|
3
|
+
export type Class = {
|
|
4
|
+
new (...args: any[]): any;
|
|
5
|
+
};
|
|
6
|
+
export type JsonValue = PrimitiveJsonValue | JsonArray | JsonObject;
|
|
7
|
+
export type JsonArray = Array<JsonValue>;
|
|
8
|
+
export interface JsonObject {
|
|
9
|
+
[key: string]: JsonValue;
|
|
10
|
+
}
|
|
11
|
+
export type ClassInstance = any;
|
|
12
|
+
export type SerializableJsonValue = symbol | Set<JsonValue> | Map<JsonValue, JsonValue> | undefined | bigint | Date | ClassInstance | RegExp;
|
|
13
|
+
export type Tree<T> = InnerNode<T> | Leaf<T>;
|
|
14
|
+
export type Leaf<T> = [T];
|
|
15
|
+
export type InnerNode<T> = [T, Record<string, Tree<T>>];
|
|
16
|
+
export type PrimitiveTypeAnnotation = "number" | "undefined" | "bigint";
|
|
17
|
+
export type LeafTypeAnnotation = PrimitiveTypeAnnotation | "regexp" | "Date" | "Error" | "URL";
|
|
18
|
+
export type TypedArrayAnnotation = ["typed-array", string];
|
|
19
|
+
export type ClassTypeAnnotation = ["class", string];
|
|
20
|
+
export type SymbolTypeAnnotation = ["symbol", string];
|
|
21
|
+
export type CustomTypeAnnotation = ["custom", string];
|
|
22
|
+
export type SimpleTypeAnnotation = LeafTypeAnnotation | "map" | "set";
|
|
23
|
+
export type CompositeTypeAnnotation = TypedArrayAnnotation | ClassTypeAnnotation | SymbolTypeAnnotation | CustomTypeAnnotation;
|
|
24
|
+
export type TypeAnnotation = SimpleTypeAnnotation | CompositeTypeAnnotation;
|
|
25
|
+
export interface JsonParseOptions extends ParseOptions {
|
|
26
|
+
/**
|
|
27
|
+
* Expect JSON with javascript-style
|
|
28
|
+
*
|
|
29
|
+
* @defaultValue false
|
|
30
|
+
*/
|
|
31
|
+
expectComments?: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Disallow javascript-style
|
|
34
|
+
*
|
|
35
|
+
* @defaultValue false
|
|
36
|
+
*/
|
|
37
|
+
disallowComments?: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Allow trailing commas in the JSON content
|
|
40
|
+
*/
|
|
41
|
+
allowTrailingComma?: boolean;
|
|
42
|
+
}
|
|
43
|
+
export interface JsonSerializeOptions {
|
|
44
|
+
/**
|
|
45
|
+
* the whitespaces to add as indentation to make the output more readable.
|
|
46
|
+
*
|
|
47
|
+
* @defaultValue 2
|
|
48
|
+
*/
|
|
49
|
+
spaces?: number;
|
|
50
|
+
}
|
|
51
|
+
export interface JsonParserResult {
|
|
52
|
+
json: JsonValue;
|
|
53
|
+
meta?: {
|
|
54
|
+
values?: Tree<TypeAnnotation> | Record<string, Tree<TypeAnnotation>> | undefined;
|
|
55
|
+
referentialEqualities?: Record<string, string[]> | [string[]] | [string[], Record<string, string[]>];
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
export interface IJsonParser {
|
|
59
|
+
parse: <TData = any>(strData: string) => TData;
|
|
60
|
+
stringify: <TData = any>(data: TData) => string;
|
|
61
|
+
serialize: (object: JsonValue) => JsonParserResult;
|
|
62
|
+
deserialize: <TData = any>(payload: JsonParserResult) => TData;
|
|
63
|
+
register: <TData = any, TJsonValue extends JsonValue = JsonValue>(name: string, serialize: (object: JsonValue) => TJsonValue, deserialize: (payload: TJsonValue) => TData, isApplicable: (data: any) => data is TData) => void;
|
|
64
|
+
}
|
package/dist/types.mjs
ADDED
|
File without changes
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.codeFrameColumns = codeFrameColumns;
|
|
7
|
+
const p = /\r\n|[\n\r\u2028\u2029]/;
|
|
8
|
+
function N(s, l, a = {}) {
|
|
9
|
+
const c = {
|
|
10
|
+
column: 0,
|
|
11
|
+
line: -1,
|
|
12
|
+
...s.start
|
|
13
|
+
},
|
|
14
|
+
m = {
|
|
15
|
+
...c,
|
|
16
|
+
...s.end
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
linesAbove: h = 2,
|
|
20
|
+
linesBelow: f = 3
|
|
21
|
+
} = a || {},
|
|
22
|
+
o = c.line,
|
|
23
|
+
t = c.column,
|
|
24
|
+
b = m.line,
|
|
25
|
+
i = m.column;
|
|
26
|
+
let d = Math.max(o - (h + 1), 0),
|
|
27
|
+
u = Math.min(l.length, b + f);
|
|
28
|
+
o === -1 && (d = 0), b === -1 && (u = l.length);
|
|
29
|
+
const L = b - o,
|
|
30
|
+
e = {};
|
|
31
|
+
if (L) for (let n = 0; n <= L; n++) {
|
|
32
|
+
const r = n + o;
|
|
33
|
+
if (!t) e[r] = !0;else if (n === 0) {
|
|
34
|
+
const g = l[r - 1]?.length ?? 0;
|
|
35
|
+
e[r] = [t, g - t + 1];
|
|
36
|
+
} else if (n === L) e[r] = [0, i];else {
|
|
37
|
+
const g = l[r - n]?.length ?? 0;
|
|
38
|
+
e[r] = [0, g];
|
|
39
|
+
}
|
|
40
|
+
} else t === i ? e[o] = t ? [t, 0] : !0 : e[o] = [t, i - t];
|
|
41
|
+
return {
|
|
42
|
+
start: d,
|
|
43
|
+
end: u,
|
|
44
|
+
markerLines: e
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function codeFrameColumns(s, l, a = {}) {
|
|
48
|
+
const c = s.split(p),
|
|
49
|
+
{
|
|
50
|
+
start: m,
|
|
51
|
+
end: h,
|
|
52
|
+
markerLines: f
|
|
53
|
+
} = N(l, c, a),
|
|
54
|
+
o = String(h).length;
|
|
55
|
+
return (a.highlight ? a.highlight(s) : s).split(p).slice(m, h).map((i, d) => {
|
|
56
|
+
const u = m + 1 + d,
|
|
57
|
+
e = ` ${` ${u}`.slice(-o)} | `,
|
|
58
|
+
n = !!(f[u] ?? !1);
|
|
59
|
+
if (n) {
|
|
60
|
+
let r = "";
|
|
61
|
+
if (Array.isArray(n)) {
|
|
62
|
+
const g = i.slice(0, Math.max(n[0] - 1, 0)).replace(/[^\t]/g, " "),
|
|
63
|
+
k = n[1] || 1;
|
|
64
|
+
r = [`
|
|
65
|
+
`, e.replace(/\d/g, " "), g, "^".repeat(k)].join("");
|
|
66
|
+
}
|
|
67
|
+
return [">", e, i, r].join("");
|
|
68
|
+
}
|
|
69
|
+
return ` ${e}${i}`;
|
|
70
|
+
}).join(`
|
|
71
|
+
`);
|
|
72
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
type Location = {
|
|
2
|
+
column: number;
|
|
3
|
+
line: number;
|
|
4
|
+
};
|
|
5
|
+
type NodeLocation = {
|
|
6
|
+
end?: Location;
|
|
7
|
+
start?: Location;
|
|
8
|
+
};
|
|
9
|
+
export declare function codeFrameColumns(rawLines: string, loc: NodeLocation, opts?: {
|
|
10
|
+
linesAbove?: number;
|
|
11
|
+
linesBelow?: number;
|
|
12
|
+
highlight?: (rawLines: string) => string;
|
|
13
|
+
}): string;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
const p=/\r\n|[\n\r\u2028\u2029]/;function N(s,l,a={}){const c={column:0,line:-1,...s.start},m={...c,...s.end},{linesAbove:h=2,linesBelow:f=3}=a||{},o=c.line,t=c.column,b=m.line,i=m.column;let d=Math.max(o-(h+1),0),u=Math.min(l.length,b+f);o===-1&&(d=0),b===-1&&(u=l.length);const L=b-o,e={};if(L)for(let n=0;n<=L;n++){const r=n+o;if(!t)e[r]=!0;else if(n===0){const g=l[r-1]?.length??0;e[r]=[t,g-t+1]}else if(n===L)e[r]=[0,i];else{const g=l[r-n]?.length??0;e[r]=[0,g]}}else t===i?e[o]=t?[t,0]:!0:e[o]=[t,i-t];return{start:d,end:u,markerLines:e}}export function codeFrameColumns(s,l,a={}){const c=s.split(p),{start:m,end:h,markerLines:f}=N(l,c,a),o=String(h).length;return(a.highlight?a.highlight(s):s).split(p).slice(m,h).map((i,d)=>{const u=m+1+d,e=` ${` ${u}`.slice(-o)} | `,n=!!(f[u]??!1);if(n){let r="";if(Array.isArray(n)){const g=i.slice(0,Math.max(n[0]-1,0)).replace(/[^\t]/g," "),k=n[1]||1;r=[`
|
|
2
|
+
`,e.replace(/\d/g," "),g,"^".repeat(k)].join("")}return[">",e,i,r].join("")}return` ${e}${i}`}).join(`
|
|
3
|
+
`)}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
var _codeFrames = require("./code-frames.cjs");
|
|
7
|
+
Object.keys(_codeFrames).forEach(function (key) {
|
|
8
|
+
if (key === "default" || key === "__esModule") return;
|
|
9
|
+
if (key in exports && exports[key] === _codeFrames[key]) return;
|
|
10
|
+
Object.defineProperty(exports, key, {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
get: function () {
|
|
13
|
+
return _codeFrames[key];
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
var _parseError = require("./parse-error.cjs");
|
|
18
|
+
Object.keys(_parseError).forEach(function (key) {
|
|
19
|
+
if (key === "default" || key === "__esModule") return;
|
|
20
|
+
if (key in exports && exports[key] === _parseError[key]) return;
|
|
21
|
+
Object.defineProperty(exports, key, {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
get: function () {
|
|
24
|
+
return _parseError[key];
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
var _stringify = require("./stringify.cjs");
|
|
29
|
+
Object.keys(_stringify).forEach(function (key) {
|
|
30
|
+
if (key === "default" || key === "__esModule") return;
|
|
31
|
+
if (key in exports && exports[key] === _stringify[key]) return;
|
|
32
|
+
Object.defineProperty(exports, key, {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
get: function () {
|
|
35
|
+
return _stringify[key];
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
var _stripComments = require("./strip-comments.cjs");
|
|
40
|
+
Object.keys(_stripComments).forEach(function (key) {
|
|
41
|
+
if (key === "default" || key === "__esModule") return;
|
|
42
|
+
if (key in exports && exports[key] === _stripComments[key]) return;
|
|
43
|
+
Object.defineProperty(exports, key, {
|
|
44
|
+
enumerable: true,
|
|
45
|
+
get: function () {
|
|
46
|
+
return _stripComments[key];
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export*from"./code-frames";export*from"./parse-error";export*from"./stringify";export*from"./strip-comments";
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.formatParseError = formatParseError;
|
|
7
|
+
var _jsoncParser = require("jsonc-parser");
|
|
8
|
+
var _linesAndColumns = require("lines-and-columns");
|
|
9
|
+
var _codeFrames = require("./code-frames.cjs");
|
|
10
|
+
function formatParseError(e, t) {
|
|
11
|
+
const {
|
|
12
|
+
error: m,
|
|
13
|
+
offset: s,
|
|
14
|
+
length: l
|
|
15
|
+
} = t;
|
|
16
|
+
let n = new _linesAndColumns.LinesAndColumns(e).locationForIndex(s),
|
|
17
|
+
r = n?.line ?? 0,
|
|
18
|
+
o = n?.column ?? 0;
|
|
19
|
+
return r++, o++, `${(0, _jsoncParser.printParseErrorCode)(m)} in JSON at ${r}:${o}
|
|
20
|
+
` + (0, _codeFrames.codeFrameColumns)(e, {
|
|
21
|
+
start: {
|
|
22
|
+
line: r,
|
|
23
|
+
column: o
|
|
24
|
+
},
|
|
25
|
+
end: {
|
|
26
|
+
line: r,
|
|
27
|
+
column: o + l
|
|
28
|
+
}
|
|
29
|
+
}) + `
|
|
30
|
+
`;
|
|
31
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ParseError } from "jsonc-parser";
|
|
2
|
+
/**
|
|
3
|
+
* Nicely formats a JSON error with context
|
|
4
|
+
*
|
|
5
|
+
* @param input - JSON content as string
|
|
6
|
+
* @param parseError - jsonc ParseError
|
|
7
|
+
* @returns
|
|
8
|
+
*/
|
|
9
|
+
export declare function formatParseError(input: string, parseError: ParseError): string;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import{printParseErrorCode as i}from"jsonc-parser";import{LinesAndColumns as a}from"lines-and-columns";import{codeFrameColumns as c}from"./code-frames";export function formatParseError(e,t){const{error:m,offset:s,length:l}=t;let n=new a(e).locationForIndex(s),r=n?.line??0,o=n?.column??0;return r++,o++,`${i(m)} in JSON at ${r}:${o}
|
|
2
|
+
`+c(e,{start:{line:r,column:o},end:{line:r,column:o+l}})+`
|
|
3
|
+
`}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.stringify = void 0;
|
|
7
|
+
var _isNumber = require("@stryke/types/type-checks/is-number");
|
|
8
|
+
const stringify = (r, t = " ") => {
|
|
9
|
+
const n = (0, _isNumber.isNumber)(t) ? " ".repeat(t) : t;
|
|
10
|
+
switch (r) {
|
|
11
|
+
case null:
|
|
12
|
+
return "!n";
|
|
13
|
+
case void 0:
|
|
14
|
+
return "!u";
|
|
15
|
+
case !0:
|
|
16
|
+
return "!t";
|
|
17
|
+
case !1:
|
|
18
|
+
return "!f";
|
|
19
|
+
}
|
|
20
|
+
if (Array.isArray(r)) return `[${n}${r.map(e => stringify(e, n)).join("," + n)}${n}]`;
|
|
21
|
+
if (r instanceof Uint8Array) return `${r}`;
|
|
22
|
+
switch (typeof r) {
|
|
23
|
+
case "number":
|
|
24
|
+
return `${r}`;
|
|
25
|
+
case "string":
|
|
26
|
+
return JSON.stringify(r);
|
|
27
|
+
case "object":
|
|
28
|
+
{
|
|
29
|
+
const e = Object.keys(r);
|
|
30
|
+
return `{${n}${e.map(s => `${s}${n}=${n}${stringify(r[s], n)}`).join("," + n)}${n}}`;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return "?";
|
|
34
|
+
};
|
|
35
|
+
exports.stringify = stringify;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stringify a value to a JSON-like string.
|
|
3
|
+
*
|
|
4
|
+
* @param value - The value to stringify
|
|
5
|
+
* @param spacing - The spacing to use for the stringification
|
|
6
|
+
* @returns The stringified value
|
|
7
|
+
*/
|
|
8
|
+
export declare const stringify: (value: unknown, spacing?: string | number) => string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{isNumber as i}from"@stryke/types/type-checks/is-number";export const stringify=(r,t=" ")=>{const n=i(t)?" ".repeat(t):t;switch(r){case null:return"!n";case void 0:return"!u";case!0:return"!t";case!1:return"!f"}if(Array.isArray(r))return`[${n}${r.map(e=>stringify(e,n)).join(","+n)}${n}]`;if(r instanceof Uint8Array)return`${r}`;switch(typeof r){case"number":return`${r}`;case"string":return JSON.stringify(r);case"object":{const e=Object.keys(r);return`{${n}${e.map(s=>`${s}${n}=${n}${stringify(r[s],n)}`).join(","+n)}${n}}`}}return"?"};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
Object.defineProperty(exports, "stripComments", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function () {
|
|
9
|
+
return _jsoncParser.stripComments;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
var _jsoncParser = require("jsonc-parser");
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { stripComments } from "jsonc-parser";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export{stripComments}from"jsonc-parser";
|