Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify logic #68

Merged
merged 3 commits into from
Jan 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 8 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,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 @@ -38,21 +39,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 @@ -115,14 +111,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);
}