Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export const getMinimalTimeUnitStringForMs = (value = 0, short = true, plural) => {
const ms = (isObjectLike(value) || isString(value))
? convertStringToNumber(value)
: value;
const parsedMs = omitBy(
pick(parseMilliseconds(Number(ms)), MinimalTimeUnitWhitelist),
isZero,
);
const {
unit: highestResolutionUnit,
value: highestResolutionValue,
} = getHighestResolutionUnit(parsedMs);
const label = buildLocalizedTimeUnitString({
plural,
short,
unit: highestResolutionUnit,
});
return `${highestResolutionValue} ${label}`;
};
export const getDuration = (level: Level, startTimeSpan: timeSpan.TimeEndFunction = lastCall) => {
const ms = startTimeSpan();
const { days, hours, minutes, seconds, milliseconds } = parseMS(ms);
const daysHoursMinutesAsMinutes = hours * 60 + days * 24 * 60 + minutes;
const duration =
daysHoursMinutesAsMinutes > 0
? `${daysHoursMinutesAsMinutes} min`
: `${seconds > 0 ? `${seconds}s` : ''}${
logConfig.isDebug
? `${milliseconds < 10 ? ' ' : ''}${milliseconds < 100 ? ' ' : ''}${Math.round(milliseconds)}ms`
: ''
}`;
lastCall = timeSpan();
const durationString = ms < (logConfig.isDebug ? 100 : 1000) ? '·' : duration;
const annotation = level === 'DEBUG' ? 'DEBUG' : '';
return chalk`{bgMagenta.white ${annotation}}{gray ${rightJustify(
durationString,
logConfig.indent - 1 - annotation.length
)} }`;
export default (
ms: number,
options: Options = { timeSeparator: ':', millisecondSepartor: '.' },
): string => {
const { days, hours, minutes, seconds, milliseconds } = parseMs(ms);
const fHours = (days * 24 + hours).toString().padStart(2, '0');
const fMinutes = minutes.toString().padStart(2, '0');
const fSeconds = seconds.toString().padStart(2, '0');
const fMilliseconds = milliseconds.toString().padStart(3, '0');
const formatted = `${fHours}${options.timeSeparator}${fMinutes}${
options.timeSeparator
}${fSeconds}${options.millisecondSepartor}${fMilliseconds}`;
return formatted;
};