2021-04-14 16:52:15 +00:00
|
|
|
const DEFAULT_RESULTS_NAME = "results";
|
2021-04-04 12:45:24 +00:00
|
|
|
|
2025-10-07 21:24:05 +00:00
|
|
|
/**
|
|
|
|
|
* Returns a proper results count text — singular or plural as needed.
|
|
|
|
|
* For regular cases, computes singular by removing "ies" or "s".
|
|
|
|
|
* For irregular cases, pass singularName.
|
|
|
|
|
*/
|
2024-06-14 17:12:56 +00:00
|
|
|
export const generateResultsCountText = (
|
2021-04-14 16:52:15 +00:00
|
|
|
name: string = DEFAULT_RESULTS_NAME,
|
2025-10-07 21:24:05 +00:00
|
|
|
resultsCount?: number,
|
|
|
|
|
singularName?: string
|
2021-06-10 14:00:03 +00:00
|
|
|
): string => {
|
2024-06-14 17:12:56 +00:00
|
|
|
if (!resultsCount || resultsCount === 0) return `0 ${name}`;
|
2021-07-26 17:01:58 +00:00
|
|
|
|
2025-10-07 21:24:05 +00:00
|
|
|
// If exactly 1 result, return singular form.
|
|
|
|
|
if (resultsCount === 1) {
|
|
|
|
|
if (singularName) {
|
|
|
|
|
return `1 ${singularName}`;
|
|
|
|
|
}
|
|
|
|
|
if (name.endsWith("ies")) {
|
|
|
|
|
return `1 ${name.slice(0, -3)}y`;
|
|
|
|
|
}
|
|
|
|
|
if (name.endsWith("s")) {
|
|
|
|
|
return `1 ${name.slice(0, -1)}`;
|
|
|
|
|
}
|
2021-07-26 17:01:58 +00:00
|
|
|
}
|
2021-04-09 10:44:57 +00:00
|
|
|
|
2023-12-21 17:23:07 +00:00
|
|
|
return `${resultsCount.toLocaleString()} ${name}`;
|
2021-04-04 12:45:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default { generateResultsCountText };
|