Skip to content

Commit

Permalink
chore: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
luxass committed Jan 25, 2024
1 parent 3c2d54c commit c2fe2d1
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 52 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
luxass,
} from "@luxass/eslint-config";
} from "@luxass/eslint-config"

export default luxass();
export default luxass()
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
22 changes: 11 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -17,7 +17,7 @@ import type {
export function parse<T = Record<string, any>>(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 (_) {}
}

Expand All @@ -33,9 +33,9 @@ export function parse<T = Record<string, any>>(data: string, options?: Options):
export async function parseFile<T = Record<string, any>>(path: string, options?: Options): Promise<T | undefined> {
const content = await readFile(path, {
encoding: "utf-8",
});
})

return parse(content, options);
return parse(content, options)
}

/**
Expand All @@ -50,12 +50,12 @@ export async function parseFile<T = Record<string, any>>(path: string, options?:
export function parseFileSync<T = Record<string, any>>(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 }
4 changes: 2 additions & 2 deletions src/strip.ts
Original file line number Diff line number Diff line change
@@ -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"
48 changes: 24 additions & 24 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -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(`
Expand All @@ -56,10 +56,10 @@ describe("jsonc-parse", () => {
"foo": /* This is also a comment */ "bar",
}
`);
`)
expect(jsonC).toEqual({
bar: "foo",
foo: "bar",
});
});
});
})
})
})
6 changes: 3 additions & 3 deletions tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineConfig } from "tsup";
import { defineConfig } from "tsup"

export default defineConfig({
entry: ["./src/index.ts", "./src/strip.ts"],
Expand All @@ -10,6 +10,6 @@ export default defineConfig({
outExtension(ctx) {
return {
js: ctx.format === "cjs" ? ".cjs" : ".mjs",
};
}
},
});
})

0 comments on commit c2fe2d1

Please sign in to comment.