-
Notifications
You must be signed in to change notification settings - Fork 0
toPersianDigits
β οΈ 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.
Converts standard Latin digits (0-9) into Extended Persian numerals (Ϋ°-ΫΉ). This is the standard digit format used in Persian (Farsi), Urdu, Dari, and Pashto locales.
(Note: If you need Arabic/Egyptian digits instead, use toArabicDigits(). If you are using the main fixRTL() function, Persian digits are applied automatically by default.)
toPersianDigits(text: string): string| Name | Type | Description |
|---|---|---|
text |
string |
The text containing Latin digits to convert. |
-
string: The text with all Latin digits replaced by Persian-Indic numerals. Returns the original string unchanged if it is empty,null, or contains no digits.
import { toPersianDigits } from 'rtl-text-tools';
toPersianDigits("Price 123"); // "Price Ϋ±Ϋ²Ϋ³"
toPersianDigits("Year 2024"); // "Year Ϋ²Ϋ°Ϋ²Ϋ΄"
toPersianDigits("Hello world"); // "Hello world" (No digits to convert)
toPersianDigits(""); // "" (Empty string)It safely ignores non-digit characters and only transforms the numbers:
import { toPersianDigits } from 'rtl-text-tools';
toPersianDigits("Total: $50.00");
// Output: "Total: $Ϋ΅Ϋ°.Ϋ°Ϋ°"-
IE11 Compatibility: The function uses the standard
String.prototype.replace()method with a regular expression and a callback function. It intentionally avoids modern methods likeString.prototype.replaceAll()to ensure full compatibility with legacy browsers like IE11. -
Performance: It uses a direct character lookup map (
PERSIAN_DIGITS) inside the replace callback, making the conversion extremely fast even for large strings. -
Safe Execution: If the input is falsy (e.g.,
null,undefined, or""), it immediately returns the input without throwing an error.
-
v0.2.0: Added
toPersianDigits()to convert Latin numbers to Persian numerals (Ϋ°-ΫΉ).
π 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!