1414 *
1515 * assertEquals(format(99674, { ignoreZero: true }), "1m 39s 674ms");
1616 *
17- * assertEquals(format(99674, { style: "full", ignoreZero: true }), "1 minutes , 39 seconds, 674 milliseconds");
17+ * assertEquals(format(99674, { style: "full", ignoreZero: true }), "1 minute , 39 seconds, 674 milliseconds");
1818 * ```
1919 * @module
2020 */
@@ -34,15 +34,20 @@ interface DurationObject {
3434}
3535
3636const keyList : Record < keyof DurationObject , string > = {
37- d : "days " ,
38- h : "hours " ,
39- m : "minutes " ,
40- s : "seconds " ,
41- ms : "milliseconds " ,
42- us : "microseconds " ,
43- ns : "nanoseconds " ,
37+ d : "day " ,
38+ h : "hour " ,
39+ m : "minute " ,
40+ s : "second " ,
41+ ms : "millisecond " ,
42+ us : "microsecond " ,
43+ ns : "nanosecond " ,
4444} ;
4545
46+ /** Get key with pluralization */
47+ function getPluralizedKey ( type : keyof DurationObject , value : number ) {
48+ return value === 1 ? keyList [ type ] : `${ keyList [ type ] } s` ;
49+ }
50+
4651/** Parse milliseconds into a duration. */
4752function millisecondsToDurationObject ( ms : number ) : DurationObject {
4853 // Duration cannot be negative
@@ -109,7 +114,7 @@ export interface FormatOptions {
109114 *
110115 * assertEquals(format(99674, { ignoreZero: true }), "1m 39s 674ms");
111116 *
112- * assertEquals(format(99674, { style: "full", ignoreZero: true }), "1 minutes , 39 seconds, 674 milliseconds");
117+ * assertEquals(format(99674, { style: "full", ignoreZero: true }), "1 minute , 39 seconds, 674 milliseconds");
113118 * ```
114119 *
115120 * @param ms The milliseconds value to format
@@ -146,12 +151,14 @@ export function format(
146151 if ( ignoreZero ) {
147152 return `${
148153 durationArr . filter ( ( x ) => x . value ) . map ( ( x ) =>
149- `${ x . value } ${ keyList [ x . type ] } `
154+ `${ x . value } ${ getPluralizedKey ( x . type , x . value ) } `
150155 ) . join ( ", " )
151156 } `;
152157 }
153158 return `${
154- durationArr . map ( ( x ) => `${ x . value } ${ keyList [ x . type ] } ` ) . join ( ", " )
159+ durationArr . map ( ( x ) =>
160+ `${ x . value } ${ getPluralizedKey ( x . type , x . value ) } `
161+ ) . join ( ", " )
155162 } `;
156163 }
157164 case "digital" : {
0 commit comments