Skip to content

Commit

Permalink
Add support for months
Browse files Browse the repository at this point in the history
Upstream issue: vercel#57
  • Loading branch information
EnriqCG committed Mar 26, 2024
1 parent 8f9d335 commit a15c54f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -12,6 +13,9 @@ type Unit =
| 'Yrs'
| 'Yr'
| 'Y'
| 'Months'
| 'Month'
| 'Mo'
| 'Weeks'
| 'Week'
| 'W'
Expand Down Expand Up @@ -92,7 +96,7 @@ export function parse(str: string): number {
);
}
const match =
/^(?<value>-?(?:\d+)?\.?\d+) *(?<type>milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)?$/i.exec(
/^(?<value>-?(?:\d+)?\.?\d+) *(?<type>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.
Expand All @@ -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':
Expand Down
6 changes: 6 additions & 0 deletions src/parse-strict.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down

0 comments on commit a15c54f

Please sign in to comment.