From e12c836af7ebd3374cc6afb318308a0e18185291 Mon Sep 17 00:00:00 2001 From: vigneshakaviki Date: Sat, 11 Apr 2026 15:18:05 -0700 Subject: [PATCH 1/2] test: add regression tests for scientific notation parsing (#284) format() can produce scientific notation for very large values (e.g. "5.696545792019405e+297y"), but parse() returns NaN for these strings. Add tests that verify the roundtrip contract. --- src/index.test.ts | 7 +++++++ src/parse.test.ts | 9 +++++++++ 2 files changed, 16 insertions(+) diff --git a/src/index.test.ts b/src/index.test.ts index 5035ab9..07a4170 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -327,6 +327,13 @@ describe('ms(number)', () => { expect(ms(-234234234)).toBe('-3d'); }); + + it('should roundtrip format/parse for large numbers', () => { + const formatted = ms(Number.MAX_VALUE); + expect(typeof formatted).toBe('string'); + const parsed = ms(formatted as `${number}`); + expect(parsed).not.toBeNaN(); + }); }); // invalid inputs diff --git a/src/parse.test.ts b/src/parse.test.ts index 9182411..e904807 100644 --- a/src/parse.test.ts +++ b/src/parse.test.ts @@ -78,6 +78,15 @@ describe('parse(string)', () => { it('should work with negative decimals starting with "."', () => { expect(parse('-.5h')).toBe(-1800000); }); + + it('should work with scientific notation (roundtrip from format)', () => { + expect(parse('5.696545792019405e+297y')).not.toBeNaN(); + expect(parse('1e3ms')).toBe(1000); + expect(parse('1.5e2s')).toBe(150000); + expect(parse('-1e3ms')).toBe(-1000); + expect(parse('1E3ms')).toBe(1000); + expect(parse('1e+3ms')).toBe(1000); + }); }); // long strings From 5ee8bdfa4a8bcfd48db2cc77a2f127bd4073bb02 Mon Sep 17 00:00:00 2001 From: vigneshakaviki Date: Sat, 11 Apr 2026 15:18:12 -0700 Subject: [PATCH 2/2] fix: support scientific notation in parse() for format() roundtrip (#284) The parse() regex only matched standard decimal notation (-?\d*\.?\d+), so values like "5.696545792019405e+297y" produced by format() for very large numbers would return NaN. Extend the numeric capture group to accept an optional exponent suffix (e[+-]?\d+), matching what JavaScript's Number formatting can produce. parseFloat() already handles scientific notation, so no other changes are needed. --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index d50e3c7..0796c11 100644 --- a/src/index.ts +++ b/src/index.ts @@ -75,7 +75,7 @@ export function parse(str: string): number { ); } const match = - /^(?-?\d*\.?\d+) *(?milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|months?|mo|years?|yrs?|y)?$/i.exec( + /^(?-?(?:\d*\.?\d+)(?:e[+-]?\d+)?) *(?milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|months?|mo|years?|yrs?|y)?$/i.exec( str, );