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

projectMonogram.ts

40 lines2.2 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 (क्ष, ক্ষ, ខ្ម, က္က, ᬓ᭄ᬱ, ꦏ꧀ꦱ).
5// Malayalam virama + ZWJ spells a legacy chillu (ര്\u200D = ർ), not a conjunct, so it takes no optional ZWJ.
6const STACKED_CONJUNCT =
7 "\u0DCA\u200D[\u0D9A-\u0DC6]|\u1039[\u1000-\u1021]|\u1B44[\u1B0B\u1B0C\u1B13-\u1B33\u1B45-\u1B4C]|\uA9C0[\uA989-\uA98B\uA98F-\uA9B2]";
8const CONJUNCT = `\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]|\u0D4D[\u0D15-\u0D3A]|\u17D2[\u1780-\u17A2]|${STACKED_CONJUNCT}`;
9const LONE_STACK = new RegExp(`\u17D2|${STACKED_CONJUNCT}`, "u");
10const GLYPH = new RegExp(`\\P{M}(?:${CONJUNCT}|\\p{M})*`, "gu");
11const WORD = new RegExp(
12 `[\\p{L}\\p{N}](?:(?=${CONJUNCT})\\p{M}\u200D|[\\p{L}\\p{N}\\p{M}])*`,
13 "gu",
14);
15​
16function splitGlyphs(value: string) {
17 return value.match(GLYPH) ?? [];
19​
20export function projectMonogram(projectName: string): string {
21 const words = projectName.normalize("NFKC").trim().match(WORD) ?? [];
22 const firstWord = words[0];
23 if (!firstWord) return "PR";
24​
25 const glyphs = splitGlyphs(firstWord);
26 const first = glyphs[0] ?? "P";
27 if (SINGLE_GLYPH_SCRIPT.test(first)) return first;
28 // A lone stacked cluster (ថ្មី, က္က) already holds two consonants; repeating it would double the name.
29 if (words.length === 1 && glyphs.length === 1 && LONE_STACK.test(first)) return first;
30 const second =
31 glyphs.slice(1).find((glyph) => /\p{N}/u.test(glyph)) ??
32 (words.length > 1 ? splitGlyphs(words.at(-1) ?? "")[0] : glyphs.at(-1)) ??
33 first;
34 return `${upperInitial(first)}${upperInitial(second)}`;
36​
37// Some letters uppercase to several (ß → SS); keep one per slot so neither initial is lost.
38function upperInitial(glyph: string) {
39 return splitGlyphs(glyph.toUpperCase())[0] ?? glyph;