Skip to content

Commit

Permalink
fix: parsing decimals and scientific notation
Browse files Browse the repository at this point in the history
  • Loading branch information
kricsleo committed Oct 24, 2023
1 parent 3721051 commit 701d67a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const suspectProtoRx =
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*:/;

const JsonSigRx = /^\s*["[{]|^\s*-?\d[\d.]{0,14}\s*$/;
const JsonSigRx = /^\s*["[{]|^\s*-?\d{1,16}(\.\d{1,17})?([Ee][+-]?\d+)?\s*$/;

function jsonParseTransform(key: string, value: any): any {
if (
Expand Down
18 changes: 18 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,22 @@ describe("destr", () => {
);
}
});

it("parses number", () => {
const testCases: Array<{ input: string; output: unknown }> = [
{ input: "9", output: 9 },
{ input: "-9", output: -9 },
{ input: "0.30000000000000004", output: 0.300_000_000_000_000_04 },
{ input: "9007199254740991", output: 9_007_199_254_740_991 },
{ input: "9e2", output: 900 },
{ input: "9e+2", output: 900 },
{ input: "9e-2", output: 0.09 },
{ input: "9.9e10000", output: Number.POSITIVE_INFINITY },
{ input: "9.9e-10000", output: 0 },
];

for (const testCase of testCases) {
expect(destr(testCase.input)).toStrictEqual(testCase.output);
}
});
});

0 comments on commit 701d67a

Please sign in to comment.