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
4function splitGlyphs(value: string) {
5 return value.match(/\P{M}\p{M}*/gu) ?? [];
6}
7
8export function projectMonogram(projectName: string): string {
9 const words =
10 projectName
11 .normalize("NFKC")
12 .trim()
13 .match(/[\p{L}\p{N}][\p{L}\p{N}\p{M}]*/gu) ?? [];
14 const firstWord = words[0];
15 if (!firstWord) return "PR";
16
17 const glyphs = splitGlyphs(firstWord);
18 const first = glyphs[0] ?? "P";
19 if (SINGLE_GLYPH_SCRIPT.test(first)) return first;
20 const second =
21 glyphs.slice(1).find((glyph) => /\p{N}/u.test(glyph)) ??
22 (words.length > 1 ? splitGlyphs(words.at(-1) ?? "")[0] : glyphs.at(-1)) ??
23 first;
24 return splitGlyphs(`${first}${second}`.toUpperCase()).slice(0, 2).join("");
25}