Skip to content

Getting Started

Homayoun edited this page Jul 9, 2026 · 1 revision

Welcome to rtl-text-tools! This guide will help you install the package and get up and running with basic RTL text processing in just a few minutes.

πŸ“¦ Installation

Install the package using your preferred package manager. The library has zero runtime dependencies and works all the way back to IE11.

# Using npm
npm install rtl-text-tools

# Using yarn
yarn add rtl-text-tools

# Using pnpm
pnpm add rtl-text-tools

πŸ’‘ Basic Usage

The primary entry point is the fixRTL() function. By default, it runs a suite of transformations on digits, punctuation, and ellipsis, tailored for Persian text. For greater accuracy, you can specify an alternative RTL language (e.g., Arabic or Hebrew) to trigger locale-specific formatting rules.

import { fixRTL } from 'rtl-text-tools';

// 1. Default Usage (Persian)
const persianText = fixRTL('Ω…Ψ±Ψ­Ψ¨Ψ§, Ψ±Ω‚Ω… 123...');
console.log(persianText); 
// Output: "...Ω…Ψ±Ψ­Ψ¨Ψ§ΨŒ Ψ±Ω‚Ω… Ϋ±Ϋ²Ϋ³" 
// (Converts to Persian digits, fixes punctuation, and moves ellipsis)

// 2. Switch to Arabic
const arabicText = fixRTL('Ω…Ψ±Ψ­Ψ¨Ψ§, Ψ±Ω‚Ω… 123...', { lang: 'arabic' });
console.log(arabicText);
// Output: "...Ω…Ψ±Ψ­Ψ¨Ψ§ΨŒ Ψ±Ω‚Ω… Ω‘Ω’Ω£" 
// (Converts to Arabic-Indic digits instead)

// 3. Safe for English/LTR text
const englishText = fixRTL('Hello, world!');
console.log(englishText); 
// Output: "Hello, world!" 
// (Automatically detects no RTL characters and leaves it completely unchanged)

Note: As of v1.1.0, fixRTL is strictly precise and will only apply transformations when RTL characters are actually present in the text.

⚑ Quick Examples

Here are a few quick examples of what you can do with the individual utility functions. (For a full list of parameters and return types, see the API Reference).

Fixing Punctuation

Convert standard LTR punctuation to their proper RTL equivalents.

import { convertPunctuation } from 'rtl-text-tools';

convertPunctuation('Ω…Ψ±Ψ­Ψ¨Ψ§, ΩƒΩŠΩ Ψ­Ψ§Ω„Ωƒ?'); 
// Output: 'Ω…Ψ±Ψ­Ψ¨Ψ§ΨŒ ΩƒΩŠΩ Ψ­Ψ§Ω„ΩƒΨŸ'

Handling Mixed RTL/LTR Text

Fix readability issues when English/URLs are mixed inside an RTL paragraph.

import { normalizeDirection } from 'rtl-text-tools';

const mixedText = "Ω…Ω† Ψ―Ψ± پارکی Ψ±Ψ§Ω‡ Ω…ΫŒ رفΨͺΩ… Do not Park here";
console.log(normalizeDirection(mixedText));
// The text will now render properly with an RTL base direction

Quick React Integration

Use the built-in style helpers to easily apply RTL direction in your React components.

import { fixRTL, getRTLStyles } from 'rtl-text-tools';

function PriceTag({ price }) {
  return (
    <span style={getRTLStyles()}>
      {fixRTL(`Ω‚ΫŒΩ…Ψͺ: ${price}`)}
    </span>
  );
}

πŸš€ rtl-text-tools

npm downloads

πŸ‘¨β€πŸ’» Created by

Homayoun Mohammadi
πŸ”— GitHub β€’ βœ‰οΈ Email

⭐ Love this project? Star it on GitHub to show your support!

Clone this wiki locally