Skip to content
Snippets Groups Projects
Unverified Commit db8a377b authored by Sergei Zharinov's avatar Sergei Zharinov Committed by GitHub
Browse files

feat(util/pretty-time): Ensure support for `years`, `months` and `weeks` (#21366)

parent cec928d7
Branches
Tags
No related merge requests found
......@@ -16,6 +16,14 @@ describe('util/pretty-time', () => {
${'1hour 1 min 1s'} | ${1 * 60 * 60 * 1000 + 1 * 60 * 1000 + 1000}
${'1h 1m 1s 1ms'} | ${1 * 60 * 60 * 1000 + 1 * 60 * 1000 + 1000 + 1}
${'3 days'} | ${3 * 24 * 60 * 60 * 1000}
${'1 week'} | ${7 * 24 * 60 * 60 * 1000}
${'1 month'} | ${30 * 24 * 60 * 60 * 1000}
${'1 M'} | ${30 * 24 * 60 * 60 * 1000}
${'2 months'} | ${2 * 30 * 24 * 60 * 60 * 1000}
${'1month'} | ${30 * 24 * 60 * 60 * 1000}
${'1M'} | ${30 * 24 * 60 * 60 * 1000}
${'2months'} | ${2 * 30 * 24 * 60 * 60 * 1000}
${'1 year'} | ${365.25 * 24 * 60 * 60 * 1000}
${'0'.repeat(100)} | ${0}
${'0'.repeat(101)} | ${null}
${'1 whatever'} | ${null}
......
......@@ -7,15 +7,19 @@ const splitRegex = regEx(/(.*?[a-z]+)/);
function split(time: string): string[] {
return time
.toLocaleLowerCase()
.split(splitRegex)
.map((x) => x.trim())
.filter(is.nonEmptyString);
}
function applyCustomFormat(spec: string): string {
const monthRegex = regEx(/^(\d+)\s*(?:months?|M)$/);
return spec.replace(monthRegex, (_, months) => `${months * 30} days`);
}
export function toMs(time: string): number | null {
try {
const specs = split(time);
const specs = split(time).map(applyCustomFormat);
if (!specs.length) {
logger.debug({ time }, `Invalid time specifier: '${time}'`);
return null;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment