Skip to content

Instantly share code, notes, and snippets.

@alahijani
Last active April 15, 2020 23:39
Show Gist options
  • Save alahijani/cd0161156ca7b5164c677ceed31ee67b to your computer and use it in GitHub Desktop.
Save alahijani/cd0161156ca7b5164c677ceed31ee67b to your computer and use it in GitHub Desktop.
Transliterate various middle eastern Abjads to Arabic
(function (window, document) {
function transliterate(s, table) {
return Array.from(s).map(function (c) {
return table.hasOwnProperty(c) ? table[c] : c
}).join('')
}
var toArabic = {
'א': 'ا',
'ב': 'ب',
'ג': 'ج',
'ד': 'د',
'ה': 'ه',
'ו': 'و',
'ז': 'ز',
'ח': 'ح',
'ט': 'ط',
'י': 'ي',
'כ': 'ك',
'ך': 'ك',
'ל': 'ل',
'מ': 'م',
'ם': 'م',
'נ': 'ن',
'ן': 'ن',
'ס': 'س',
'ע': 'ع',
'פ': 'ف',
'ף': 'ف',
'צ': 'ص',
'ץ': 'ص',
'ק': 'ق',
'ר': 'ر',
'ש': 'ش',
'ת': 'ت',
'ְ': 'ْ',
'ֱ': 'ِ',
'ֲ': 'َ',
'ֳ': 'ֳ',
'ִ': 'ٖ',
'ֵ': 'ِ',
'ֶ': 'ِ',
'ַ': 'َ',
'ָ': 'ٰ',
'ֹ': 'ُ',
'ֻ': 'ֻ',
'ּ': '',
'ֿ': 'ֿ',
'ׁ': '',
'ׂ': '',
'ܐ': 'ا',
'ܒ': 'ب',
'ܓ': 'ج',
'ܕ': 'د',
'ܗ': 'ه',
'ܘ': 'و',
'ܙ': 'ز',
'ܚ': 'ح',
'ܛ': 'ط',
'ܝ': 'ي',
'ܟ': 'ك',
'ܠ': 'ل',
'ܡ': 'م',
'ܢ': 'ن',
'ܣ': 'س',
'ܤ': 'س',
'ܥ': 'ع',
'ܦ': 'ف',
'ܨ': 'ص',
'ܩ': 'ق',
'ܪ': 'ر',
'ܫ': 'ش',
'ܬ': 'ت',
'𐭠': 'ا',
'𐭡': 'ب',
'𐭢': 'ج',
'𐭣': 'د',
'𐭤': 'ه',
'𐭥': 'ݬ',
'𐭦': 'ز',
'𐭧': 'ح',
'𐭨': 'ط',
'𐭩': 'ي',
'𐭪': 'ك',
'𐭫': 'ل',
'𐭬': 'ٯ',
'𐭭': 'ن',
'𐭮': 'س',
'𐭯': 'ف',
'𐭰': 'ص',
'𐭱': 'ش',
'𐭲': 'ت',
};
S = window.getSelection();
function t(N) {
return N.nodeType == N.TEXT_NODE;
}
function r(N) {
if (t(N)) N.data = transliterate(N.data, toArabic);
}
for (j = 0; j < S.rangeCount; ++j) {
var g = S.getRangeAt(j), e = g.startContainer, f = g.endContainer, E = g.startOffset, F = g.endOffset,
m = (e == f);
if (!m || !t(e)) {
q = document.createTreeWalker(g.commonAncestorContainer, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT, null, false);
q.currentNode = e;
for (N = q.nextNode(); N && N != f; N = q.nextNode()) r(N);
}
if (t(f)) f.splitText(F);
if (!m) r(f);
if (t(e)) {
r(k = e.splitText(E));
if (m) f = k;
e = k;
}
if (t(f)) g.setEnd(f, f.data.length);
}
})(window, document)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment