@reliverse/relifso 1.4.0 → 1.4.2
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/LICENSES +16 -0
- package/README.md +48 -17
- package/bin/impl/bun.d.ts +5 -28
- package/bin/impl/bun.js +2 -126
- package/bin/impl/copy.js +8 -7
- package/bin/impl/create.d.ts +34 -0
- package/bin/impl/create.js +54 -0
- package/bin/impl/dive.d.ts +10 -0
- package/bin/impl/dive.js +89 -0
- package/bin/impl/empty.d.ts +28 -0
- package/bin/impl/empty.js +75 -0
- package/bin/impl/extras.d.ts +22 -2
- package/bin/impl/extras.js +68 -3
- package/bin/impl/json-utils.d.ts +30 -0
- package/bin/impl/json-utils.js +46 -0
- package/bin/impl/output-file.d.ts +3 -2
- package/bin/impl/output-json.d.ts +7 -2
- package/bin/impl/output-json.js +73 -11
- package/bin/impl/read-file.d.ts +15 -4
- package/bin/impl/read-file.js +88 -10
- package/bin/impl/read-json.d.ts +6 -0
- package/bin/impl/read-json.js +133 -21
- package/bin/impl/stats.d.ts +31 -0
- package/bin/impl/stats.js +141 -0
- package/bin/impl/write-file.d.ts +19 -8
- package/bin/impl/write-file.js +153 -24
- package/bin/impl/write-json.d.ts +13 -2
- package/bin/impl/write-json.js +46 -7
- package/bin/mod.d.ts +84 -36
- package/bin/mod.js +108 -39
- package/bin/utils/json/helpers/JSONRepairError.d.ts +4 -0
- package/bin/utils/json/helpers/JSONRepairError.js +7 -0
- package/bin/utils/json/helpers/JsonSchemaError.d.ts +6 -0
- package/bin/utils/json/helpers/JsonSchemaError.js +6 -0
- package/bin/utils/json/helpers/stringUtils.d.ts +64 -0
- package/bin/utils/json/helpers/stringUtils.js +87 -0
- package/bin/utils/json/regular/jsonc.d.ts +45 -0
- package/bin/utils/json/regular/jsonc.js +88 -0
- package/bin/utils/json/regular/jsonrepair.d.ts +17 -0
- package/bin/utils/json/regular/jsonrepair.js +576 -0
- package/bin/utils/json/regular/validate.d.ts +22 -0
- package/bin/utils/json/regular/validate.js +52 -0
- package/bin/utils/json/stream/JsonStreamError.d.ts +6 -0
- package/bin/utils/json/stream/JsonStreamError.js +6 -0
- package/bin/utils/json/stream/buffer/InputBuffer.d.ts +13 -0
- package/bin/utils/json/stream/buffer/InputBuffer.js +68 -0
- package/bin/utils/json/stream/buffer/OutputBuffer.d.ts +17 -0
- package/bin/utils/json/stream/buffer/OutputBuffer.js +101 -0
- package/bin/utils/json/stream/core.d.ts +10 -0
- package/bin/utils/json/stream/core.js +695 -0
- package/bin/utils/json/stream/jsonl.d.ts +21 -0
- package/bin/utils/json/stream/jsonl.js +55 -0
- package/bin/utils/json/stream/parser.d.ts +14 -0
- package/bin/utils/json/stream/parser.js +81 -0
- package/bin/utils/json/stream/stack.d.ts +19 -0
- package/bin/utils/json/stream/stack.js +43 -0
- package/bin/utils/json/stream/stream.d.ts +6 -0
- package/bin/utils/json/stream/stream.js +30 -0
- package/bin/utils/json/stream/writer.d.ts +14 -0
- package/bin/utils/json/stream/writer.js +44 -0
- package/package.json +3 -2
- package/bin/impl/create-file.d.ts +0 -2
- package/bin/impl/create-file.js +0 -21
- package/bin/impl/dive-async.d.ts +0 -11
- package/bin/impl/dive-async.js +0 -88
- package/bin/impl/empty-dir.d.ts +0 -2
- package/bin/impl/empty-dir.js +0 -24
- package/bin/impl/file-utils.d.ts +0 -20
- package/bin/impl/file-utils.js +0 -63
- /package/bin/{impl/logger.d.ts → utils/log.d.ts} +0 -0
- /package/bin/{impl/logger.js → utils/log.js} +0 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
const codeSpace = 32;
|
|
2
|
+
const codeNewline = 10;
|
|
3
|
+
const codeTab = 9;
|
|
4
|
+
const codeReturn = 13;
|
|
5
|
+
const codeNonBreakingSpace = 160;
|
|
6
|
+
const codeEnQuad = 8192;
|
|
7
|
+
const codeHairSpace = 8202;
|
|
8
|
+
const codeNarrowNoBreakSpace = 8239;
|
|
9
|
+
const codeMediumMathematicalSpace = 8287;
|
|
10
|
+
const codeIdeographicSpace = 12288;
|
|
11
|
+
export function isHex(char) {
|
|
12
|
+
return /^[0-9A-Fa-f]$/.test(char);
|
|
13
|
+
}
|
|
14
|
+
export function isDigit(char) {
|
|
15
|
+
return char >= "0" && char <= "9";
|
|
16
|
+
}
|
|
17
|
+
export function isValidStringCharacter(char) {
|
|
18
|
+
return char >= " ";
|
|
19
|
+
}
|
|
20
|
+
export function isDelimiter(char) {
|
|
21
|
+
return ",:[]/{}()\n+".includes(char);
|
|
22
|
+
}
|
|
23
|
+
export function isFunctionNameCharStart(char) {
|
|
24
|
+
return char >= "a" && char <= "z" || char >= "A" && char <= "Z" || char === "_" || char === "$";
|
|
25
|
+
}
|
|
26
|
+
export function isFunctionNameChar(char) {
|
|
27
|
+
return char >= "a" && char <= "z" || char >= "A" && char <= "Z" || char === "_" || char === "$" || char >= "0" && char <= "9";
|
|
28
|
+
}
|
|
29
|
+
export const regexUrlStart = /^(http|https|ftp|mailto|file|data|irc):\/\/$/;
|
|
30
|
+
export const regexUrlChar = /^[A-Za-z0-9-._~:/?#@!$&'()*+;=]$/;
|
|
31
|
+
export function isUnquotedStringDelimiter(char) {
|
|
32
|
+
return ",[]/{}\n+".includes(char);
|
|
33
|
+
}
|
|
34
|
+
export function isStartOfValue(char) {
|
|
35
|
+
return isQuote(char) || regexStartOfValue.test(char);
|
|
36
|
+
}
|
|
37
|
+
const regexStartOfValue = /^[[{\w-]$/;
|
|
38
|
+
export function isControlCharacter(char) {
|
|
39
|
+
return char === "\n" || char === "\r" || char === " " || char === "\b" || char === "\f";
|
|
40
|
+
}
|
|
41
|
+
export function isWhitespace(text, index) {
|
|
42
|
+
const code = text.charCodeAt(index);
|
|
43
|
+
return code === codeSpace || code === codeNewline || code === codeTab || code === codeReturn;
|
|
44
|
+
}
|
|
45
|
+
export function isWhitespaceExceptNewline(text, index) {
|
|
46
|
+
const code = text.charCodeAt(index);
|
|
47
|
+
return code === codeSpace || code === codeTab || code === codeReturn;
|
|
48
|
+
}
|
|
49
|
+
export function isSpecialWhitespace(text, index) {
|
|
50
|
+
const code = text.charCodeAt(index);
|
|
51
|
+
return code === codeNonBreakingSpace || code >= codeEnQuad && code <= codeHairSpace || code === codeNarrowNoBreakSpace || code === codeMediumMathematicalSpace || code === codeIdeographicSpace;
|
|
52
|
+
}
|
|
53
|
+
export function isQuote(char) {
|
|
54
|
+
return isDoubleQuoteLike(char) || isSingleQuoteLike(char);
|
|
55
|
+
}
|
|
56
|
+
export function isDoubleQuoteLike(char) {
|
|
57
|
+
return char === '"' || char === "\u201C" || char === "\u201D";
|
|
58
|
+
}
|
|
59
|
+
export function isDoubleQuote(char) {
|
|
60
|
+
return char === '"';
|
|
61
|
+
}
|
|
62
|
+
export function isSingleQuoteLike(char) {
|
|
63
|
+
return char === "'" || char === "\u2018" || char === "\u2019" || char === "`" || char === "\xB4";
|
|
64
|
+
}
|
|
65
|
+
export function isSingleQuote(char) {
|
|
66
|
+
return char === "'";
|
|
67
|
+
}
|
|
68
|
+
export function stripLastOccurrence(text, textToStrip, stripRemainingText = false) {
|
|
69
|
+
const index = text.lastIndexOf(textToStrip);
|
|
70
|
+
return index !== -1 ? text.substring(0, index) + (stripRemainingText ? "" : text.substring(index + 1)) : text;
|
|
71
|
+
}
|
|
72
|
+
export function insertBeforeLastWhitespace(text, textToInsert) {
|
|
73
|
+
let index = text.length;
|
|
74
|
+
if (!isWhitespace(text, index - 1)) {
|
|
75
|
+
return text + textToInsert;
|
|
76
|
+
}
|
|
77
|
+
while (isWhitespace(text, index - 1)) {
|
|
78
|
+
index--;
|
|
79
|
+
}
|
|
80
|
+
return text.substring(0, index) + textToInsert + text.substring(index);
|
|
81
|
+
}
|
|
82
|
+
export function removeAtIndex(text, start, count) {
|
|
83
|
+
return text.substring(0, start) + text.substring(start + count);
|
|
84
|
+
}
|
|
85
|
+
export function endsWithCommaOrNewline(text) {
|
|
86
|
+
return /[,\n][ \t\r]*$/.test(text);
|
|
87
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export interface JsoncParseOptions {
|
|
2
|
+
/**
|
|
3
|
+
* Whether to preserve comments in the output
|
|
4
|
+
* @default false
|
|
5
|
+
*/
|
|
6
|
+
preserveComments?: boolean;
|
|
7
|
+
/**
|
|
8
|
+
* Whether to throw an error if the JSONC is invalid
|
|
9
|
+
* @default true
|
|
10
|
+
*/
|
|
11
|
+
throws?: boolean;
|
|
12
|
+
}
|
|
13
|
+
export interface JsoncStringifyOptions {
|
|
14
|
+
/**
|
|
15
|
+
* Whether to include comments in the output
|
|
16
|
+
* @default false
|
|
17
|
+
*/
|
|
18
|
+
includeComments?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Number of spaces to use for indentation
|
|
21
|
+
* @default 2
|
|
22
|
+
*/
|
|
23
|
+
spaces?: number;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Parse a JSONC string into a JavaScript value
|
|
27
|
+
*/
|
|
28
|
+
export declare function parseJsonc(text: string, options?: JsoncParseOptions): unknown;
|
|
29
|
+
/**
|
|
30
|
+
* Convert a JavaScript value to a JSONC string
|
|
31
|
+
*/
|
|
32
|
+
export declare function stringifyJsonc(value: unknown, options?: JsoncStringifyOptions): string;
|
|
33
|
+
/**
|
|
34
|
+
* Check if a string is valid JSONC
|
|
35
|
+
*/
|
|
36
|
+
export declare function isValidJsonc(text: string): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Extract comments from a JSONC string
|
|
39
|
+
*/
|
|
40
|
+
export declare function extractComments(text: string): {
|
|
41
|
+
line: number;
|
|
42
|
+
column: number;
|
|
43
|
+
text: string;
|
|
44
|
+
type: "line" | "block";
|
|
45
|
+
}[];
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { JSONRepairError } from "../helpers/JSONRepairError.js";
|
|
2
|
+
import { jsonrepair } from "./jsonrepair.js";
|
|
3
|
+
export function parseJsonc(text, options = {}) {
|
|
4
|
+
const { throws = true } = options;
|
|
5
|
+
try {
|
|
6
|
+
return JSON.parse(text);
|
|
7
|
+
} catch (_error) {
|
|
8
|
+
try {
|
|
9
|
+
const repaired = jsonrepair(text);
|
|
10
|
+
return JSON.parse(repaired);
|
|
11
|
+
} catch (_error2) {
|
|
12
|
+
if (throws) {
|
|
13
|
+
throw new JSONRepairError("Failed to parse JSONC", -1);
|
|
14
|
+
}
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export function stringifyJsonc(value, options = {}) {
|
|
20
|
+
const { spaces = 2 } = options;
|
|
21
|
+
return JSON.stringify(value, null, spaces);
|
|
22
|
+
}
|
|
23
|
+
export function isValidJsonc(text) {
|
|
24
|
+
try {
|
|
25
|
+
parseJsonc(text, { throws: true });
|
|
26
|
+
return true;
|
|
27
|
+
} catch {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export function extractComments(text) {
|
|
32
|
+
const comments = [];
|
|
33
|
+
let line = 1;
|
|
34
|
+
let column = 0;
|
|
35
|
+
let i = 0;
|
|
36
|
+
while (i < text.length) {
|
|
37
|
+
const char = text[i];
|
|
38
|
+
if (char === "/" && text[i + 1] === "/") {
|
|
39
|
+
const startColumn = column;
|
|
40
|
+
const startLine = line;
|
|
41
|
+
let commentText = "";
|
|
42
|
+
i += 2;
|
|
43
|
+
while (i < text.length && text[i] !== "\n") {
|
|
44
|
+
commentText += text[i];
|
|
45
|
+
i++;
|
|
46
|
+
}
|
|
47
|
+
comments.push({
|
|
48
|
+
line: startLine,
|
|
49
|
+
column: startColumn,
|
|
50
|
+
text: commentText.trim(),
|
|
51
|
+
type: "line"
|
|
52
|
+
});
|
|
53
|
+
} else if (char === "/" && text[i + 1] === "*") {
|
|
54
|
+
const startColumn = column;
|
|
55
|
+
const startLine = line;
|
|
56
|
+
let commentText = "";
|
|
57
|
+
i += 2;
|
|
58
|
+
while (i < text.length - 1 && !(text[i] === "*" && text[i + 1] === "/")) {
|
|
59
|
+
if (text[i] === "\n") {
|
|
60
|
+
line++;
|
|
61
|
+
column = 0;
|
|
62
|
+
} else {
|
|
63
|
+
column++;
|
|
64
|
+
}
|
|
65
|
+
commentText += text[i];
|
|
66
|
+
i++;
|
|
67
|
+
}
|
|
68
|
+
if (i < text.length - 1) {
|
|
69
|
+
i += 2;
|
|
70
|
+
}
|
|
71
|
+
comments.push({
|
|
72
|
+
line: startLine,
|
|
73
|
+
column: startColumn,
|
|
74
|
+
text: commentText.trim(),
|
|
75
|
+
type: "block"
|
|
76
|
+
});
|
|
77
|
+
} else {
|
|
78
|
+
if (char === "\n") {
|
|
79
|
+
line++;
|
|
80
|
+
column = 0;
|
|
81
|
+
} else {
|
|
82
|
+
column++;
|
|
83
|
+
}
|
|
84
|
+
i++;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return comments;
|
|
88
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Repair a string containing an invalid JSON document.
|
|
3
|
+
* For example changes JavaScript notation into JSON notation.
|
|
4
|
+
*
|
|
5
|
+
* Example:
|
|
6
|
+
*
|
|
7
|
+
* try {
|
|
8
|
+
* const json = "{name: 'John'}"
|
|
9
|
+
* const repaired = jsonrepair(json)
|
|
10
|
+
* console.log(repaired)
|
|
11
|
+
* // '{"name": "John"}'
|
|
12
|
+
* } catch (err) {
|
|
13
|
+
* console.error(err)
|
|
14
|
+
* }
|
|
15
|
+
*
|
|
16
|
+
*/
|
|
17
|
+
export declare function jsonrepair(text: string): string;
|