Open History to see the last commit
1// One ideograph or syllable block already reads as a compact mark; two look bulky.
2const SINGLE_GLYPH_SCRIPT = /^[\p{scx=Han}\p{scx=Hiragana}\p{scx=Katakana}\p{scx=Hangul}]/u;
3
4export function projectMonogram(projectName: string): string {
5 const words =
6 projectName
7 .normalize("NFKC")
8 .trim()
9 .match(/[\p{L}\p{N}]+/gu) ?? [];
10 const firstWord = words[0];
11 if (!firstWord) return "PR";
12
13 const glyphs = Array.from(firstWord);
14 const first = glyphs[0] ?? "P";
15 if (SINGLE_GLYPH_SCRIPT.test(first)) return first;
16 const second =
17 glyphs.slice(1).find((glyph) => /\p{N}/u.test(glyph)) ??
18 (words.length > 1 ? Array.from(words.at(-1) ?? "")[0] : glyphs.at(-1)) ??
19 first;
20 return Array.from(`${first}${second}`.toUpperCase()).slice(0, 2).join("");
21}