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/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, ); 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