From f025786d49905ba796b520c92750f0540b90ca73 Mon Sep 17 00:00:00 2001 From: fisker Date: Thu, 18 Jan 2024 18:02:06 +0800 Subject: [PATCH 1/3] Simplify logic --- index.js | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index c0f433e..6b56c45 100644 --- a/index.js +++ b/index.js @@ -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); @@ -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); @@ -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)) } - return options.colonNotation ? result.join('') : result.join(' '); + return result.join(separator); } From e6af4c5be78c90f82617e75c36d7c434f779e6d1 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 18 Jan 2024 18:08:35 +0800 Subject: [PATCH 2/3] Update index.js --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 6b56c45..ceb3518 100644 --- a/index.js +++ b/index.js @@ -116,7 +116,7 @@ export default function prettyMilliseconds(milliseconds, options = {}) { const separator = options.colonNotation ? ':' : ' '; if (typeof options.unitCount === 'number') { - result = result.slice(0, Math.max(options.unitCount, 1)) + result = result.slice(0, Math.max(options.unitCount, 1)); } return result.join(separator); From d52ab5d9d3c061930e7384115f28a9e214557919 Mon Sep 17 00:00:00 2001 From: fisker Date: Thu, 18 Jan 2024 18:10:52 +0800 Subject: [PATCH 3/3] Simplify --- index.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/index.js b/index.js index ceb3518..fb7182d 100644 --- a/index.js +++ b/index.js @@ -18,6 +18,7 @@ export default function prettyMilliseconds(milliseconds, options = {}) { } if (options.compact) { + options.unitCount = 1; options.secondsDecimalDigits = 0; options.millisecondsDecimalDigits = 0; } @@ -110,10 +111,6 @@ 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') { result = result.slice(0, Math.max(options.unitCount, 1));