From 6012407dfdb6e3f2991f7d8cb128fa7e6c66a445 Mon Sep 17 00:00:00 2001 From: Michael Gerber Date: Wed, 16 Oct 2024 12:20:16 +0200 Subject: [PATCH 01/10] Enhance time parsing with new hide options Added options `hideYears`, `hideYearsAndDays`, and `hideSeconds` to allow for more customizable time formatting. --- index.d.ts | 21 +++++++++ index.js | 126 +++++++++++++++++++++++++++++------------------------ test.js | 40 +++++++++++++++++ 3 files changed, 130 insertions(+), 57 deletions(-) diff --git a/index.d.ts b/index.d.ts index 01fd9ba..52c23b3 100644 --- a/index.d.ts +++ b/index.d.ts @@ -75,6 +75,27 @@ export type Options = { @default false */ readonly colonNotation?: boolean; + + /** + Hides the years and shows the hidden years additionally as days (365 per year).: `1y 3d 5h 1m 45s` → `368d 5h 1m 45s`. + + @default false + */ + readonly hideYears?: boolean; + + /** + Hides the years and days and shows the hidden values additionally as hours.: `1y 3d 5h 1m 45s` → `8837h 1m 45s`. + + @default false + */ + readonly hideYearsAndDays?: boolean; + + /** + Hides the seconds: `1y 3d 5h 1m 45s` → `1y 3d 5h 1m`. + + @default false + */ + readonly hideSeconds?: boolean; }; /** diff --git a/index.js b/index.js index 00a3d0d..373e790 100644 --- a/index.js +++ b/index.js @@ -61,67 +61,79 @@ export default function prettyMilliseconds(milliseconds, options) { const parsed = parseMilliseconds(milliseconds); const days = BigInt(parsed.days); - add(days / 365n, 'year', 'y'); - add(days % 365n, 'day', 'd'); - add(Number(parsed.hours), 'hour', 'h'); + if (options.hideYearsAndDays) { + add((Number(days) * 24) + Number(parsed.hours), 'hour', 'h'); + } else { + if (options.hideYears) { + add(days, 'day', 'd'); + } else { + add(days / 365n, 'year', 'y'); + add(days % 365n, 'day', 'd'); + } + + add(Number(parsed.hours), 'hour', 'h'); + } + add(Number(parsed.minutes), 'minute', 'm'); - if ( - options.separateMilliseconds - || options.formatSubMilliseconds - || (!options.colonNotation && milliseconds < 1000) - ) { - const seconds = Number(parsed.seconds); - const milliseconds = Number(parsed.milliseconds); - const microseconds = Number(parsed.microseconds); - const nanoseconds = Number(parsed.nanoseconds); - - add(seconds, 'second', 's'); - - if (options.formatSubMilliseconds) { - add(milliseconds, 'millisecond', 'ms'); - add(microseconds, 'microsecond', 'µs'); - add(nanoseconds, 'nanosecond', 'ns'); + if (!options.hideSeconds) { + if ( + options.separateMilliseconds + || options.formatSubMilliseconds + || (!options.colonNotation && milliseconds < 1000) + ) { + const seconds = Number(parsed.seconds); + const milliseconds = Number(parsed.milliseconds); + const microseconds = Number(parsed.microseconds); + const nanoseconds = Number(parsed.nanoseconds); + + add(seconds, 'second', 's'); + + if (options.formatSubMilliseconds) { + add(milliseconds, 'millisecond', 'ms'); + add(microseconds, 'microsecond', 'µs'); + add(nanoseconds, 'nanosecond', 'ns'); + } else { + const millisecondsAndBelow + = milliseconds + + (microseconds / 1000) + + (nanoseconds / 1e6); + + const millisecondsDecimalDigits + = typeof options.millisecondsDecimalDigits === 'number' + ? options.millisecondsDecimalDigits + : 0; + + const roundedMilliseconds = millisecondsAndBelow >= 1 + ? Math.round(millisecondsAndBelow) + : Math.ceil(millisecondsAndBelow); + + const millisecondsString = millisecondsDecimalDigits + ? millisecondsAndBelow.toFixed(millisecondsDecimalDigits) + : roundedMilliseconds; + + add( + Number.parseFloat(millisecondsString), + 'millisecond', + 'ms', + millisecondsString, + ); + } } else { - const millisecondsAndBelow - = milliseconds - + (microseconds / 1000) - + (nanoseconds / 1e6); - - const millisecondsDecimalDigits - = typeof options.millisecondsDecimalDigits === 'number' - ? options.millisecondsDecimalDigits - : 0; - - const roundedMilliseconds = millisecondsAndBelow >= 1 - ? Math.round(millisecondsAndBelow) - : Math.ceil(millisecondsAndBelow); - - const millisecondsString = millisecondsDecimalDigits - ? millisecondsAndBelow.toFixed(millisecondsDecimalDigits) - : roundedMilliseconds; - - add( - Number.parseFloat(millisecondsString), - 'millisecond', - 'ms', - millisecondsString, - ); + const seconds = ( + (isBigInt ? Number(milliseconds % ONE_DAY_IN_MILLISECONDS) : milliseconds) + / 1000 + ) % 60; + const secondsDecimalDigits + = typeof options.secondsDecimalDigits === 'number' + ? options.secondsDecimalDigits + : 1; + const secondsFixed = floorDecimals(seconds, secondsDecimalDigits); + const secondsString = options.keepDecimalsOnWholeSeconds + ? secondsFixed + : secondsFixed.replace(/\.0+$/, ''); + add(Number.parseFloat(secondsString), 'second', 's', secondsString); } - } else { - const seconds = ( - (isBigInt ? Number(milliseconds % ONE_DAY_IN_MILLISECONDS) : milliseconds) - / 1000 - ) % 60; - const secondsDecimalDigits - = typeof options.secondsDecimalDigits === 'number' - ? options.secondsDecimalDigits - : 1; - const secondsFixed = floorDecimals(seconds, secondsDecimalDigits); - const secondsString = options.keepDecimalsOnWholeSeconds - ? secondsFixed - : secondsFixed.replace(/\.0+$/, ''); - add(Number.parseFloat(secondsString), 'second', 's', secondsString); } if (result.length === 0) { diff --git a/test.js b/test.js index 7be3b6c..d7af14c 100644 --- a/test.js +++ b/test.js @@ -379,6 +379,46 @@ runTests({ ], }); +runTests({ + title: 'have a hideYears option', + cases: [ + [1000 * 60, {hideYears: true}, '1m'], + [1000 * 60, {hideYears: false}, '1m'], + [1000 * 60 * 67, {hideYears: true}, '1h 7m'], + [1000 * 60 * 67, {hideYears: false}, '1h 7m'], + [1000 * 60 * 67 * 24 * 465, {hideYears: false}, '1y 154d 6h'], + [1000 * 60 * 67 * 24 * 465, {hideYears: true}, '519d 6h'], + ], +}); + +runTests({ + title: 'have a hideYearsAndDays option', + cases: [ + [1000 * 60, {hideYearsAndDays: true}, '1m'], + [1000 * 60, {hideYearsAndDays: false}, '1m'], + [1000 * 60 * 67, {hideYearsAndDays: false}, '1h 7m'], + [1000 * 60 * 67, {hideYearsAndDays: true}, '1h 7m'], + [1000 * 60 * 67 * 24 * 465, {hideYearsAndDays: false}, '1y 154d 6h'], + [1000 * 60 * 67 * 24 * 465, {hideYearsAndDays: true}, '12462h'], + ], +}); + +runTests({ + title: 'have a hideSeconds option', + cases: [ + [(1000 * 60) + 6500, {hideSeconds: false}, '1m 6.5s'], + [(1000 * 60) + 6500, {hideSeconds: true}, '1m'], + ], +}); + +runTests({ + title: 'have hideYearsAndDays,hideSeconds and colonNotation options', + cases: [ + [(1000 * 60 * 60 * 15) + (1000 * 60 * 59) + (1000 * 59) + 543, {hideSeconds: true, hideYearsAndDays: true, colonNotation: true}, '15:59'], + [(1000 * 60 * 67 * 24 * 465) + (1000 * 60 * 60 * 15) + (1000 * 60 * 59) + (1000 * 59) + 543, {hideSeconds: true, hideYearsAndDays: true, colonNotation: true}, '12477:59'], + ], +}); + test('Big numbers', t => { t.is( prettyMilliseconds(Number.MAX_VALUE), From ab793ad9704f02c23f05bdb0f3ddde9cb0bb0bfa Mon Sep 17 00:00:00 2001 From: Michael Gerber Date: Tue, 29 Oct 2024 10:40:30 +0100 Subject: [PATCH 02/10] Rename hideYears and hideYearsAndDays options Updated options "hideYears" to "hideYear" and "hideYearsAndDays" to "hideYearAndDays" for consistency. Also updated relevant code and tests to use the new option names. --- index.d.ts | 8 ++++---- index.js | 4 ++-- test.js | 34 +++++++++++++++++----------------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/index.d.ts b/index.d.ts index 52c23b3..4ed9786 100644 --- a/index.d.ts +++ b/index.d.ts @@ -77,18 +77,18 @@ export type Options = { readonly colonNotation?: boolean; /** - Hides the years and shows the hidden years additionally as days (365 per year).: `1y 3d 5h 1m 45s` → `368d 5h 1m 45s`. + Hides the year and shows the hidden year additionally as days (365 per year).: `1y 3d 5h 1m 45s` → `368d 5h 1m 45s`. @default false */ - readonly hideYears?: boolean; + readonly hideYear?: boolean; /** - Hides the years and days and shows the hidden values additionally as hours.: `1y 3d 5h 1m 45s` → `8837h 1m 45s`. + Hides the year and days and shows the hidden values additionally as hours.: `1y 3d 5h 1m 45s` → `8837h 1m 45s`. @default false */ - readonly hideYearsAndDays?: boolean; + readonly hideYearAndDays?: boolean; /** Hides the seconds: `1y 3d 5h 1m 45s` → `1y 3d 5h 1m`. diff --git a/index.js b/index.js index 373e790..fdeb407 100644 --- a/index.js +++ b/index.js @@ -61,10 +61,10 @@ export default function prettyMilliseconds(milliseconds, options) { const parsed = parseMilliseconds(milliseconds); const days = BigInt(parsed.days); - if (options.hideYearsAndDays) { + if (options.hideYearAndDays) { add((Number(days) * 24) + Number(parsed.hours), 'hour', 'h'); } else { - if (options.hideYears) { + if (options.hideYear) { add(days, 'day', 'd'); } else { add(days / 365n, 'year', 'y'); diff --git a/test.js b/test.js index d7af14c..2a2fea7 100644 --- a/test.js +++ b/test.js @@ -380,26 +380,26 @@ runTests({ }); runTests({ - title: 'have a hideYears option', + title: 'have a hideYear option', cases: [ - [1000 * 60, {hideYears: true}, '1m'], - [1000 * 60, {hideYears: false}, '1m'], - [1000 * 60 * 67, {hideYears: true}, '1h 7m'], - [1000 * 60 * 67, {hideYears: false}, '1h 7m'], - [1000 * 60 * 67 * 24 * 465, {hideYears: false}, '1y 154d 6h'], - [1000 * 60 * 67 * 24 * 465, {hideYears: true}, '519d 6h'], + [1000 * 60, {hideYear: true}, '1m'], + [1000 * 60, {hideYear: false}, '1m'], + [1000 * 60 * 67, {hideYear: true}, '1h 7m'], + [1000 * 60 * 67, {hideYear: false}, '1h 7m'], + [1000 * 60 * 67 * 24 * 465, {hideYear: false}, '1y 154d 6h'], + [1000 * 60 * 67 * 24 * 465, {hideYear: true}, '519d 6h'], ], }); runTests({ - title: 'have a hideYearsAndDays option', + title: 'have a hideYearAndDays option', cases: [ - [1000 * 60, {hideYearsAndDays: true}, '1m'], - [1000 * 60, {hideYearsAndDays: false}, '1m'], - [1000 * 60 * 67, {hideYearsAndDays: false}, '1h 7m'], - [1000 * 60 * 67, {hideYearsAndDays: true}, '1h 7m'], - [1000 * 60 * 67 * 24 * 465, {hideYearsAndDays: false}, '1y 154d 6h'], - [1000 * 60 * 67 * 24 * 465, {hideYearsAndDays: true}, '12462h'], + [1000 * 60, {hideYearAndDays: true}, '1m'], + [1000 * 60, {hideYearAndDays: false}, '1m'], + [1000 * 60 * 67, {hideYearAndDays: false}, '1h 7m'], + [1000 * 60 * 67, {hideYearAndDays: true}, '1h 7m'], + [1000 * 60 * 67 * 24 * 465, {hideYearAndDays: false}, '1y 154d 6h'], + [1000 * 60 * 67 * 24 * 465, {hideYearAndDays: true}, '12462h'], ], }); @@ -412,10 +412,10 @@ runTests({ }); runTests({ - title: 'have hideYearsAndDays,hideSeconds and colonNotation options', + title: 'have hideYearAndDays,hideSeconds and colonNotation options', cases: [ - [(1000 * 60 * 60 * 15) + (1000 * 60 * 59) + (1000 * 59) + 543, {hideSeconds: true, hideYearsAndDays: true, colonNotation: true}, '15:59'], - [(1000 * 60 * 67 * 24 * 465) + (1000 * 60 * 60 * 15) + (1000 * 60 * 59) + (1000 * 59) + 543, {hideSeconds: true, hideYearsAndDays: true, colonNotation: true}, '12477:59'], + [(1000 * 60 * 60 * 15) + (1000 * 60 * 59) + (1000 * 59) + 543, {hideSeconds: true, hideYearAndDays: true, colonNotation: true}, '15:59'], + [(1000 * 60 * 67 * 24 * 465) + (1000 * 60 * 60 * 15) + (1000 * 60 * 59) + (1000 * 59) + 543, {hideSeconds: true, hideYearAndDays: true, colonNotation: true}, '12477:59'], ], }); From 0a7f8c1f17a4d8339797fff675500ee0fe16281a Mon Sep 17 00:00:00 2001 From: Michael Gerber Date: Tue, 29 Oct 2024 10:40:55 +0100 Subject: [PATCH 03/10] Add new time formatting options to the readme.md --- readme.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/readme.md b/readme.md index 389c515..ccc826e 100644 --- a/readme.md +++ b/readme.md @@ -137,6 +137,28 @@ Setting `colonNotation` to `true` overrides the following options to `false`: - `separateMilliseconds` - `verbose` +##### hideYear + +Type: `boolean`\ +Default: `false` + +Hides the year and shows the hidden year additionally as days (365 per year).: `1y 3d 5h 1m 45s` → `368d 5h 1m 45s`. + +##### hideYearAndDays + +Type: `boolean`\ +Default: `false` + +Hides the year and days and shows the hidden values additionally as hours.: `1y 3d 5h 1m 45s` → `8837h 1m 45s`. + +##### hideSeconds + +Type: `boolean`\ +Default: `false` + +Hides the seconds: `1y 3d 5h 1m 45s` → `1y 3d 5h 1m`. + + ## Related - [pretty-ms-cli](https://github.com/sindresorhus/pretty-ms-cli) - CLI for this module From f4f8143fe8a704e12476d1d079239371a7eea825 Mon Sep 17 00:00:00 2001 From: Michael Gerber Date: Tue, 29 Oct 2024 11:03:33 +0100 Subject: [PATCH 04/10] Refactor to use BigInt for hours calculation Changed the hours calculation to use BigInt to handle large integers accurately. This ensures that the computation remains precise even when dealing with large numbers, enhancing the robustness of the code. --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index fdeb407..33455d1 100644 --- a/index.js +++ b/index.js @@ -62,7 +62,7 @@ export default function prettyMilliseconds(milliseconds, options) { const days = BigInt(parsed.days); if (options.hideYearAndDays) { - add((Number(days) * 24) + Number(parsed.hours), 'hour', 'h'); + add((BigInt(days) * BigInt(24)) + BigInt(parsed.hours), 'hour', 'h'); } else { if (options.hideYear) { add(days, 'day', 'd'); From e319fbf05febc4da742147257ef1eb28b5727b59 Mon Sep 17 00:00:00 2001 From: Michael Gerber Date: Tue, 29 Oct 2024 11:03:42 +0100 Subject: [PATCH 05/10] Add new test cases for enhanced time formatting options Included various configurations to test detailed time formatting including decimal seconds, sub-millisecond precision, and verbose modes. This ensures comprehensive coverage across different time display preferences. --- test.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test.js b/test.js index 2a2fea7..fa6469d 100644 --- a/test.js +++ b/test.js @@ -388,6 +388,13 @@ runTests({ [1000 * 60 * 67, {hideYear: false}, '1h 7m'], [1000 * 60 * 67 * 24 * 465, {hideYear: false}, '1y 154d 6h'], [1000 * 60 * 67 * 24 * 465, {hideYear: true}, '519d 6h'], + [1000 * 60 * 67 * 24 * 465 + (1000 * 60) + 6500, {hideYear: false}, '1y 154d 6h 1m 6.5s'], + [1000 * 60 * 67 * 24 * 465 + (1000 * 60) + 6500, {hideYear: true}, '519d 6h 1m 6.5s'], + [1000 * 60 * 67 * 24 * 465 + (1000 * 60) + 6500, {hideYear: true, secondsDecimalDigits: 0}, '519d 6h 1m 6s'], + [1000 * 60 * 67 * 24 * 465 + (1000 * 60) + 6000, {hideYear: true, keepDecimalsOnWholeSeconds: true}, '519d 6h 1m 6.0s'], + [1000 * 60 * 67 * 24 * 465 + (1000 * 60) + 6500, {hideYear: true, separateMilliseconds: true}, '519d 6h 1m 6s 500ms'], + [1000 * 60 * 67 * 24 * 465 + (1000 * 60) + 6500, {hideYear: true, verbose: true}, '519 days 6 hours 1 minute 6.5 seconds'], + [1000 * 60 * 67 * 24 * 465 + (1000 * 60) + 6500, {hideYear: true, compact: true}, '519d'], ], }); @@ -400,6 +407,13 @@ runTests({ [1000 * 60 * 67, {hideYearAndDays: true}, '1h 7m'], [1000 * 60 * 67 * 24 * 465, {hideYearAndDays: false}, '1y 154d 6h'], [1000 * 60 * 67 * 24 * 465, {hideYearAndDays: true}, '12462h'], + [1000 * 60 * 67 * 24 * 465 + (1000 * 60) + 6500, {hideYearAndDays: false}, '1y 154d 6h 1m 6.5s'], + [1000 * 60 * 67 * 24 * 465 + (1000 * 60) + 6500, {hideYearAndDays: true}, '12462h 1m 6.5s'], + [1000 * 60 * 67 * 24 * 465 + (1000 * 60) + 6500, {hideYearAndDays: true, secondsDecimalDigits: 0}, '12462h 1m 6s'], + [1000 * 60 * 67 * 24 * 465 + (1000 * 60) + 6000, {hideYearAndDays: true, keepDecimalsOnWholeSeconds: true}, '12462h 1m 6.0s'], + [1000 * 60 * 67 * 24 * 465 + (1000 * 60) + 6500, {hideYearAndDays: true, separateMilliseconds: true}, '12462h 1m 6s 500ms'], + [1000 * 60 * 67 * 24 * 465 + (1000 * 60) + 6500, {hideYearAndDays: true, verbose: true}, '12462 hours 1 minute 6.5 seconds'], + [1000 * 60 * 67 * 24 * 465 + (1000 * 60) + 6500, {hideYearAndDays: true, compact: true}, '12462h'], ], }); @@ -408,6 +422,12 @@ runTests({ cases: [ [(1000 * 60) + 6500, {hideSeconds: false}, '1m 6.5s'], [(1000 * 60) + 6500, {hideSeconds: true}, '1m'], + [(1000 * 60) + 6500, {hideSeconds: true, secondsDecimalDigits: 3}, '1m'], + [(1000 * 60) + 6500, {hideSeconds: true, keepDecimalsOnWholeSeconds: true}, '1m'], + [(1000 * 60) + 6500, {hideSeconds: true, formatSubMilliseconds: true}, '1m'], + [(1000 * 60) + 6500, {hideSeconds: true, separateMilliseconds: true}, '1m'], + [(1000 * 60) + 6500, {hideSeconds: true, verbose: true}, '1 minute'], + [(1000 * 60) + 6500, {hideSeconds: true, compact: true}, '1m'], ], }); @@ -416,6 +436,7 @@ runTests({ cases: [ [(1000 * 60 * 60 * 15) + (1000 * 60 * 59) + (1000 * 59) + 543, {hideSeconds: true, hideYearAndDays: true, colonNotation: true}, '15:59'], [(1000 * 60 * 67 * 24 * 465) + (1000 * 60 * 60 * 15) + (1000 * 60 * 59) + (1000 * 59) + 543, {hideSeconds: true, hideYearAndDays: true, colonNotation: true}, '12477:59'], + [BigInt(Number.MAX_VALUE), {hideSeconds: true, hideYearAndDays: true, colonNotation: true}, '49935920412842103004035395481028987999464046534956943499699299111988127994452371877941544064657466158761238598198439573398422590802628939657907651862093754718347197382375356132290413913997035817798852363459759428417939788028673041157169044258923152298554951723373534213538382550255361078125112229495590:14'], ], }); From 96f7c17b4e72f2832e7b4401ed34b91f62dc871c Mon Sep 17 00:00:00 2001 From: Michael Gerber Date: Tue, 29 Oct 2024 11:06:12 +0100 Subject: [PATCH 06/10] Fix formatting in TypeScript definition comments Removed extra spaces in the comment blocks and aligned the `@default` lines correctly. This change improves code readability and consistency in the index.d.ts file. --- index.d.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/index.d.ts b/index.d.ts index 4ed9786..318f98c 100644 --- a/index.d.ts +++ b/index.d.ts @@ -77,24 +77,24 @@ export type Options = { readonly colonNotation?: boolean; /** - Hides the year and shows the hidden year additionally as days (365 per year).: `1y 3d 5h 1m 45s` → `368d 5h 1m 45s`. + Hides the year and shows the hidden year additionally as days (365 per year).: `1y 3d 5h 1m 45s` → `368d 5h 1m 45s`. - @default false - */ + @default false + */ readonly hideYear?: boolean; /** - Hides the year and days and shows the hidden values additionally as hours.: `1y 3d 5h 1m 45s` → `8837h 1m 45s`. + Hides the year and days and shows the hidden values additionally as hours.: `1y 3d 5h 1m 45s` → `8837h 1m 45s`. - @default false - */ + @default false + */ readonly hideYearAndDays?: boolean; /** - Hides the seconds: `1y 3d 5h 1m 45s` → `1y 3d 5h 1m`. + Hides the seconds: `1y 3d 5h 1m 45s` → `1y 3d 5h 1m`. - @default false - */ + @default false + */ readonly hideSeconds?: boolean; }; From 66f67ef73e8606bf5115563e01cc9178c1ce5387 Mon Sep 17 00:00:00 2001 From: Michael Gerber Date: Tue, 29 Oct 2024 11:09:17 +0100 Subject: [PATCH 07/10] Reformat test cases in test.js --- test.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/test.js b/test.js index fa6469d..f3553e0 100644 --- a/test.js +++ b/test.js @@ -388,13 +388,13 @@ runTests({ [1000 * 60 * 67, {hideYear: false}, '1h 7m'], [1000 * 60 * 67 * 24 * 465, {hideYear: false}, '1y 154d 6h'], [1000 * 60 * 67 * 24 * 465, {hideYear: true}, '519d 6h'], - [1000 * 60 * 67 * 24 * 465 + (1000 * 60) + 6500, {hideYear: false}, '1y 154d 6h 1m 6.5s'], - [1000 * 60 * 67 * 24 * 465 + (1000 * 60) + 6500, {hideYear: true}, '519d 6h 1m 6.5s'], - [1000 * 60 * 67 * 24 * 465 + (1000 * 60) + 6500, {hideYear: true, secondsDecimalDigits: 0}, '519d 6h 1m 6s'], - [1000 * 60 * 67 * 24 * 465 + (1000 * 60) + 6000, {hideYear: true, keepDecimalsOnWholeSeconds: true}, '519d 6h 1m 6.0s'], - [1000 * 60 * 67 * 24 * 465 + (1000 * 60) + 6500, {hideYear: true, separateMilliseconds: true}, '519d 6h 1m 6s 500ms'], - [1000 * 60 * 67 * 24 * 465 + (1000 * 60) + 6500, {hideYear: true, verbose: true}, '519 days 6 hours 1 minute 6.5 seconds'], - [1000 * 60 * 67 * 24 * 465 + (1000 * 60) + 6500, {hideYear: true, compact: true}, '519d'], + [(1000 * 60 * 67 * 24 * 465) + (1000 * 60) + 6500, {hideYear: false}, '1y 154d 6h 1m 6.5s'], + [(1000 * 60 * 67 * 24 * 465) + (1000 * 60) + 6500, {hideYear: true}, '519d 6h 1m 6.5s'], + [(1000 * 60 * 67 * 24 * 465) + (1000 * 60) + 6500, {hideYear: true, secondsDecimalDigits: 0}, '519d 6h 1m 6s'], + [(1000 * 60 * 67 * 24 * 465) + (1000 * 60) + 6000, {hideYear: true, keepDecimalsOnWholeSeconds: true}, '519d 6h 1m 6.0s'], + [(1000 * 60 * 67 * 24 * 465) + (1000 * 60) + 6500, {hideYear: true, separateMilliseconds: true}, '519d 6h 1m 6s 500ms'], + [(1000 * 60 * 67 * 24 * 465) + (1000 * 60) + 6500, {hideYear: true, verbose: true}, '519 days 6 hours 1 minute 6.5 seconds'], + [(1000 * 60 * 67 * 24 * 465) + (1000 * 60) + 6500, {hideYear: true, compact: true}, '519d'], ], }); @@ -407,13 +407,13 @@ runTests({ [1000 * 60 * 67, {hideYearAndDays: true}, '1h 7m'], [1000 * 60 * 67 * 24 * 465, {hideYearAndDays: false}, '1y 154d 6h'], [1000 * 60 * 67 * 24 * 465, {hideYearAndDays: true}, '12462h'], - [1000 * 60 * 67 * 24 * 465 + (1000 * 60) + 6500, {hideYearAndDays: false}, '1y 154d 6h 1m 6.5s'], - [1000 * 60 * 67 * 24 * 465 + (1000 * 60) + 6500, {hideYearAndDays: true}, '12462h 1m 6.5s'], - [1000 * 60 * 67 * 24 * 465 + (1000 * 60) + 6500, {hideYearAndDays: true, secondsDecimalDigits: 0}, '12462h 1m 6s'], - [1000 * 60 * 67 * 24 * 465 + (1000 * 60) + 6000, {hideYearAndDays: true, keepDecimalsOnWholeSeconds: true}, '12462h 1m 6.0s'], - [1000 * 60 * 67 * 24 * 465 + (1000 * 60) + 6500, {hideYearAndDays: true, separateMilliseconds: true}, '12462h 1m 6s 500ms'], - [1000 * 60 * 67 * 24 * 465 + (1000 * 60) + 6500, {hideYearAndDays: true, verbose: true}, '12462 hours 1 minute 6.5 seconds'], - [1000 * 60 * 67 * 24 * 465 + (1000 * 60) + 6500, {hideYearAndDays: true, compact: true}, '12462h'], + [(1000 * 60 * 67 * 24 * 465) + (1000 * 60) + 6500, {hideYearAndDays: false}, '1y 154d 6h 1m 6.5s'], + [(1000 * 60 * 67 * 24 * 465) + (1000 * 60) + 6500, {hideYearAndDays: true}, '12462h 1m 6.5s'], + [(1000 * 60 * 67 * 24 * 465) + (1000 * 60) + 6500, {hideYearAndDays: true, secondsDecimalDigits: 0}, '12462h 1m 6s'], + [(1000 * 60 * 67 * 24 * 465) + (1000 * 60) + 6000, {hideYearAndDays: true, keepDecimalsOnWholeSeconds: true}, '12462h 1m 6.0s'], + [(1000 * 60 * 67 * 24 * 465) + (1000 * 60) + 6500, {hideYearAndDays: true, separateMilliseconds: true}, '12462h 1m 6s 500ms'], + [(1000 * 60 * 67 * 24 * 465) + (1000 * 60) + 6500, {hideYearAndDays: true, verbose: true}, '12462 hours 1 minute 6.5 seconds'], + [(1000 * 60 * 67 * 24 * 465) + (1000 * 60) + 6500, {hideYearAndDays: true, compact: true}, '12462h'], ], }); From 3e55e0a54a83b33f1efca57462758de7fc1d7955 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Fri, 15 Nov 2024 16:25:44 +0700 Subject: [PATCH 08/10] Update index.d.ts --- index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index 318f98c..c513ad9 100644 --- a/index.d.ts +++ b/index.d.ts @@ -77,14 +77,14 @@ export type Options = { readonly colonNotation?: boolean; /** - Hides the year and shows the hidden year additionally as days (365 per year).: `1y 3d 5h 1m 45s` → `368d 5h 1m 45s`. + Hides the year and shows the hidden year additionally as days (365 per year): `1y 3d 5h 1m 45s` → `368d 5h 1m 45s`. @default false */ readonly hideYear?: boolean; /** - Hides the year and days and shows the hidden values additionally as hours.: `1y 3d 5h 1m 45s` → `8837h 1m 45s`. + Hides the year and days and shows the hidden values additionally as hours: `1y 3d 5h 1m 45s` → `8837h 1m 45s`. @default false */ From 6ea1beb5797305e5f34c009c0d5f0d740a8e3115 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Fri, 15 Nov 2024 16:26:02 +0700 Subject: [PATCH 09/10] Update index.js --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 33455d1..d8e0f0d 100644 --- a/index.js +++ b/index.js @@ -62,7 +62,7 @@ export default function prettyMilliseconds(milliseconds, options) { const days = BigInt(parsed.days); if (options.hideYearAndDays) { - add((BigInt(days) * BigInt(24)) + BigInt(parsed.hours), 'hour', 'h'); + add((BigInt(days) * 24n) + BigInt(parsed.hours), 'hour', 'h'); } else { if (options.hideYear) { add(days, 'day', 'd'); From 895fcd985fc09b288bc67fef8bc3a5a0c0eb0686 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Fri, 15 Nov 2024 16:26:58 +0700 Subject: [PATCH 10/10] Update readme.md --- readme.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/readme.md b/readme.md index ccc826e..b80ed9e 100644 --- a/readme.md +++ b/readme.md @@ -142,14 +142,14 @@ Setting `colonNotation` to `true` overrides the following options to `false`: Type: `boolean`\ Default: `false` -Hides the year and shows the hidden year additionally as days (365 per year).: `1y 3d 5h 1m 45s` → `368d 5h 1m 45s`. +Hides the year and shows the hidden year additionally as days (365 per year): `1y 3d 5h 1m 45s` → `368d 5h 1m 45s`. ##### hideYearAndDays Type: `boolean`\ Default: `false` -Hides the year and days and shows the hidden values additionally as hours.: `1y 3d 5h 1m 45s` → `8837h 1m 45s`. +Hides the year and days and shows the hidden values additionally as hours: `1y 3d 5h 1m 45s` → `8837h 1m 45s`. ##### hideSeconds @@ -158,7 +158,6 @@ Default: `false` Hides the seconds: `1y 3d 5h 1m 45s` → `1y 3d 5h 1m`. - ## Related - [pretty-ms-cli](https://github.com/sindresorhus/pretty-ms-cli) - CLI for this module