2023-02-06 09:42:34 +00:00
|
|
|
import is from '@sindresorhus/is';
|
|
|
|
import ms from 'ms';
|
|
|
|
import { logger } from '../logger';
|
|
|
|
import { regEx } from './regex';
|
|
|
|
|
|
|
|
const splitRegex = regEx(/(.*?[a-z]+)/);
|
|
|
|
|
|
|
|
function split(time: string): string[] {
|
|
|
|
return time
|
|
|
|
.split(splitRegex)
|
|
|
|
.map((x) => x.trim())
|
|
|
|
.filter(is.nonEmptyString);
|
|
|
|
}
|
|
|
|
|
2023-04-06 07:17:57 +00:00
|
|
|
function applyCustomFormat(spec: string): string {
|
|
|
|
const monthRegex = regEx(/^(\d+)\s*(?:months?|M)$/);
|
|
|
|
return spec.replace(monthRegex, (_, months) => `${months * 30} days`);
|
|
|
|
}
|
|
|
|
|
2023-02-06 09:42:34 +00:00
|
|
|
export function toMs(time: string): number | null {
|
|
|
|
try {
|
2023-04-06 07:17:57 +00:00
|
|
|
const specs = split(time).map(applyCustomFormat);
|
2023-02-06 09:42:34 +00:00
|
|
|
if (!specs.length) {
|
|
|
|
logger.debug({ time }, `Invalid time specifier: '${time}'`);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
let totalMillis = 0;
|
|
|
|
for (const spec of specs) {
|
|
|
|
const millis = ms(spec);
|
|
|
|
if (!is.number(millis)) {
|
|
|
|
logger.debug({ time }, `Invalid time specifier: '${spec}'`);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
totalMillis += millis;
|
|
|
|
}
|
|
|
|
return totalMillis;
|
|
|
|
} catch (err) {
|
|
|
|
logger.debug({ time, err }, `Invalid time specifier: '${time}'`);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|