@ac-kit/format-json5 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2024-2026 Anthony Champagne and contributors
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # @ac-kit/format-json5
2
+
3
+ JSON5 — JSON relaxed for humans to write by hand: comments, trailing commas,
4
+ unquoted keys, single-quoted strings, hexadecimal numbers and `Infinity`.
5
+
6
+ It is what a configuration file wants to be when JSON's strictness makes it
7
+ hostile to edit.
8
+
9
+ ```ts
10
+ import { parseJson5, printJson5 } from "@ac-kit/format-json5";
11
+
12
+ parseJson5(`{
13
+ // trailing commas and comments are fine here
14
+ port: 8080,
15
+ }`);
16
+ ```
17
+
18
+ Same surface as the rest of the JSON family: `parseJson5`, `printJson5`,
19
+ `editJson5`/`createJson5Edits` for changing one value in place, and
20
+ `Json5ParseStream`/`Json5PrintStream` as `TransformStream`s over
21
+ `@ac-kit/format-core`.
22
+
23
+ Portable: no Node.js built-ins, so it runs unchanged in browsers, Deno, Bun and
24
+ edge runtimes.
25
+
26
+ See the
27
+ [generated API docs](https://anthochamp.github.io/node-essentials/api/format-json5/)
28
+ for the full reference.
package/dist/edit.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { PropertyPath, TextEdit } from "@ac-kit/core";
2
+ import { type Json5PrintOptions } from "./print.js";
3
+ /** Returns a full-replace {@link TextEdit} after setting `value` at `path`. */
4
+ export declare function createJson5Edits(source: string, path: PropertyPath, value: unknown, options?: Json5PrintOptions): TextEdit[];
5
+ /** Sets `value` at `path` in a JSON5 string and returns the updated source. */
6
+ export declare function editJson5(source: string, path: PropertyPath, value: unknown, options?: Json5PrintOptions): string;
7
+ //# sourceMappingURL=edit.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"edit.d.ts","sourceRoot":"","sources":["../src/edit.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,YAAY,EACZ,QAAQ,EAGR,MAAM,cAAc,CAAC;AAGtB,OAAO,EAAc,KAAK,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAEhE,+EAA+E;AAC/E,wBAAgB,gBAAgB,CAC/B,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,YAAY,EAClB,KAAK,EAAE,OAAO,EACd,OAAO,CAAC,EAAE,iBAAiB,GACzB,QAAQ,EAAE,CAKZ;AAED,+EAA+E;AAC/E,wBAAgB,SAAS,CACxB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,YAAY,EAClB,KAAK,EAAE,OAAO,EACd,OAAO,CAAC,EAAE,iBAAiB,GACzB,MAAM,CAER"}
package/dist/edit.js ADDED
@@ -0,0 +1,15 @@
1
+ import { applyTextEdits, setAtPath, } from "@ac-kit/core";
2
+ import json5 from "json5";
3
+ import { printJson5 } from "./print.js";
4
+ /** Returns a full-replace {@link TextEdit} after setting `value` at `path`. */
5
+ export function createJson5Edits(source, path, value, options) {
6
+ const root = json5.parse(source);
7
+ setAtPath(root, path, value);
8
+ const newSource = printJson5(root, options);
9
+ return [{ offset: 0, length: source.length, content: newSource }];
10
+ }
11
+ /** Sets `value` at `path` in a JSON5 string and returns the updated source. */
12
+ export function editJson5(source, path, value, options) {
13
+ return applyTextEdits(source, createJson5Edits(source, path, value, options));
14
+ }
15
+ //# sourceMappingURL=edit.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"edit.js","sourceRoot":"","sources":["../src/edit.ts"],"names":[],"mappings":"AAAA,OAAO,EAGN,cAAc,EACd,SAAS,GACT,MAAM,cAAc,CAAC;AACtB,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,OAAO,EAAE,UAAU,EAA0B,MAAM,YAAY,CAAC;AAEhE,+EAA+E;AAC/E,MAAM,UAAU,gBAAgB,CAC/B,MAAc,EACd,IAAkB,EAClB,KAAc,EACd,OAA2B;IAE3B,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAA0B,MAAM,CAAC,CAAC;IAC1D,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;IAC7B,MAAM,SAAS,GAAG,UAAU,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IAC5C,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,EAAE,SAAS,EAAE,CAAC,CAAC;AACnE,CAAC;AAED,+EAA+E;AAC/E,MAAM,UAAU,SAAS,CACxB,MAAc,EACd,IAAkB,EAClB,KAAc,EACd,OAA2B;IAE3B,OAAO,cAAc,CAAC,MAAM,EAAE,gBAAgB,CAAC,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC;AAC/E,CAAC"}
@@ -0,0 +1,6 @@
1
+ export * from "./edit.js";
2
+ export * from "./parse-stream.js";
3
+ export * from "./parse.js";
4
+ export * from "./print-stream.js";
5
+ export * from "./print.js";
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,mBAAmB,CAAC;AAClC,cAAc,YAAY,CAAC;AAC3B,cAAc,mBAAmB,CAAC;AAClC,cAAc,YAAY,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,6 @@
1
+ export * from "./edit.js";
2
+ export * from "./parse-stream.js";
3
+ export * from "./parse.js";
4
+ export * from "./print-stream.js";
5
+ export * from "./print.js";
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,mBAAmB,CAAC;AAClC,cAAc,YAAY,CAAC;AAC3B,cAAc,mBAAmB,CAAC;AAClC,cAAc,YAAY,CAAC"}
@@ -0,0 +1,6 @@
1
+ import type { JsonValue } from "type-fest";
2
+ /** Buffers a byte stream and emits one parsed JSON5 value on flush. */
3
+ export declare class Json5ParseStream extends TransformStream<Uint8Array, JsonValue> {
4
+ constructor();
5
+ }
6
+ //# sourceMappingURL=parse-stream.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-stream.d.ts","sourceRoot":"","sources":["../src/parse-stream.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAE3C,uEAAuE;AACvE,qBAAa,gBAAiB,SAAQ,eAAe,CAAC,UAAU,EAAE,SAAS,CAAC;IAC3E,cAOC;CACD"}
@@ -0,0 +1,9 @@
1
+ import { wholeTextParseTransformer } from "@ac-kit/format-core";
2
+ import json5 from "json5";
3
+ /** Buffers a byte stream and emits one parsed JSON5 value on flush. */
4
+ export class Json5ParseStream extends TransformStream {
5
+ constructor() {
6
+ super(wholeTextParseTransformer((source) => json5.parse(source), "JSON5"));
7
+ }
8
+ }
9
+ //# sourceMappingURL=parse-stream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-stream.js","sourceRoot":"","sources":["../src/parse-stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,qBAAqB,CAAC;AAChE,OAAO,KAAK,MAAM,OAAO,CAAC;AAG1B,uEAAuE;AACvE,MAAM,OAAO,gBAAiB,SAAQ,eAAsC;IAC3E;QACC,KAAK,CACJ,yBAAyB,CACxB,CAAC,MAAM,EAAE,EAAE,CAAC,KAAK,CAAC,KAAK,CAAY,MAAM,CAAC,EAC1C,OAAO,CACP,CACD,CAAC;IACH,CAAC;CACD"}
@@ -0,0 +1,7 @@
1
+ import type { JsonValue } from "type-fest";
2
+ export type Json5ParseOptions = {
3
+ reviver?: Parameters<typeof JSON.parse>[1];
4
+ };
5
+ /** Parses a JSON5 string into a {@link JsonValue}. */
6
+ export declare function parseJson5(source: string, options?: Json5ParseOptions): JsonValue;
7
+ //# sourceMappingURL=parse.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAE3C,MAAM,MAAM,iBAAiB,GAAG;IAC/B,OAAO,CAAC,EAAE,UAAU,CAAC,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;CAC3C,CAAC;AAEF,sDAAsD;AACtD,wBAAgB,UAAU,CACzB,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,iBAAiB,GACzB,SAAS,CAMX"}
package/dist/parse.js ADDED
@@ -0,0 +1,11 @@
1
+ import json5 from "json5";
2
+ /** Parses a JSON5 string into a {@link JsonValue}. */
3
+ export function parseJson5(source, options) {
4
+ try {
5
+ return json5.parse(source, options?.reviver);
6
+ }
7
+ catch (error) {
8
+ throw new Error("parse JSON5", { cause: error });
9
+ }
10
+ }
11
+ //# sourceMappingURL=parse.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse.js","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAO1B,sDAAsD;AACtD,MAAM,UAAU,UAAU,CACzB,MAAc,EACd,OAA2B;IAE3B,IAAI,CAAC;QACJ,OAAO,KAAK,CAAC,KAAK,CAAY,MAAM,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;IACzD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,MAAM,IAAI,KAAK,CAAC,aAAa,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;IAClD,CAAC;AACF,CAAC"}
@@ -0,0 +1,11 @@
1
+ import type { JsonValue } from "type-fest";
2
+ export type Json5PrintStreamOptions = {
3
+ indent?: number | string;
4
+ replacer?: Parameters<typeof JSON.stringify>[1];
5
+ quote?: string;
6
+ };
7
+ /** Emits each incoming {@link JsonValue} as UTF-8 encoded JSON5. */
8
+ export declare class Json5PrintStream extends TransformStream<JsonValue, Uint8Array> {
9
+ constructor(options?: Json5PrintStreamOptions);
10
+ }
11
+ //# sourceMappingURL=print-stream.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"print-stream.d.ts","sourceRoot":"","sources":["../src/print-stream.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAE3C,MAAM,MAAM,uBAAuB,GAAG;IACrC,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACzB,QAAQ,CAAC,EAAE,UAAU,CAAC,OAAO,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;IAChD,KAAK,CAAC,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,oEAAoE;AACpE,qBAAa,gBAAiB,SAAQ,eAAe,CAAC,SAAS,EAAE,UAAU,CAAC;IAC3E,YAAY,OAAO,GAAE,uBAA4B,EAYhD;CACD"}
@@ -0,0 +1,13 @@
1
+ import { textPrintTransformer } from "@ac-kit/format-core";
2
+ import json5 from "json5";
3
+ /** Emits each incoming {@link JsonValue} as UTF-8 encoded JSON5. */
4
+ export class Json5PrintStream extends TransformStream {
5
+ constructor(options = {}) {
6
+ super(textPrintTransformer((value) => json5.stringify(value, {
7
+ replacer: options.replacer,
8
+ space: options.indent,
9
+ quote: options.quote,
10
+ }), "JSON5"));
11
+ }
12
+ }
13
+ //# sourceMappingURL=print-stream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"print-stream.js","sourceRoot":"","sources":["../src/print-stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAC3D,OAAO,KAAK,MAAM,OAAO,CAAC;AAS1B,oEAAoE;AACpE,MAAM,OAAO,gBAAiB,SAAQ,eAAsC;IAC3E,YAAY,OAAO,GAA4B,EAAE;QAChD,KAAK,CACJ,oBAAoB,CACnB,CAAC,KAAK,EAAE,EAAE,CACT,KAAK,CAAC,SAAS,CAAC,KAAK,EAAE;YACtB,QAAQ,EAAE,OAAO,CAAC,QAAQ;YAC1B,KAAK,EAAE,OAAO,CAAC,MAAM;YACrB,KAAK,EAAE,OAAO,CAAC,KAAK;SACpB,CAAC,EACH,OAAO,CACP,CACD,CAAC;IACH,CAAC;CACD"}
@@ -0,0 +1,8 @@
1
+ export type Json5PrintOptions = {
2
+ indent?: number | string;
3
+ replacer?: Parameters<typeof JSON.stringify>[1];
4
+ quote?: string;
5
+ };
6
+ /** Serializes a value to a JSON5 string. */
7
+ export declare function printJson5(value: unknown, options?: Json5PrintOptions): string;
8
+ //# sourceMappingURL=print.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"print.d.ts","sourceRoot":"","sources":["../src/print.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,iBAAiB,GAAG;IAC/B,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACzB,QAAQ,CAAC,EAAE,UAAU,CAAC,OAAO,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;IAChD,KAAK,CAAC,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,4CAA4C;AAC5C,wBAAgB,UAAU,CACzB,KAAK,EAAE,OAAO,EACd,OAAO,CAAC,EAAE,iBAAiB,GACzB,MAAM,CAUR"}
package/dist/print.js ADDED
@@ -0,0 +1,15 @@
1
+ import json5 from "json5";
2
+ /** Serializes a value to a JSON5 string. */
3
+ export function printJson5(value, options) {
4
+ try {
5
+ return json5.stringify(value, {
6
+ replacer: options?.replacer,
7
+ space: options?.indent,
8
+ quote: options?.quote,
9
+ });
10
+ }
11
+ catch (error) {
12
+ throw new Error("print JSON5", { cause: error });
13
+ }
14
+ }
15
+ //# sourceMappingURL=print.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"print.js","sourceRoot":"","sources":["../src/print.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAQ1B,4CAA4C;AAC5C,MAAM,UAAU,UAAU,CACzB,KAAc,EACd,OAA2B;IAE3B,IAAI,CAAC;QACJ,OAAO,KAAK,CAAC,SAAS,CAAC,KAAK,EAAE;YAC7B,QAAQ,EAAE,OAAO,EAAE,QAAQ;YAC3B,KAAK,EAAE,OAAO,EAAE,MAAM;YACtB,KAAK,EAAE,OAAO,EAAE,KAAK;SACrB,CAAC,CAAC;IACJ,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,MAAM,IAAI,KAAK,CAAC,aAAa,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;IAClD,CAAC;AACF,CAAC"}
package/package.json ADDED
@@ -0,0 +1,67 @@
1
+ {
2
+ "name": "@ac-kit/format-json5",
3
+ "version": "0.1.0",
4
+ "description": "JSON5 parse and print streams over @ac-kit/format-core.",
5
+ "keywords": [
6
+ "codec",
7
+ "json5",
8
+ "parser",
9
+ "streams"
10
+ ],
11
+ "homepage": "https://anthochamp.github.io/node-essentials/api/format-json5/",
12
+ "bugs": {
13
+ "url": "https://github.com/anthochamp/node-essentials/issues"
14
+ },
15
+ "license": "BSD-3-Clause",
16
+ "author": "Anthony Champagne <dev@anthonychampagne.fr>",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/anthochamp/node-essentials.git",
20
+ "directory": "packages/format-json5"
21
+ },
22
+ "files": [
23
+ "dist"
24
+ ],
25
+ "type": "module",
26
+ "sideEffects": false,
27
+ "main": "./dist/index.js",
28
+ "exports": {
29
+ ".": "./dist/index.js",
30
+ "./file": "./dist/file.js"
31
+ },
32
+ "publishConfig": {
33
+ "main": "./dist/index.js",
34
+ "exports": {
35
+ ".": "./dist/index.js",
36
+ "./file": "./dist/file.js"
37
+ },
38
+ "access": "public"
39
+ },
40
+ "scripts": {
41
+ "compile": "tsc -p ./tsconfig.build.json -outDir dist",
42
+ "compile:check": "tsc -noEmit -p ./tsconfig.json",
43
+ "clean": "rimraf dist",
44
+ "build": "yarn clean && yarn compile",
45
+ "test": "yarn run -T vitest run",
46
+ "prepack": "yarn build"
47
+ },
48
+ "dependencies": {
49
+ "@ac-kit/core": "^0.1.0",
50
+ "@ac-kit/format-core": "^0.1.0",
51
+ "@types/node": "^26.5.1",
52
+ "json5": "^2.2.3",
53
+ "type-fest": "^5.8.0"
54
+ },
55
+ "devDependencies": {
56
+ "@ac-kit/tsconfig": "0.1.0",
57
+ "@ac-kit/typedoc-config": "0.1.0",
58
+ "@ac-kit/vitest-config": "0.1.0",
59
+ "@typescript/native": "typescript@7.0.2",
60
+ "rimraf": "6.1.3",
61
+ "typedoc": "0.28.20",
62
+ "typescript": "@typescript/typescript6@6.0.2"
63
+ },
64
+ "engines": {
65
+ "node": ">=24"
66
+ }
67
+ }