Skip to content

Commit 32d8968

Browse files
committed
Use pluralize function for pluralization
1 parent 768c107 commit 32d8968

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

extensions/ql-vscode/src/pure/word.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ export function pluralize(
77
numItems: number | undefined,
88
singular: string,
99
plural: string,
10+
numberFormatter: (value: number) => string = (value) => value.toString(),
1011
): string {
1112
return numItems !== undefined
12-
? `${numItems} ${numItems === 1 ? singular : plural}`
13+
? `${numberFormatter(numItems)} ${numItems === 1 ? singular : plural}`
1314
: "";
1415
}

extensions/ql-vscode/src/view/data-extensions-editor/LibraryRow.tsx

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useCallback, useMemo, useState } from "react";
33
import styled from "styled-components";
44
import { ExternalApiUsage } from "../../data-extensions-editor/external-api-usage";
55
import { ModeledMethod } from "../../data-extensions-editor/modeled-method";
6+
import { pluralize } from "../../pure/word";
67
import { ModeledMethodDataGrid } from "./ModeledMethodDataGrid";
78
import { calculateModeledPercentage } from "./modeled";
89
import { decimalFormatter, percentFormatter } from "./formatters";
@@ -72,22 +73,35 @@ export const LibraryRow = ({
7273
{isExpanded ? null : (
7374
<>
7475
{" "}
75-
({decimalFormatter.format(externalApiUsages.length)} method
76-
{externalApiUsages.length !== 1 ? "s" : ""},{" "}
77-
{percentFormatter.format(modeledPercentage / 100)} modeled)
76+
(
77+
{pluralize(
78+
externalApiUsages.length,
79+
"method",
80+
"methods",
81+
decimalFormatter.format.bind(decimalFormatter),
82+
)}
83+
, {percentFormatter.format(modeledPercentage / 100)} modeled)
7884
</>
7985
)}
8086
</TitleContainer>
8187
{isExpanded && (
8288
<>
8389
<StatusContainer>
8490
<div>
85-
{decimalFormatter.format(externalApiUsages.length)} method
86-
{externalApiUsages.length !== 1 ? "s" : ""}
91+
{pluralize(
92+
externalApiUsages.length,
93+
"method",
94+
"methods",
95+
decimalFormatter.format.bind(decimalFormatter),
96+
)}
8797
</div>
8898
<div>
89-
{decimalFormatter.format(usagesCount)} usage
90-
{usagesCount !== 1 ? "s" : ""}
99+
{pluralize(
100+
usagesCount,
101+
"usage",
102+
"usages",
103+
decimalFormatter.format.bind(decimalFormatter),
104+
)}
91105
</div>
92106
<div>
93107
{percentFormatter.format(modeledPercentage / 100)} modeled

extensions/ql-vscode/test/unit-tests/pure/word.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,22 @@ describe("word helpers", () => {
1414
it("should return the empty string if the number is undefined", () => {
1515
expect(pluralize(undefined, "thing", "things")).toBe("");
1616
});
17+
it("should return an unformatted number when no formatter is specified", () => {
18+
expect(pluralize(1_000_000, "thing", "things")).toBe("1000000 things");
19+
});
20+
it("should return a formatted number when a formatter is specified", () => {
21+
const formatter = new Intl.NumberFormat("en-US", {
22+
style: "decimal",
23+
});
24+
25+
expect(
26+
pluralize(
27+
1_000_000,
28+
"thing",
29+
"things",
30+
formatter.format.bind(formatter),
31+
),
32+
).toBe("1,000,000 things");
33+
});
1734
});
1835
});

0 commit comments

Comments
 (0)