Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

projectMonogram.ts

37 lines1.8 KB

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​
4// A Brahmic virama or Khmer coeng joins the next consonant of its own script into one conjunct (क्ष, ক্ষ, ខ្ម).
5const CONJUNCT =
6 "\u094D\u200D?[\u0915-\u0939\u0958-\u095F\u0978-\u097F]|\u09CD\u200D?[\u0995-\u09B9\u09DC-\u09DF\u09F0\u09F1]|\u0A4D\u200D?[\u0A15-\u0A39\u0A59-\u0A5E]|\u0ACD\u200D?[\u0A95-\u0AB9\u0AF9]|\u0B4D\u200D?[\u0B15-\u0B39\u0B5C-\u0B5F\u0B71]|\u0C4D\u200D?[\u0C15-\u0C39\u0C58-\u0C5A\u0C5D]|\u0CCD\u200D?[\u0C95-\u0CB9\u0CDD\u0CDE]|\u17D2[\u1780-\u17A2]";
7const GLYPH = new RegExp(`\\P{M}(?:${CONJUNCT}|\\p{M})*`, "gu");
8const WORD = new RegExp(
9 `[\\p{L}\\p{N}](?:(?=${CONJUNCT})\\p{M}\u200D|[\\p{L}\\p{N}\\p{M}])*`,
10 "gu",
11);
12​
13function splitGlyphs(value: string) {
14 return value.match(GLYPH) ?? [];
16​
17export function projectMonogram(projectName: string): string {
18 const words = projectName.normalize("NFKC").trim().match(WORD) ?? [];
19 const firstWord = words[0];
20 if (!firstWord) return "PR";
21​
22 const glyphs = splitGlyphs(firstWord);
23 const first = glyphs[0] ?? "P";
24 if (SINGLE_GLYPH_SCRIPT.test(first)) return first;
25 // A lone Khmer stacked cluster (ថ្មី) already holds two consonants; repeating it would double the name.
26 if (words.length === 1 && glyphs.length === 1 && first.includes("\u17D2")) return first;
27 const second =
28 glyphs.slice(1).find((glyph) => /\p{N}/u.test(glyph)) ??
29 (words.length > 1 ? splitGlyphs(words.at(-1) ?? "")[0] : glyphs.at(-1)) ??
30 first;
31 return `${upperInitial(first)}${upperInitial(second)}`;
33​
34// Some letters uppercase to several (ß → SS); keep one per slot so neither initial is lost.
35function upperInitial(glyph: string) {
36 return splitGlyphs(glyph.toUpperCase())[0] ?? glyph;