From 5982abfb05798e415730be5743b6d5504111c837 Mon Sep 17 00:00:00 2001 From: Z8MB1E Date: Tue, 28 Jul 2026 14:02:41 -0400 Subject: [PATCH] feat(ui): add pluralize utility function Handles irregular plurals, measurement units that stay singular, and standard English pluralization rules. --- src/lib/utils.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/lib/utils.ts b/src/lib/utils.ts index bd0c391..3638f35 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -4,3 +4,25 @@ import { twMerge } from "tailwind-merge" export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)) } + +const IRREGULAR: Record = { + person: "people", + child: "children", + man: "men", + woman: "women", +}; + +const NO_PLURAL = new Set([ + "kg", "g", "mg", "mcg", "l", "ml", "cl", "dl", + "m", "cm", "mm", "km", "tick", "hertz", "hz", +]); + +export function pluralize(amount: number, unit: string): string { + if (amount === 1) return unit; + const lower = unit.toLowerCase(); + if (NO_PLURAL.has(lower) || unit.endsWith("s")) return unit; + if (IRREGULAR[lower]) return IRREGULAR[lower]; + if (/[^aeiou]y$/i.test(unit)) return unit.slice(0, -1) + "ies"; + if (/[sxz]$/i.test(unit) || /(sh|ch)$/i.test(unit)) return unit + "es"; + return unit + "s"; +}