41 lines
857 B
TypeScript
41 lines
857 B
TypeScript
import { type ClassValue, clsx } from "clsx";
|
|
import { twMerge } from "tailwind-merge";
|
|
|
|
export function cn(...inputs: ClassValue[]) {
|
|
return twMerge(clsx(inputs));
|
|
}
|
|
|
|
const IRREGULAR: Record<string, string> = {
|
|
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";
|
|
}
|