-
Notifications
You must be signed in to change notification settings - Fork 0
hasRTL
β οΈ Version Recommendation: Always install and use the latest version ofrtl-text-tools(npm install rtl-text-tools@latest). While this specific feature was introduced in an earlier version, the latest release includes crucial bug fixes, better edge-case handling, and improved legacy browser compatibility. Avoid pinning your project to an old version just to use this function.
Detects if a given string contains any Right-to-Left (RTL) characters. This is the primary way to check if text needs RTL processing before applying other transformations.
hasRTL(text: string): boolean| Name | Type | Description |
|---|---|---|
text |
string |
The text to check for RTL characters. |
-
boolean: Returnstrueif the text contains at least one RTL character, otherwisefalse. Returnsfalsefor empty strings,null, orundefined.
import { hasRTL } from 'rtl-text-tools';
hasRTL("Hello"); // false (English/LTR)
hasRTL("Ω
Ψ±ΨΨ¨Ψ§"); // true (Arabic/Persian)
hasRTL("Χ©ΧΧΧ"); // true (Hebrew)
hasRTL("123"); // false (Numbers only)
hasRTL(""); // false (Empty string)Use it to conditionally apply RTL fixes only when necessary, saving processing time for purely LTR content:
import { hasRTL, fixRTL } from 'rtl-text-tools';
const userInput = "Hello world!";
if (hasRTL(userInput)) {
console.log(fixRTL(userInput)); // Process only if RTL is detected
} else {
console.log("No RTL processing needed.");
}This function checks against a comprehensive set of Unicode ranges covering all major RTL scripts:
| Script | Unicode Range |
|---|---|
| Hebrew | \u0590-\u05FF |
| Arabic (Core, includes Persian/Urdu) | \u0600-\u06FF |
| Syriac | \u0700-\u074F |
| Arabic Supplement | \u0750-\u077F |
| Thaana (Maldivian) | \u0780-\u07BF |
| N'Ko | \u07C0-\u07FF |
| Samaritan | \u0800-\u083F |
| Mandaic | \u0840-\u085F |
| Arabic Extended-A | \u08A0-\u08FF |
| Hebrew Presentation Forms | \uFB1D-\uFB4F |
| Arabic Presentation Forms-A | \uFB50-\uFDFF |
| Arabic Presentation Forms-B | \uFE70-\uFEFF |
-
IE11 Compatibility: The underlying regular expression intentionally avoids the ES6
u(unicode) flag to ensure full compatibility with legacy browsers like IE11. -
BMP Only: It relies entirely on Basic Multilingual Plane (BMP) code points (
\uXXXX), which work perfectly without theuflag. -
Performance: The regex is optimized for fast execution, returning
truethe moment it finds the first matching character without scanning the entire string.
-
v0.2.0: Renamed from
containsRTL()tohasRTL()for better naming conventions. -
v0.1.0: Feature originally introduced as
containsRTL()in the initial release.
π Home β’ π Getting Started β’ π API Reference β’ π Report Issue
Made with β€οΈ by Homayoun Mohammadi
If you find this project useful, please consider β starring the repo to show your support!
- π Home
- π Getting Started
- π§© API Overview
- π οΈ Framework Guides
- π‘ Use Cases
- π₯οΈ Browser Compatibility
- β FAQ & Troubleshooting
Homayoun Mohammadi
π GitHub β’ βοΈ Email
β Love this project? Star it on GitHub to show your support!