@@ -23,58 +23,62 @@ function addZero(num: number, digits: number) {
2323 return String ( num ) . padStart ( digits , "0" ) ;
2424}
2525
26- interface DurationObject {
27- d : number ;
28- h : number ;
29- m : number ;
30- s : number ;
31- ms : number ;
32- us : number ;
33- ns : number ;
26+ type DurationPartUnit =
27+ | "days"
28+ | "hours"
29+ | "minutes"
30+ | "seconds"
31+ | "milliseconds"
32+ | "microseconds"
33+ | "nanoseconds" ;
34+
35+ interface DurationPart {
36+ unit : DurationPartUnit ;
37+ value : number ;
3438}
3539
36- const keyList : Record < keyof DurationObject , string > = {
37- d : "day" ,
38- h : "hour" ,
39- m : "minute" ,
40- s : "second" ,
41- ms : "millisecond" ,
42- us : "microsecond" ,
43- ns : "nanosecond" ,
44- } ;
40+ const NARROW_UNIT_NAME_MAP = new Map < DurationPartUnit , string > ( [
41+ [ "days" , "d" ] ,
42+ [ "hours" , "h" ] ,
43+ [ "minutes" , "m" ] ,
44+ [ "seconds" , "s" ] ,
45+ [ "milliseconds" , "ms" ] ,
46+ [ "microseconds" , "µs" ] ,
47+ [ "nanoseconds" , "ns" ] ,
48+ ] ) ;
49+
50+ const FULL_UNIT_NAME_MAP = new Map < DurationPartUnit , string > ( [
51+ [ "days" , "day" ] ,
52+ [ "hours" , "hour" ] ,
53+ [ "minutes" , "minute" ] ,
54+ [ "seconds" , "second" ] ,
55+ [ "milliseconds" , "millisecond" ] ,
56+ [ "microseconds" , "microsecond" ] ,
57+ [ "nanoseconds" , "nanosecond" ] ,
58+ ] ) ;
4559
4660/** Get key with pluralization */
47- function getPluralizedKey ( type : keyof DurationObject , value : number ) {
48- return value === 1 ? keyList [ type ] : `${ keyList [ type ] } s` ;
61+ function getPluralizedKey ( unit : DurationPartUnit , value : number ) {
62+ return value === 1
63+ ? FULL_UNIT_NAME_MAP . get ( unit )
64+ : `${ FULL_UNIT_NAME_MAP . get ( unit ) } s` ;
4965}
5066
5167/** Parse milliseconds into a duration. */
52- function millisecondsToDurationObject ( ms : number ) : DurationObject {
68+ function millisecondsToDurationParts (
69+ ms : number ,
70+ ) : DurationPart [ ] {
5371 // Duration cannot be negative
5472 const millis = Math . abs ( ms ) ;
5573 const millisFraction = millis . toFixed ( 7 ) . slice ( - 7 , - 1 ) ;
56- return {
57- d : Math . trunc ( millis / 86400000 ) ,
58- h : Math . trunc ( millis / 3600000 ) % 24 ,
59- m : Math . trunc ( millis / 60000 ) % 60 ,
60- s : Math . trunc ( millis / 1000 ) % 60 ,
61- ms : Math . trunc ( millis ) % 1000 ,
62- us : + millisFraction . slice ( 0 , 3 ) ,
63- ns : + millisFraction . slice ( 3 , 6 ) ,
64- } ;
65- }
66-
67- function durationArray (
68- duration : DurationObject ,
69- ) : { type : keyof DurationObject ; value : number } [ ] {
7074 return [
71- { type : "d " , value : duration . d } ,
72- { type : "h " , value : duration . h } ,
73- { type : "m " , value : duration . m } ,
74- { type : "s " , value : duration . s } ,
75- { type : "ms " , value : duration . ms } ,
76- { type : "us " , value : duration . us } ,
77- { type : "ns " , value : duration . ns } ,
75+ { unit : "days " , value : Math . trunc ( millis / 86400000 ) } ,
76+ { unit : "hours " , value : Math . trunc ( millis / 3600000 ) % 24 } ,
77+ { unit : "minutes " , value : Math . trunc ( millis / 60000 ) % 60 } ,
78+ { unit : "seconds " , value : Math . trunc ( millis / 1000 ) % 60 } ,
79+ { unit : "milliseconds " , value : Math . trunc ( millis ) % 1000 } ,
80+ { unit : "microseconds " , value : + millisFraction . slice ( 0 , 3 ) } ,
81+ { unit : "nanoseconds " , value : + millisFraction . slice ( 3 , 6 ) } ,
7882 ] ;
7983}
8084
@@ -130,40 +134,26 @@ export function format(
130134 ignoreZero = false ,
131135 } = options ?? { } ;
132136
133- const duration = millisecondsToDurationObject ( ms ) ;
134- const durationArr = durationArray ( duration ) ;
137+ const parts = millisecondsToDurationParts ( ms ) ;
138+
135139 switch ( style ) {
136140 case "narrow" : {
137- if ( ignoreZero ) {
138- return `${
139- durationArr . filter ( ( x ) => x . value ) . map ( ( x ) =>
140- `${ x . value } ${ x . type === "us" ? "µs" : x . type } `
141- )
142- . join ( " " )
143- } `;
144- }
145- return `${
146- durationArr . map ( ( x ) => `${ x . value } ${ x . type === "us" ? "µs" : x . type } ` )
147- . join ( " " )
148- } `;
141+ let arr = parts ;
142+ if ( ignoreZero ) arr = arr . filter ( ( x ) => x . value ) ;
143+ return arr
144+ . map ( ( x ) => `${ x . value } ${ NARROW_UNIT_NAME_MAP . get ( x . unit ) } ` )
145+ . join ( " " ) ;
149146 }
150147 case "full" : {
151- if ( ignoreZero ) {
152- return `${
153- durationArr . filter ( ( x ) => x . value ) . map ( ( x ) =>
154- `${ x . value } ${ getPluralizedKey ( x . type , x . value ) } `
155- ) . join ( ", " )
156- } `;
157- }
158- return `${
159- durationArr . map ( ( x ) =>
160- `${ x . value } ${ getPluralizedKey ( x . type , x . value ) } `
161- ) . join ( ", " )
162- } `;
148+ let arr = parts ;
149+ if ( ignoreZero ) arr = arr . filter ( ( x ) => x . value ) ;
150+ return arr
151+ . map ( ( x ) => `${ x . value } ${ getPluralizedKey ( x . unit , x . value ) } ` )
152+ . join ( ", " ) ;
163153 }
164154 case "digital" : {
165- const arr = durationArr . map ( ( x ) =>
166- [ "ms " , "us " , "ns " ] . includes ( x . type )
155+ const arr = parts . map ( ( x ) =>
156+ [ "milliseconds " , "microseconds " , "nanoseconds " ] . includes ( x . unit )
167157 ? addZero ( x . value , 3 )
168158 : addZero ( x . value , 2 )
169159 ) ;
0 commit comments