infoalloggi-monorepo/apps/infoalloggi/src/utils/utils.ts

37 lines
943 B
TypeScript

export const run = <TResult>(fn: () => TResult): TResult => fn();
export const pluralize = (count: number, singular: string, plural: string) =>
count === 1 ? singular : plural;
export const listWithAnd = (list: string[]) => {
if (list.length === 0) {
return "";
}
if (list.length === 1) {
return list[0];
}
if (list.length === 2) {
return `${list[0]} and ${list[1]}`;
}
return `${list.slice(0, -1).join(", ")}, and ${list.at(-1)}`;
};
export function titleCase(str: string | null | undefined): string {
if (!str) return "";
return str
.trim()
.toLowerCase()
.split(/\s+/) // Handle multiple spaces
.map((word) => {
if (word.length === 0) return word;
return word.charAt(0).toUpperCase() + word.slice(1);
})
.join(" ");
}
export const startsWithVowel = (s: string) => /^[aeiouàèéìòù]/i.test(s);
export function assertNever(x: never): never {
throw new Error(`Unexpected: ${JSON.stringify(x)}`);
}