mirror of
https://github.com/swc-project/swc.git
synced 2024-12-18 11:11:30 +03:00
aacf7aaf80
**Description:** We only inline function declarations if `ref_count` is 1, so we don't need to preserve the name of the function. **Related issue:** - Closes https://github.com/swc-project/swc/issues/5682.
114 lines
3.0 KiB
JavaScript
114 lines
3.0 KiB
JavaScript
const s = 1000;
|
|
const m = s * 60;
|
|
const h = m * 60;
|
|
const d = h * 24;
|
|
const w = d * 7;
|
|
const y = d * 365.25;
|
|
function msFn(value, options) {
|
|
try {
|
|
if (typeof value === 'string' && value.length > 0) {
|
|
return parse(value);
|
|
} else if (typeof value === 'number' && isFinite(value)) {
|
|
return options?.long ? fmtLong(value) : fmtShort(value);
|
|
}
|
|
throw new Error('Value is not a string or number.');
|
|
} catch (error) {
|
|
const message = isError(error) ? `${error.message}. value=${JSON.stringify(value)}` : 'An unknown error has occurred.';
|
|
throw new Error(message);
|
|
}
|
|
}
|
|
function parse(str) {
|
|
if (str.length > 100) {
|
|
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(str);
|
|
const groups = match?.groups;
|
|
if (!groups) {
|
|
return NaN;
|
|
}
|
|
const n = parseFloat(groups.value);
|
|
const type = (groups.type || 'ms').toLowerCase();
|
|
switch (type) {
|
|
case 'years':
|
|
case 'year':
|
|
case 'yrs':
|
|
case 'yr':
|
|
case 'y':
|
|
return n * y;
|
|
case 'weeks':
|
|
case 'week':
|
|
case 'w':
|
|
return n * w;
|
|
case 'days':
|
|
case 'day':
|
|
case 'd':
|
|
return n * d;
|
|
case 'hours':
|
|
case 'hour':
|
|
case 'hrs':
|
|
case 'hr':
|
|
case 'h':
|
|
return n * h;
|
|
case 'minutes':
|
|
case 'minute':
|
|
case 'mins':
|
|
case 'min':
|
|
case 'm':
|
|
return n * m;
|
|
case 'seconds':
|
|
case 'second':
|
|
case 'secs':
|
|
case 'sec':
|
|
case 's':
|
|
return n * s;
|
|
case 'milliseconds':
|
|
case 'millisecond':
|
|
case 'msecs':
|
|
case 'msec':
|
|
case 'ms':
|
|
return n;
|
|
default:
|
|
throw new Error(`The unit ${type} was matched, but no matching case exists.`);
|
|
}
|
|
}
|
|
export default msFn;
|
|
function fmtShort(ms) {
|
|
const msAbs = Math.abs(ms);
|
|
if (msAbs >= d) {
|
|
return `${Math.round(ms / d)}d`;
|
|
}
|
|
if (msAbs >= h) {
|
|
return `${Math.round(ms / h)}h`;
|
|
}
|
|
if (msAbs >= m) {
|
|
return `${Math.round(ms / m)}m`;
|
|
}
|
|
if (msAbs >= s) {
|
|
return `${Math.round(ms / s)}s`;
|
|
}
|
|
return `${ms}ms`;
|
|
}
|
|
function fmtLong(ms) {
|
|
const msAbs = Math.abs(ms);
|
|
if (msAbs >= d) {
|
|
return plural(ms, msAbs, d, 'day');
|
|
}
|
|
if (msAbs >= h) {
|
|
return plural(ms, msAbs, h, 'hour');
|
|
}
|
|
if (msAbs >= m) {
|
|
return plural(ms, msAbs, m, 'minute');
|
|
}
|
|
if (msAbs >= s) {
|
|
return plural(ms, msAbs, s, 'second');
|
|
}
|
|
return `${ms} ms`;
|
|
}
|
|
function plural(ms, msAbs, n, name) {
|
|
const isPlural = msAbs >= n * 1.5;
|
|
return `${Math.round(ms / n)} ${name}${isPlural ? 's' : ''}`;
|
|
}
|
|
function isError(value) {
|
|
return typeof value === 'object' && value !== null && 'message' in value;
|
|
}
|