Skip to content

Commit d02d714

Browse files
fix(fmt): add correct pluralization to fmt/duration when using style: full (#6295)
1 parent 9bf17e4 commit d02d714

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

fmt/duration.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
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

3636
const 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. */
4752
function 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": {

fmt/duration_test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Deno.test({
3232
fn() {
3333
assertEquals(
3434
format(99674, { style: "full" }),
35-
"0 days, 0 hours, 1 minutes, 39 seconds, 674 milliseconds, 0 microseconds, 0 nanoseconds",
35+
"0 days, 0 hours, 1 minute, 39 seconds, 674 milliseconds, 0 microseconds, 0 nanoseconds",
3636
);
3737
},
3838
});
@@ -62,7 +62,7 @@ Deno.test({
6262
fn() {
6363
assertEquals(
6464
format(99674, { style: "full", ignoreZero: true }),
65-
"1 minutes, 39 seconds, 674 milliseconds",
65+
"1 minute, 39 seconds, 674 milliseconds",
6666
);
6767
},
6868
});

0 commit comments

Comments
 (0)