Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added month support #210

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.4375;
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 @@ -90,7 +94,7 @@ function parse(str: string): number {
throw new Error('Value exceeds the maximum length of 100 characters.');
}
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 @@ -108,6 +112,10 @@ 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