From 5d1b5bf39cc950182f466cb875f862ac6c66fc01 Mon Sep 17 00:00:00 2001 From: Henry Date: Thu, 3 Oct 2019 19:39:08 +0200 Subject: [PATCH 1/8] fix rounding inconsistency Signed-off-by: Henry --- index.js | 12 +++++++++++- test.js | 5 +++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index a6cb90a..e17c06e 100644 --- a/index.js +++ b/index.js @@ -37,6 +37,14 @@ module.exports = (milliseconds, options = {}) => { } } + if (milliseconds > 1000 && !options.separateMilliseconds && + !options.formatSubMilliseconds) { + const difference = 60 - milliseconds % 60 + if (difference < 20) { + milliseconds += difference; + } + } + const parsed = parseMilliseconds(milliseconds); add(Math.trunc(parsed.days / 365), 'year', 'y'); @@ -67,7 +75,9 @@ module.exports = (milliseconds, options = {}) => { const millisecondsString = millisecondsDecimalDigits ? millisecondsAndBelow.toFixed(millisecondsDecimalDigits) : - Math.ceil(millisecondsAndBelow); + millisecondsAndBelow >= 1 ? + Math.round(millisecondsAndBelow) : + Math.ceil(millisecondsAndBelow); add( parseFloat(millisecondsString, 10), diff --git a/test.js b/test.js index e569b5e..79a74d9 100644 --- a/test.js +++ b/test.js @@ -16,6 +16,7 @@ test('prettify milliseconds', t => { t.is(prettyMilliseconds(1000 * 60 * 60 * 999), '41d 15h'); t.is(prettyMilliseconds(1000 * 60 * 60 * 24 * 465), '1y 100d'); t.is(prettyMilliseconds(1000 * 60 * 67 * 24 * 465), '1y 154d 6h'); + t.is(prettyMilliseconds(119999), '2m'); }); test('have a compact option', t => { @@ -43,8 +44,8 @@ test('have a secondsDecimalDigits option', t => { }); test('have a millisecondsDecimalDigits option', t => { - t.is(prettyMilliseconds(33.333), '34ms'); - t.is(prettyMilliseconds(33.333, {millisecondsDecimalDigits: 0}), '34ms'); + t.is(prettyMilliseconds(33.333), '33ms'); + t.is(prettyMilliseconds(33.333, {millisecondsDecimalDigits: 0}), '33ms'); t.is(prettyMilliseconds(33.333, {millisecondsDecimalDigits: 4}), '33.3330ms'); }); From 995aded6959f4aae6099a99208abd7029363da48 Mon Sep 17 00:00:00 2001 From: Henry Date: Thu, 3 Oct 2019 19:46:39 +0200 Subject: [PATCH 2/8] fix rounding difference Signed-off-by: Henry --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index e17c06e..e3ae417 100644 --- a/index.js +++ b/index.js @@ -40,7 +40,7 @@ module.exports = (milliseconds, options = {}) => { if (milliseconds > 1000 && !options.separateMilliseconds && !options.formatSubMilliseconds) { const difference = 60 - milliseconds % 60 - if (difference < 20) { + if (difference < 50) { milliseconds += difference; } } From 6154e5a2a695cc2db20dbf57d2da2f30d8497694 Mon Sep 17 00:00:00 2001 From: Henry Date: Thu, 3 Oct 2019 19:54:50 +0200 Subject: [PATCH 3/8] fix rounding Signed-off-by: Henry --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index e3ae417..404833a 100644 --- a/index.js +++ b/index.js @@ -37,10 +37,10 @@ module.exports = (milliseconds, options = {}) => { } } - if (milliseconds > 1000 && !options.separateMilliseconds && + if (milliseconds >= (1000 * 60) - 50 && !options.separateMilliseconds && !options.formatSubMilliseconds) { const difference = 60 - milliseconds % 60 - if (difference < 50) { + if (difference <= 50) { milliseconds += difference; } } From 6adf3cdaf8f2ddf55dff8889313ef8a8338b66db Mon Sep 17 00:00:00 2001 From: Henry Date: Thu, 3 Oct 2019 23:37:15 +0200 Subject: [PATCH 4/8] fix formatting Signed-off-by: Henry --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 404833a..fd035cb 100644 --- a/index.js +++ b/index.js @@ -39,7 +39,7 @@ module.exports = (milliseconds, options = {}) => { if (milliseconds >= (1000 * 60) - 50 && !options.separateMilliseconds && !options.formatSubMilliseconds) { - const difference = 60 - milliseconds % 60 + const difference = 60 - (milliseconds % 60); if (difference <= 50) { milliseconds += difference; } From 6624cde60f1490d5c37a15affea1eada386e4595 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 13 Nov 2019 17:08:27 +0700 Subject: [PATCH 5/8] Update index.js --- index.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index fd035cb..d7f78ca 100644 --- a/index.js +++ b/index.js @@ -37,8 +37,11 @@ module.exports = (milliseconds, options = {}) => { } } - if (milliseconds >= (1000 * 60) - 50 && !options.separateMilliseconds && - !options.formatSubMilliseconds) { + if ( + milliseconds >= (1000 * 60) - 50 && + !options.separateMilliseconds && + !options.formatSubMilliseconds + ) { const difference = 60 - (milliseconds % 60); if (difference <= 50) { milliseconds += difference; From e32a5b64e1851f52ffba898ccb2ffe6db3af7b8e Mon Sep 17 00:00:00 2001 From: Henry Gressmann Date: Sun, 22 Dec 2019 17:00:21 +0100 Subject: [PATCH 6/8] added comment --- index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/index.js b/index.js index d7f78ca..235cc7f 100644 --- a/index.js +++ b/index.js @@ -37,6 +37,9 @@ module.exports = (milliseconds, options = {}) => { } } + // Round up milliseconds for values lager than 1 minute - 50ms since these + // always need to be round up. + // (this fixes issues when rounding seconds independently of minute later) if ( milliseconds >= (1000 * 60) - 50 && !options.separateMilliseconds && From 999bc4f71637ca606f9a69c6524e5f2f57292b71 Mon Sep 17 00:00:00 2001 From: Henry Gressmann Date: Sun, 22 Dec 2019 17:08:25 +0100 Subject: [PATCH 7/8] grammar Signed-off-by: Henry Gressmann --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 235cc7f..50d3a66 100644 --- a/index.js +++ b/index.js @@ -39,7 +39,7 @@ module.exports = (milliseconds, options = {}) => { // Round up milliseconds for values lager than 1 minute - 50ms since these // always need to be round up. - // (this fixes issues when rounding seconds independently of minute later) + // (this fixes issues when rounding seconds independently of minutes later) if ( milliseconds >= (1000 * 60) - 50 && !options.separateMilliseconds && From 7d38fb7103b8f393341cb8f2c650d1f56b572291 Mon Sep 17 00:00:00 2001 From: Henry Gressmann Date: Sun, 22 Dec 2019 17:13:08 +0100 Subject: [PATCH 8/8] fix linting issue Signed-off-by: Henry Gressmann --- index.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 50d3a66..416794d 100644 --- a/index.js +++ b/index.js @@ -79,11 +79,13 @@ module.exports = (milliseconds, options = {}) => { options.millisecondsDecimalDigits : 0; + const roundedMiliseconds = millisecondsAndBelow >= 1 ? + Math.round(millisecondsAndBelow) : + Math.ceil(millisecondsAndBelow); + const millisecondsString = millisecondsDecimalDigits ? millisecondsAndBelow.toFixed(millisecondsDecimalDigits) : - millisecondsAndBelow >= 1 ? - Math.round(millisecondsAndBelow) : - Math.ceil(millisecondsAndBelow); + roundedMiliseconds; add( parseFloat(millisecondsString, 10),