Skip to content

Instantly share code, notes, and snippets.

@Mood-al
Last active October 2, 2022 23:08
Show Gist options
  • Save Mood-al/6ea025f38ddfa4f9a705783c2622505e to your computer and use it in GitHub Desktop.
Save Mood-al/6ea025f38ddfa4f9a705783c2622505e to your computer and use it in GitHub Desktop.
//code from this repo https://github.com/kavirajk/isRTL/blob/master/isRTL.js
let reRTL, rtlChars;
rtlChars = [
/* arabic ranges*/
"\u0600-\u06FF",
"\u0750-\u077F",
"\uFB50-\uFDFF",
"\uFE70-\uFEFF",
/* hebrew range*/
"\u05D0-\u05FF"
].join("");
reRTL = new RegExp("[" + rtlChars + "]", "g");
/**
* * Count the number of characters in the text that are not numbers, spaces, or punctuation.
* * Count the number of characters in the text that are in the Arabic Unicode block.
* * If the number of Arabic characters is greater than the number of non-Arabic characters, then the
* text is probably Arabic
* @param text - The text to check for RTL.
* @returns a boolean value.
*/
export const isRTL = (text) => {
var textCount = text?.replace(/[0-9\s\\\/.,\-+="']/g, "").length; // remove multilengual characters from count
var rtlCount = (text?.match(reRTL) || []).length;
return rtlCount >= textCount - rtlCount && textCount > 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment