@@ -4,10 +4,12 @@ const isZero = value => value === 0 || value === 0n;
4
4
const pluralize = ( word , count ) => ( count === 1 || count === 1n ) ? word : `${ word } s` ;
5
5
6
6
const SECOND_ROUNDING_EPSILON = 0.000_000_1 ;
7
+ const ONE_DAY_IN_MILLISECONDS = 24n * 60n * 60n * 1000n ;
7
8
8
9
export 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' ) ;
11
13
}
12
14
13
15
options = { ...options } ;
@@ -58,27 +60,34 @@ export default function prettyMilliseconds(milliseconds, options) {
58
60
} ;
59
61
60
62
const parsed = parseMilliseconds ( milliseconds ) ;
63
+ const days = BigInt ( parsed . days ) ;
61
64
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' ) ;
66
69
67
70
if (
68
71
options . separateMilliseconds
69
72
|| options . formatSubMilliseconds
70
73
|| ( ! options . colonNotation && milliseconds < 1000 )
71
74
) {
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
+
73
82
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' ) ;
77
86
} else {
78
87
const millisecondsAndBelow
79
- = parsed . milliseconds
80
- + ( parsed . microseconds / 1000 )
81
- + ( parsed . nanoseconds / 1e6 ) ;
88
+ = milliseconds
89
+ + ( microseconds / 1000 )
90
+ + ( nanoseconds / 1e6 ) ;
82
91
83
92
const millisecondsDecimalDigits
84
93
= typeof options . millisecondsDecimalDigits === 'number'
@@ -101,7 +110,10 @@ export default function prettyMilliseconds(milliseconds, options) {
101
110
) ;
102
111
}
103
112
} else {
104
- const seconds = ( milliseconds / 1000 ) % 60 ;
113
+ const seconds = (
114
+ ( isBigInt ? Number ( milliseconds % ONE_DAY_IN_MILLISECONDS ) : milliseconds )
115
+ / 1000
116
+ ) % 60 ;
105
117
const secondsDecimalDigits
106
118
= typeof options . secondsDecimalDigits === 'number'
107
119
? options . secondsDecimalDigits
0 commit comments