diff --git a/src/index.ts b/src/index.ts index d50e3c7..2e7baa7 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*|\.\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..3b6febf 100644 --- a/src/parse.test.ts +++ b/src/parse.test.ts @@ -54,6 +54,12 @@ describe('parse(string)', () => { expect(Number.isNaN(parse('foo'))).toBe(true); }); + it('should parse long invalid numeric strings without catastrophic backtracking', () => { + const start = performance.now(); + expect(Number.isNaN(parse(`${'9'.repeat(99)}z`))).toBe(true); + expect(performance.now() - start).toBeLessThan(20); + }); + it('should be case-insensitive', () => { expect(parse('53 YeArS')).toBe(1672552800000); expect(parse('53 WeEkS')).toBe(32054400000);