String.prototype.batchReplace = function (table) {
const keys = Object.keys(table).sort((a, b) => b.length - a.length);
const regex = new RegExp(keys.join('|'), 'g');
return this.replace(regex, match => table[match]);
};
let wylieToTibetan = function (wylie) {
const consonantTable = {
"k": 0xf40,
"x": 0xf41,
"g": 0xf42,
"ŋ": 0xf44,
"c": 0xf45,
"ʨ": 0xf46,
"j": 0xf47,
"ɲ": 0xf49,
"t": 0xf4f,
"θ": 0xf50,
"d": 0xf51,
"n": 0xf53,
"p": 0xf54,
"ɸ": 0xf55,
"b": 0xf56,
"m": 0xf58,
"ʦ": 0xf59,
"ʩ": 0xf5a,
"ʣ": 0xf5b,
"w": 0xf5d,
"ʑ": 0xf5e,
"z": 0xf5f,
"'": 0xf60,
"v": 0xf60,
"y": 0xf61,
"r": 0xf62,
"l": 0xf63,
"ɕ": 0xf64,
"s": 0xf66,
"h": 0xf67,
};
const vowelTable = {
"ai": "ཻ",
"au": "ཽ",
"i": "ི",
"u": "ུ",
"e": "ེ",
"o": "ོ",
};
// Convert digraphs
const digraphTable = {
"kh": "x",
"ng": "ŋ",
"ch": "ʨ",
"ny": "ɲ",
"th": "θ",
"ph": "ɸ",
"ts": "ʦ",
"tsh": "ʩ",
"dz": "ʣ",
"zh": "ʑ",
"sh": "ɕ"
};
/*
for (let digraph in digraphTable) {
wylie = wylie.replaceAll(digraph, digraphTable[digraph]);
};
*/
wylie = wylie.batchReplace(digraphTable);
// Process force stackings
/*
for (let stack of (wylie.match(/[\w']\+[\w']/g) || [])) {
wylie = wylie.replace(stack, String.fromCharCode(consonantTable[stack[0]], consonantTable[stack[2]] + 0x50));
};
*/
wylie = wylie.replace(/[a-zA-Z'\u0f40-\u0fbf]+(?:\+[a-zA-Z'\u0f40-\u0fbf])+/g, (match) => {
const parts = match.split('+');
let result = "";
parts.forEach((part, idx) => {
if (consonantTable[part]) {
const code = consonantTable[part] + (idx === 0 ? 0 : 0x50);
result += String.fromCharCode(code);
} else {
result += part;
};
});
return result;
});
let output = [];
let syls = wylie.split(/\s+/g);
for (let syl of syls) {
if (!syl) continue;
let prefix, vowel, suffix;
const hasVowel = /[aeiou]/.test(syl);
if (hasVowel) {
// Split prefixes and suffixes
[prefix, vowel, suffix] = syl.split(/([aeiou])/g);
} else {
vowel = "";
if (syl.length === 2) {
prefix = syl[0];
suffix = syl[1];
} else if (syl.length === 4) {
prefix = Array.from(syl).slice(0, 2).join("");
suffix = Array.from(syl).slice(2).join("");
} else if (syl.length === 3) {
if (/[gŋbm]s|[nrl]d/.test(syl.slice(1, 3))) {
prefix = syl[0];
suffix = Array.from(syl).slice(1).join("");
} else {
prefix = Array.from(syl).slice(0, 2).join("");
suffix = syl[2];
};
} else {
};
};
for (let match of (prefix.match(/[rls]\w[wyrl]/g) || [])) {
syl = syl.replace(match, String.fromCharCode(consonantTable[match[0]], consonantTable[match[1]] + 0x50, consonantTable[match[2]] + 0x50));
};
// Process subscript
for (let match of (prefix.match(/\w[wyrl]/g) || [])) {
syl = syl.replace(match, String.fromCharCode(consonantTable[match[0]], consonantTable[match[1]] + 0x50));
};
// Process superscript
for (let match of (prefix.match(/[rls]\w/g) || [])) {
syl = syl.replace(match, String.fromCharCode(consonantTable[match[0]], consonantTable[match[1]] + 0x50));
};
// Process prefix and suffix
for (let consonant in consonantTable) {
syl = syl.replace(consonant, String.fromCharCode(consonantTable[consonant]));
};
// syl = syl.batchReplace(consonantTable);
// Add vowel-initial filler
if (/^[aeiou]/.test(syl)) {
syl = "ཨ" + syl;
};
// Process vowels
/*
for (let vowel in vowelTable) {
syl = syl.replace(vowel, vowelTable[vowel]);
};
*/
syl = syl.batchReplace(vowelTable);
// Remove force non-stacking flags
syl = syl.replaceAll(/[a\.]/g, "");
output.push(syl);
};
return output.join("་");
};