diff --git a/src/index.ts b/src/index.ts index 668644f..1c8c3e7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,6 +4,7 @@ const m = s * 60; const h = m * 60; const d = h * 24; const w = d * 7; +const mo = d * 30; const y = d * 365.25; type Unit = @@ -12,6 +13,9 @@ type Unit = | 'Yrs' | 'Yr' | 'Y' + | 'Months' + | 'Month' + | 'Mo' | 'Weeks' | 'Week' | 'W' @@ -92,7 +96,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|years?|yrs?|y)?$/i.exec( + /^(?-?(?:\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, ); // Named capture groups need to be manually typed today. @@ -110,6 +114,10 @@ export function parse(str: string): number { case 'yr': case 'y': return n * y; + case 'months': + case 'month': + case 'mo': + return n * mo; case 'weeks': case 'week': case 'w': diff --git a/src/parse-strict.test.ts b/src/parse-strict.test.ts index 5707b87..1343b3b 100644 --- a/src/parse-strict.test.ts +++ b/src/parse-strict.test.ts @@ -115,6 +115,12 @@ describe('parseStrict(long string)', () => { expect(parseStrict('1 week')).toBe(604800000); }); + it('should convert months to ms', () => { + expect(parseStrict('1 month')).toBe(2592000000); + expect(parseStrict('3 months')).toBe(2592000000 * 3); + expect(parseStrict('2 mo')).toBe(2592000000 * 2); + }); + it('should convert years to ms', () => { expect(parseStrict('1 year')).toBe(31557600000); });