diff --git a/src/index.ts b/src/index.ts index 9110722..f5196d8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 ( diff --git a/test/index.test.ts b/test/index.test.ts index 7e4d3fa..261a39c 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -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); + } + }); });