diff --git a/README.md b/README.md index 1cec803..f2bc71e 100644 --- a/README.md +++ b/README.md @@ -15,36 +15,36 @@ npm install jsonc-parse ## 📚 Usage ```ts -import { parse, parseFile, parseFileSync } from "jsonc-parse"; +import { parse, parseFile, parseFileSync } from "jsonc-parse" // From file async -const jsonCFile = await parseFile("./config.jsonc"); +const jsonCFile = await parseFile("./config.jsonc") // From file -const jsonCFile = parseFileSync("./config.jsonc"); +const jsonCFile = parseFileSync("./config.jsonc") // From string const jsonC = parse(`{ "bar": "foo", // This is a comment. "foo": /* This is also a comment */ "bar", -}`); +}`) ``` you can also just import the `strip` function to remove comments from a string. ```ts -import { strip } from "jsonc-parse/strip"; +import { strip } from "jsonc-parse/strip" // or -import { strip } from "jsonc-parse"; +import { strip } from "jsonc-parse" const json = strip(`{ "bar": "foo", // This is a comment. "foo": /* This is also a comment */ "bar", -}`); -JSON.parse(strip(json)); // { bar: "foo", foo: "bar" } +}`) +JSON.parse(strip(json)) // { bar: "foo", foo: "bar" } ``` ## 📄 License diff --git a/eslint.config.js b/eslint.config.js index 42cbb38..a9916fc 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -1,5 +1,5 @@ import { luxass, -} from "@luxass/eslint-config"; +} from "@luxass/eslint-config" -export default luxass(); +export default luxass() diff --git a/package.json b/package.json index a6e7d26..8002d50 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,14 @@ { "name": "jsonc-parse", - "type": "module", "version": "1.2.0", - "packageManager": "pnpm@8.14.1", "description": "A lightweight JSON with Comments parser.", + "type": "module", "author": { "name": "Lucas Nørgård", "email": "lucasnrgaard@gmail.com", "url": "https://luxass.dev" }, + "packageManager": "pnpm@8.14.1", "license": "MIT", "homepage": "https://github.com/luxass/jsonc-parse", "repository": { diff --git a/src/index.ts b/src/index.ts index e4114ae..8c8436d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,9 @@ -import { readFile } from "node:fs/promises"; -import { readFileSync } from "node:fs"; -import { strip } from "./strip"; +import { readFile } from "node:fs/promises" +import { readFileSync } from "node:fs" +import { strip } from "./strip" import type { Options, -} from "./strip"; +} from "./strip" /** * Parse a JSON string, removing comments. @@ -17,7 +17,7 @@ import type { export function parse>(data: string, options?: Options): T | undefined { try { // eslint-disable-next-line no-new-func - return new Function(`return ${strip(data, options).trim()}`)(); + return new Function(`return ${strip(data, options).trim()}`)() } catch (_) {} } @@ -33,9 +33,9 @@ export function parse>(data: string, options?: Options): export async function parseFile>(path: string, options?: Options): Promise { const content = await readFile(path, { encoding: "utf-8", - }); + }) - return parse(content, options); + return parse(content, options) } /** @@ -50,12 +50,12 @@ export async function parseFile>(path: string, options?: export function parseFileSync>(path: string, options?: Options): T | undefined { const content = readFileSync(path, { encoding: "utf-8", - }); + }) - return parse(content, options); + return parse(content, options) } export { strip, -}; -export type { Options }; +} +export type { Options } diff --git a/src/strip.ts b/src/strip.ts index 6c2f11c..d8c7c7b 100644 --- a/src/strip.ts +++ b/src/strip.ts @@ -1,4 +1,4 @@ -export { strip } from "@luxass/strip-json-comments"; +export { strip } from "@luxass/strip-json-comments" export type { Options, -} from "@luxass/strip-json-comments"; +} from "@luxass/strip-json-comments" diff --git a/tests/index.test.ts b/tests/index.test.ts index f1a1283..49f3298 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -1,52 +1,52 @@ -import { describe, expect, it } from "vitest"; -import { parse, parseFile, parseFileSync } from "../src"; +import { describe, expect, it } from "vitest" +import { parse, parseFile, parseFileSync } from "../src" describe("jsonc-parse", () => { it("parse jsonc from file (async)", async () => { - const test1 = await parseFile("./tests/test1.jsonc"); - const test2 = await parseFile("./tests/test2.jsonc"); + const test1 = await parseFile("./tests/test1.jsonc") + const test2 = await parseFile("./tests/test2.jsonc") expect(test1?.bb).toEqual([ 1, "Hey!", - ]); - expect(test1?.num).toBe(1); + ]) + expect(test1?.num).toBe(1) - expect(test2?.aa).toBeTypeOf("object"); + expect(test2?.aa).toBeTypeOf("object") expect(test2?.nested).toEqual({ this: { is: { a: "nested", }, }, - }); + }) - expect(test1?.bar).toEqual(test2?.bar); - expect(test1?.foo).toEqual(test2?.foo); - }); + expect(test1?.bar).toEqual(test2?.bar) + expect(test1?.foo).toEqual(test2?.foo) + }) it("parse jsonc from file", () => { - const test1 = parseFileSync("./tests/test1.jsonc"); - const test2 = parseFileSync("./tests/test2.jsonc"); + const test1 = parseFileSync("./tests/test1.jsonc") + const test2 = parseFileSync("./tests/test2.jsonc") expect(test1?.bb).toEqual([ 1, "Hey!", - ]); - expect(test1?.num).toBe(1); + ]) + expect(test1?.num).toBe(1) - expect(test2?.aa).toBeTypeOf("object"); + expect(test2?.aa).toBeTypeOf("object") expect(test2?.nested).toEqual({ this: { is: { a: "nested", }, }, - }); + }) - expect(test1?.bar).toEqual(test2?.bar); - expect(test1?.foo).toEqual(test2?.foo); - }); + expect(test1?.bar).toEqual(test2?.bar) + expect(test1?.foo).toEqual(test2?.foo) + }) it("parse jsonc from string", () => { const jsonC = parse(` @@ -56,10 +56,10 @@ describe("jsonc-parse", () => { "foo": /* This is also a comment */ "bar", } - `); + `) expect(jsonC).toEqual({ bar: "foo", foo: "bar", - }); - }); -}); + }) + }) +}) diff --git a/tsup.config.ts b/tsup.config.ts index b1464ff..2f990ae 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -1,4 +1,4 @@ -import { defineConfig } from "tsup"; +import { defineConfig } from "tsup" export default defineConfig({ entry: ["./src/index.ts", "./src/strip.ts"], @@ -10,6 +10,6 @@ export default defineConfig({ outExtension(ctx) { return { js: ctx.format === "cjs" ? ".cjs" : ".mjs", - }; + } }, -}); +})