summaryrefslogtreecommitdiff
path: root/src/lib/utils.ts
blob: 0674dea175719fd20efce929b0ae0e0b34f7aa0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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<string> {
  const combinations: Set<string> = 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<number> {
  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<T>(
  settlement: PromiseSettledResult<T>,
): 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, "");
}