@@ -4,10 +4,12 @@ const isZero = value => value === 0 || value === 0n;
44const pluralize = ( word , count ) => ( count === 1 || count === 1n ) ? word : `${ word } s` ;
55
66const SECOND_ROUNDING_EPSILON = 0.000_000_1 ;
7+ const ONE_DAY_IN_MILLISECONDS = 24n * 60n * 60n * 1000n ;
78
89export default function prettyMilliseconds ( milliseconds , options ) {
9- if ( ! Number . isFinite ( milliseconds ) ) {
10- throw new TypeError ( 'Expected a finite number' ) ;
10+ const isBigInt = typeof milliseconds === 'bigint' ;
11+ if ( ! isBigInt && ! Number . isFinite ( milliseconds ) ) {
12+ throw new TypeError ( 'Expected a finite number or bigint' ) ;
1113 }
1214
1315 options = { ...options } ;
@@ -58,27 +60,34 @@ export default function prettyMilliseconds(milliseconds, options) {
5860 } ;
5961
6062 const parsed = parseMilliseconds ( milliseconds ) ;
63+ const days = BigInt ( parsed . days ) ;
6164
62- add ( BigInt ( parsed . days ) / 365n , 'year' , 'y' ) ;
63- add ( parsed . days % 365 , 'day' , 'd' ) ;
64- add ( parsed . hours , 'hour' , 'h' ) ;
65- add ( parsed . minutes , 'minute' , 'm' ) ;
65+ add ( days / 365n , 'year' , 'y' ) ;
66+ add ( days % 365n , 'day' , 'd' ) ;
67+ add ( Number ( parsed . hours ) , 'hour' , 'h' ) ;
68+ add ( Number ( parsed . minutes ) , 'minute' , 'm' ) ;
6669
6770 if (
6871 options . separateMilliseconds
6972 || options . formatSubMilliseconds
7073 || ( ! options . colonNotation && milliseconds < 1000 )
7174 ) {
72- add ( parsed . seconds , 'second' , 's' ) ;
75+ const seconds = Number ( parsed . seconds ) ;
76+ const milliseconds = Number ( parsed . milliseconds ) ;
77+ const microseconds = Number ( parsed . microseconds ) ;
78+ const nanoseconds = Number ( parsed . nanoseconds ) ;
79+
80+ add ( seconds , 'second' , 's' ) ;
81+
7382 if ( options . formatSubMilliseconds ) {
74- add ( parsed . milliseconds , 'millisecond' , 'ms' ) ;
75- add ( parsed . microseconds , 'microsecond' , 'µs' ) ;
76- add ( parsed . nanoseconds , 'nanosecond' , 'ns' ) ;
83+ add ( milliseconds , 'millisecond' , 'ms' ) ;
84+ add ( microseconds , 'microsecond' , 'µs' ) ;
85+ add ( nanoseconds , 'nanosecond' , 'ns' ) ;
7786 } else {
7887 const millisecondsAndBelow
79- = parsed . milliseconds
80- + ( parsed . microseconds / 1000 )
81- + ( parsed . nanoseconds / 1e6 ) ;
88+ = milliseconds
89+ + ( microseconds / 1000 )
90+ + ( nanoseconds / 1e6 ) ;
8291
8392 const millisecondsDecimalDigits
8493 = typeof options . millisecondsDecimalDigits === 'number'
@@ -101,7 +110,10 @@ export default function prettyMilliseconds(milliseconds, options) {
101110 ) ;
102111 }
103112 } else {
104- const seconds = ( milliseconds / 1000 ) % 60 ;
113+ const seconds = (
114+ ( isBigInt ? Number ( milliseconds % ONE_DAY_IN_MILLISECONDS ) : milliseconds )
115+ / 1000
116+ ) % 60 ;
105117 const secondsDecimalDigits
106118 = typeof options . secondsDecimalDigits === 'number'
107119 ? options . secondsDecimalDigits
0 commit comments