Skip to content

Commit

Permalink
Simplify logic
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jan 18, 2024
1 parent 228dd07 commit f025786
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function prettyMilliseconds(milliseconds, options = {}) {
options.millisecondsDecimalDigits = 0;
}

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

const floorDecimals = (value, decimalDigits) => {
const flooredInterimValue = Math.floor((value * (10 ** decimalDigits)) + SECOND_ROUNDING_EPSILON);
Expand All @@ -38,21 +38,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 @@ -119,10 +114,10 @@ export default function prettyMilliseconds(milliseconds, options = {}) {
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))
}

Check failure on line 121 in index.js

View workflow job for this annotation

GitHub Actions / Node.js 20

Missing semicolon.

Check failure on line 121 in index.js

View workflow job for this annotation

GitHub Actions / Node.js 18

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

0 comments on commit f025786

Please sign in to comment.