Use pluralize function for pluralization
This commit is contained in:
@@ -7,8 +7,9 @@ export function pluralize(
|
|||||||
numItems: number | undefined,
|
numItems: number | undefined,
|
||||||
singular: string,
|
singular: string,
|
||||||
plural: string,
|
plural: string,
|
||||||
|
numberFormatter: (value: number) => string = (value) => value.toString(),
|
||||||
): string {
|
): string {
|
||||||
return numItems !== undefined
|
return numItems !== undefined
|
||||||
? `${numItems} ${numItems === 1 ? singular : plural}`
|
? `${numberFormatter(numItems)} ${numItems === 1 ? singular : plural}`
|
||||||
: "";
|
: "";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { useCallback, useMemo, useState } from "react";
|
|||||||
import styled from "styled-components";
|
import styled from "styled-components";
|
||||||
import { ExternalApiUsage } from "../../data-extensions-editor/external-api-usage";
|
import { ExternalApiUsage } from "../../data-extensions-editor/external-api-usage";
|
||||||
import { ModeledMethod } from "../../data-extensions-editor/modeled-method";
|
import { ModeledMethod } from "../../data-extensions-editor/modeled-method";
|
||||||
|
import { pluralize } from "../../pure/word";
|
||||||
import { ModeledMethodDataGrid } from "./ModeledMethodDataGrid";
|
import { ModeledMethodDataGrid } from "./ModeledMethodDataGrid";
|
||||||
import { calculateModeledPercentage } from "./modeled";
|
import { calculateModeledPercentage } from "./modeled";
|
||||||
import { decimalFormatter, percentFormatter } from "./formatters";
|
import { decimalFormatter, percentFormatter } from "./formatters";
|
||||||
@@ -72,9 +73,14 @@ export const LibraryRow = ({
|
|||||||
{isExpanded ? null : (
|
{isExpanded ? null : (
|
||||||
<>
|
<>
|
||||||
{" "}
|
{" "}
|
||||||
({decimalFormatter.format(externalApiUsages.length)} method
|
(
|
||||||
{externalApiUsages.length !== 1 ? "s" : ""},{" "}
|
{pluralize(
|
||||||
{percentFormatter.format(modeledPercentage / 100)} modeled)
|
externalApiUsages.length,
|
||||||
|
"method",
|
||||||
|
"methods",
|
||||||
|
decimalFormatter.format.bind(decimalFormatter),
|
||||||
|
)}
|
||||||
|
, {percentFormatter.format(modeledPercentage / 100)} modeled)
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
</TitleContainer>
|
</TitleContainer>
|
||||||
@@ -82,12 +88,20 @@ export const LibraryRow = ({
|
|||||||
<>
|
<>
|
||||||
<StatusContainer>
|
<StatusContainer>
|
||||||
<div>
|
<div>
|
||||||
{decimalFormatter.format(externalApiUsages.length)} method
|
{pluralize(
|
||||||
{externalApiUsages.length !== 1 ? "s" : ""}
|
externalApiUsages.length,
|
||||||
|
"method",
|
||||||
|
"methods",
|
||||||
|
decimalFormatter.format.bind(decimalFormatter),
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
{decimalFormatter.format(usagesCount)} usage
|
{pluralize(
|
||||||
{usagesCount !== 1 ? "s" : ""}
|
usagesCount,
|
||||||
|
"usage",
|
||||||
|
"usages",
|
||||||
|
decimalFormatter.format.bind(decimalFormatter),
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
{percentFormatter.format(modeledPercentage / 100)} modeled
|
{percentFormatter.format(modeledPercentage / 100)} modeled
|
||||||
|
|||||||
@@ -14,5 +14,22 @@ describe("word helpers", () => {
|
|||||||
it("should return the empty string if the number is undefined", () => {
|
it("should return the empty string if the number is undefined", () => {
|
||||||
expect(pluralize(undefined, "thing", "things")).toBe("");
|
expect(pluralize(undefined, "thing", "things")).toBe("");
|
||||||
});
|
});
|
||||||
|
it("should return an unformatted number when no formatter is specified", () => {
|
||||||
|
expect(pluralize(1_000_000, "thing", "things")).toBe("1000000 things");
|
||||||
|
});
|
||||||
|
it("should return a formatted number when a formatter is specified", () => {
|
||||||
|
const formatter = new Intl.NumberFormat("en-US", {
|
||||||
|
style: "decimal",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
pluralize(
|
||||||
|
1_000_000,
|
||||||
|
"thing",
|
||||||
|
"things",
|
||||||
|
formatter.format.bind(formatter),
|
||||||
|
),
|
||||||
|
).toBe("1,000,000 things");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user