Skip to content

Commit

Permalink
Simplify logic (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jan 18, 2024
1 parent 014edf2 commit fcb7872
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions index.js
Expand Up @@ -22,11 +22,12 @@ export default function prettyMilliseconds(milliseconds, options) {
}

if (options.compact) {
options.unitCount = 1;
options.secondsDecimalDigits = 0;
options.millisecondsDecimalDigits = 0;
}

const result = [];
let result = [];

const floorDecimals = (value, decimalDigits) => {
const flooredInterimValue = Math.floor((value * (10 ** decimalDigits)) + SECOND_ROUNDING_EPSILON);
Expand All @@ -42,21 +43,16 @@ export default function prettyMilliseconds(milliseconds, options) {
return;
}

valueString = (valueString || value || '0').toString();
let prefix;
let suffix;
valueString = valueString ?? String(value);
if (options.colonNotation) {
prefix = result.length > 0 ? ':' : '';
suffix = '';
const wholeDigits = valueString.includes('.') ? valueString.split('.')[0].length : valueString.length;
const minLength = result.length > 0 ? 2 : 1;
valueString = '0'.repeat(Math.max(0, minLength - wholeDigits)) + valueString;
} else {
prefix = '';
suffix = options.verbose ? ' ' + pluralize(long, value) : short;
valueString += options.verbose ? ' ' + pluralize(long, value) : short;
}

result.push(prefix + valueString + suffix);
result.push(valueString);
};

const parsed = parseMilliseconds(milliseconds);
Expand Down Expand Up @@ -129,14 +125,10 @@ export default function prettyMilliseconds(milliseconds, options) {
return '0' + (options.verbose ? ' milliseconds' : 'ms');
}

if (options.compact) {
return result[0];
}

const separator = options.colonNotation ? ':' : ' ';
if (typeof options.unitCount === 'number') {
const separator = options.colonNotation ? '' : ' ';
return result.slice(0, Math.max(options.unitCount, 1)).join(separator);
result = result.slice(0, Math.max(options.unitCount, 1));
}

return options.colonNotation ? result.join('') : result.join(' ');
return result.join(separator);
}

0 comments on commit fcb7872

Please sign in to comment.