Skip to content

Urdu and text

import { slugify, compareUrdu, isRTL, wrapBidi, toUrduDigits, toEnglishDigits, normalizeText, normalizeRomanUrdu } from 'pakdatakit/text'

Small helpers for text that mixes Urdu and English: slugs that keep Urdu letters as they are, sorting in Urdu alphabetical order, telling whether text reads right to left, isolating an English order number inside an Urdu sentence, and switching between Urdu digits (۰–۹) and English digits (0–9).

slugify() → لاہور-قلعہ-2024

Latin-only slug
2024
Reads right to left
Yes
Urdu digits
لاہور قلعہ ۲۰۲۴
English digits
لاہور قلعہ 2024
import { slugify, compareUrdu, toUrduDigits } from 'pakdatakit';
slugify('لاہور قلعہ ۲۰۲۴');
// → 'لاہور-قلعہ-2024'
slugify('لاہور Fort, Café', { script: 'latin' });
// → 'fort-cafe'
compareUrdu('بھائی', 'باغ');
// → 1
toUrduDigits('0300-1234567');
// → '۰۳۰۰-۱۲۳۴۵۶۷'
function slugify(input: string, options?: SlugifyOptions): string;
type SlugScript = 'any' | 'latin' | 'urdu';
interface SlugifyOptions {
script?: SlugScript; // default 'any'
}

Parameters. input: any text. options.script: any (the default) keeps Latin and Urdu letters, latin keeps only a-z, urdu keeps only Urdu letters. Digits are always kept, as 0-9.

Returns lower-case words joined by -, or '' when nothing is left (or the input is not a string). It never throws.

Caveats. Urdu letters are kept as they are, with no transliteration: Urdu does not write short vowels, so there is no single Roman spelling. Diacritics, tatweel and zero-width characters are dropped, the Arabic look-alikes ك ي ى become the Urdu ک ی ی, Urdu and Arabic-Indic digits become 0-9, and accented Latin-1 letters lose their accents (é → e, ß → ss). Anything else separates words.

Related: toEnglishDigits().

function compareUrdu(a: string, b: string): -1 | 0 | 1;

Parameters. Two strings. Pass the function itself to Array.sort: words.sort(compareUrdu).

Returns -1, 0 or 1. A value that is not a string is treated as ''. It never throws, and it returns 0 only for identical strings.

Caveats. Letters follow the Unicode CLDR collation for Urdu (ur, CLDR 49). A letter joined with ھ is a letter of its own, so بھ comes after every other ب word. أ ؤ ۂ ئ ۓ sort as ا و ہ ی ے and come just after them when words are otherwise equal, and diacritics only break ties. Outside the Urdu letters, spaces and punctuation come first, then digits digit by digit (Urdu, Arabic-Indic and ASCII digits are equal, and 10 comes before 2), then Urdu letters, then other Arabic-script characters, then everything else by code point, ignoring ASCII case.

The government’s National Language Promotion Department does not publish a sort order online, so CLDR is the order cited. The implementation was checked against ICU’s ur collator on 200,000 random pairs of Urdu words; see data sources.

function isRTL(text: string): boolean;

Returns true when the first letter of the text is from a right-to-left script such as Urdu or Arabic. Digits, punctuation and spaces are skipped, as in the Unicode bidirectional algorithm’s paragraph rule, so isRTL('123 سلام') is true and isRTL('Order نمبر') is false. Returns false when there is no letter or the input is not a string. It never throws.

Related: wrapBidi().

function wrapBidi(text: string, direction?: BidiDirection): string;
type BidiDirection = 'auto' | 'rtl' | 'ltr';

Parameters. text: the part to isolate, such as a name or an order number. direction: auto (the default), rtl or ltr.

Returns the text wrapped in Unicode directional isolates, so that something written in one direction does not reorder the sentence around it (for example an English order number inside an Urdu message). It uses FIRST STRONG ISOLATE (U+2068) by default, RIGHT-TO-LEFT ISOLATE (U+2067) for rtl and LEFT-TO-RIGHT ISOLATE (U+2066) for ltr, closed with POP DIRECTIONAL ISOLATE (U+2069). Any other direction is treated as auto. Returns '' when the input is not a string. It never throws.

`آرڈر ${wrapBidi('PDK-1042')} بھیج دیا گیا`;

Related: isRTL().

function toUrduDigits(text: string): string;

Returns the text with English and Arabic-Indic digits changed to Urdu digits (۰–۹). Everything else is left as it is.

function toEnglishDigits(text: string): string;

Returns the text with Urdu and Arabic-Indic digits changed to English digits (0–9).

function normalizeText(text: string): string;

Returns the text trimmed, with whitespace collapsed and Unicode NFC applied.

function normalizeRomanUrdu(text: string): string;

Returns the text with common informal Roman Urdu spellings changed to a preferred form (kia → kya, ap → aap, shukria → shukriya, nhi → nahi and a few more). It also applies normalizeText() and changes Urdu and Arabic-Indic digits to English.

Caveats. Only unambiguous variants are changed: words like men/mein (“in”) and main (“I”) are left alone, because rewriting them changes the meaning. A capitalized word keeps its capital; all-caps words (acronyms such as AP) are left unchanged.