Skip to content

Commit 6aa42c6

Browse files
committedJun 12, 2023
chore: lint with prettier
1 parent 4aa3592 commit 6aa42c6

File tree

5 files changed

+55
-23
lines changed

5 files changed

+55
-23
lines changed
 

‎.prettierrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

‎package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"bench": "pnpm build && node ./bench.cjs",
2323
"build": "unbuild",
2424
"dev": "vitest dev",
25-
"lint": "eslint --ext .ts .",
25+
"lint": "eslint --ext .ts . && prettier -c src test",
26+
"lint:fix": "eslint --ext .ts . --fix && prettier -w src test",
2627
"release": "pnpm test && pnpm build && standard-version && git push --follow-tags && pnpm publish",
2728
"test": "pnpm lint && vitest run --coverage"
2829
},
@@ -32,6 +33,7 @@
3233
"benchmark": "^2.1.4",
3334
"eslint": "^8.42.0",
3435
"eslint-config-unjs": "^0.2.1",
36+
"prettier": "^2.8.8",
3537
"secure-json-parse": "^2.7.0",
3638
"standard-version": "^9.5.0",
3739
"typescript": "^5.1.3",

‎pnpm-lock.yaml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/index.ts

+32-13
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,57 @@
11
// https://github.com/fastify/secure-json-parse
22
// https://github.com/hapijs/bourne
3-
const suspectProtoRx = /"(?:_|\\u0{2}5[Ff]){2}(?:p|\\u0{2}70)(?:r|\\u0{2}72)(?:o|\\u0{2}6[Ff])(?:t|\\u0{2}74)(?:o|\\u0{2}6[Ff])(?:_|\\u0{2}5[Ff]){2}"\s*:/;
4-
const suspectConstructorRx = /"(?:c|\\u0063)(?:o|\\u006[Ff])(?:n|\\u006[Ee])(?:s|\\u0073)(?:t|\\u0074)(?:r|\\u0072)(?:u|\\u0075)(?:c|\\u0063)(?:t|\\u0074)(?:o|\\u006[Ff])(?:r|\\u0072)"\s*:/;
3+
const suspectProtoRx =
4+
/"(?:_|\\u0{2}5[Ff]){2}(?:p|\\u0{2}70)(?:r|\\u0{2}72)(?:o|\\u0{2}6[Ff])(?:t|\\u0{2}74)(?:o|\\u0{2}6[Ff])(?:_|\\u0{2}5[Ff]){2}"\s*:/;
5+
const suspectConstructorRx =
6+
/"(?:c|\\u0063)(?:o|\\u006[Ff])(?:n|\\u006[Ee])(?:s|\\u0073)(?:t|\\u0074)(?:r|\\u0072)(?:u|\\u0075)(?:c|\\u0063)(?:t|\\u0074)(?:o|\\u006[Ff])(?:r|\\u0072)"\s*:/;
57

68
const JsonSigRx = /^\s*["[{]|^\s*-?\d[\d.]{0,14}\s*$/;
79

8-
function jsonParseTransform (key: string, value: any): any {
10+
function jsonParseTransform(key: string, value: any): any {
911
if (key === "__proto__") {
1012
return;
1113
}
12-
if (key === "constructor" && value && typeof value === "object" && ("prototype" in value)) {
14+
if (
15+
key === "constructor" &&
16+
value &&
17+
typeof value === "object" &&
18+
"prototype" in value
19+
) {
1320
// Has possible malicious prototype
1421
return;
1522
}
1623
return value;
1724
}
1825

1926
export type Options = {
20-
strict?: boolean
21-
}
27+
strict?: boolean;
28+
};
2229

23-
export default function destr (value: any, options: Options = {}): any {
30+
export default function destr(value: any, options: Options = {}): any {
2431
if (typeof value !== "string") {
2532
return value;
2633
}
2734

2835
const _lval = value.toLowerCase().trim();
29-
if (_lval === "true") { return true; }
30-
if (_lval === "false") { return false; }
36+
if (_lval === "true") {
37+
return true;
38+
}
39+
if (_lval === "false") {
40+
return false;
41+
}
3142
// eslint-disable-next-line unicorn/no-null
32-
if (_lval === "null") { return null; }
33-
if (_lval === "nan") { return Number.NaN; }
34-
if (_lval === "infinity") { return Number.POSITIVE_INFINITY; }
35-
if (_lval === "undefined") { return undefined; }
43+
if (_lval === "null") {
44+
return null;
45+
}
46+
if (_lval === "nan") {
47+
return Number.NaN;
48+
}
49+
if (_lval === "infinity") {
50+
return Number.POSITIVE_INFINITY;
51+
}
52+
if (_lval === "undefined") {
53+
return undefined;
54+
}
3655

3756
if (!JsonSigRx.test(value)) {
3857
if (options.strict) {

‎test/index.test.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe("destr", () => {
1212
/* eslint-disable-next-line unicorn/no-null */
1313
{ input: null },
1414
{ input: Number.POSITIVE_INFINITY },
15-
{ input: undefined }
15+
{ input: undefined },
1616
];
1717

1818
for (const testCase of testCases) {
@@ -55,18 +55,18 @@ describe("destr", () => {
5555
it("parses with surrounding spaces", () => {
5656
expect(destr(" true ")).toBe(true);
5757
expect(destr(" -123 ")).toStrictEqual(-123);
58-
expect(destr(" { \"test\": 123 } ")).toStrictEqual({ test: 123 });
58+
expect(destr(' { "test": 123 } ')).toStrictEqual({ test: 123 });
5959
});
6060

6161
it("parses valid JSON texts", () => {
6262
const testCases = [
6363
{ input: "{}", output: {} },
6464
{ input: "[]", output: [] },
65-
{ input: "{ \"key\": \"value\" }", output: { key: "value" } },
66-
{ input: "{ \"constructor\": \"value\" }", output: { constructor: "value" } },
65+
{ input: '{ "key": "value" }', output: { key: "value" } },
66+
{ input: '{ "constructor": "value" }', output: { constructor: "value" } },
6767
// eslint-disable-next-line unicorn/no-null
6868
{ input: '{ "constructor": null }', output: { constructor: null } },
69-
{ input: "[1,2,3]", output: [1, 2, 3] }
69+
{ input: "[1,2,3]", output: [1, 2, 3] },
7070
];
7171

7272
for (const testCase of testCases) {
@@ -78,7 +78,7 @@ describe("destr", () => {
7878
const testCases = [
7979
{ input: '{ "__proto__": {} }', output: {} },
8080
{ input: '{ "constructor": { "prototype": {} } }', output: {} },
81-
{ input: '{ "constructor": { "prototype": null } }', output: {} }
81+
{ input: '{ "constructor": { "prototype": null } }', output: {} },
8282
];
8383

8484
for (const testCase of testCases) {
@@ -92,7 +92,7 @@ describe("destr", () => {
9292
{ input: "[ " },
9393
{ input: '" ' },
9494
{ input: "[1,2,3]?" },
95-
{ input: "invalid JSON text" }
95+
{ input: "invalid JSON text" },
9696
];
9797

9898
for (const testCase of testCases) {
@@ -106,11 +106,13 @@ describe("destr", () => {
106106
{ input: "[ ", output: "Unexpected end of JSON input" },
107107
{ input: '" ', output: "Unexpected end of JSON input" },
108108
{ input: "[1,2,3]?", output: "Unexpected token" },
109-
{ input: "invalid JSON text", output: "Invalid JSON" }
109+
{ input: "invalid JSON text", output: "Invalid JSON" },
110110
];
111111

112112
for (const testCase of testCases) {
113-
expect(() => destr(testCase.input, { strict: true })).toThrowError(testCase.output || "");
113+
expect(() => destr(testCase.input, { strict: true })).toThrowError(
114+
testCase.output || ""
115+
);
114116
}
115117
});
116118
});

0 commit comments

Comments
 (0)
Please sign in to comment.