import { type ClassValue, clsx } from "clsx"; import { twMerge } from "tailwind-merge"; import type { Result } from "./types"; export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)); } export function wordFactorial(words: string[]): Set { const combinations: Set = new Set([]); for (let i = 0; i < words.length; i++) { let inner = ""; for (let ii = i; ii < words.length; ii++) { inner += (ii > i ? " " : "") + words[ii]!.toLowerCase(); combinations.add(inner); } } return combinations; } export function getSyllableCount(ipa: string): number { const syllables = ipa .replace(/\//g, "") .split(/[ˌ\.ˈ]/) .filter(Boolean); return syllables.length; } export function getStressedSyllable(ipa: string): Result { const split = ipa.replace(/\//g, "").split(/ˈ/); if (split.length === 1) { if (getSyllableCount(ipa) === 1) return { ok: 1 }; else return { error: "No stress mark" }; } const preSplit = split[0]; if (!preSplit) return { ok: 1 }; else { const pp = preSplit.split(/[ˌ\.]/g); return { ok: pp.length + 1 }; } } export function getDBOffset(page: number, pageSize: number) { return (page - 1) * pageSize; } export function handlePromise( settlement: PromiseSettledResult, ): T | string { if (settlement.status === "fulfilled") return settlement.value; else return `${settlement.reason}`; } export function getRandomHexColor() { // Generate a random number and convert it to a hexadecimal string const randomColor = Math.floor(Math.random() * 16777215).toString(16); // Ensure the color code is always 6 digits by padding with zeros if needed return "#" + randomColor.padStart(6, "0"); } export function cleanIpa(ipa: string): string { const r1 = /\.\//; const r2 = /[\[\]\/]/g; return ipa.replace(r1, "").replace(r2, ""); } export function randomFromArray(arr: T[]): T { const idx = Math.floor(Math.random() * arr.length); return arr[idx]!; }