urdu-text-utils
Version:
Comprehensive Urdu NLP & text processing toolkit: normalization, Roman Urdu transliteration, stop words, script detection, digits, diacritics (aerab), sorting, search, and statistics. Zero dependencies, ESM + CJS.
170 lines (169 loc) • 7.74 kB
TypeScript
declare const SETS: {
readonly urdu: "۰۱۲۳۴۵۶۷۸۹";
readonly english: "0123456789";
readonly arabic: "٠١٢٣٤٥٦٧٨٩";
};
export type DigitStyle = keyof typeof SETS;
/** `12345` -> `۱۲۳۴۵`. Accepts Arabic-Indic digits as input too. */
export declare function toUrduDigits(input: string): string;
/** `۱۲۳۴۵` -> `12345`. Safe to feed into `Number()` afterwards. */
export declare function toEnglishDigits(input: string): string;
/** `۱۲۳۴۵` -> `١٢٣٤٥` (Arabic-Indic, U+0660 block — a different block from Urdu's). */
export declare function toArabicIndicDigits(input: string): string;
/**
* Rewrite every digit in `input` to one style.
*
* @example
* convertNumbers("12345") // "۱۲۳۴۵"
* convertNumbers("۱۲۳۴۵", "english") // "12345"
*/
export declare function convertNumbers(input: string, to?: DigitStyle): string;
/**
* Parse a number written with Urdu or Arabic-Indic digits.
* Handles the Urdu decimal separator ٫ and thousands separator ٬.
* Returns `NaN` when the string is not a number.
*/
export declare function parseUrduNumber(input: string): number;
/** Cardinal words for 0-9. Index 0 is empty: `0` is spelled `صفر` separately. */
export declare const URDU_ONES: readonly string[];
/** Words for 10-20 plus every multiple of ten through 100. */
export declare const URDU_TEENS_AND_TENS: Readonly<Record<number, string>>;
/**
* The 21-99 values that are irregular: Urdu does not compose them as
* "twenty-one", each unit has its own word. Every non-multiple of ten from 21
* to 99 appears here exactly once.
*
* A few of these spellings have documented variants (`26` also appears as
* `چھببیس`; `92` as `بیانوے`). This table follows the standard textbook forms.
*/
export declare const URDU_TENS_UNITS: Readonly<Record<number, string>>;
/**
* The South Asian scale, largest first. `کھرب` is 10^11 and `ارب` is 10^9 —
* note they are not the western billion/trillion, and each step below `کروڑ`
* is two orders of magnitude, not three.
*/
export declare const URDU_SCALES: ReadonlyArray<readonly [number, string]>;
/**
* Ordinals 1-6 are irregular; every larger ordinal is the last word of the
* cardinal with `واں` (masculine) or `ویں` (feminine) attached.
*/
export declare const URDU_ORDINAL_IRREGULARS: Readonly<Record<number, {
masculine: string;
feminine: string;
}>>;
/**
* Spell a whole number in Urdu words, using the South Asian scale
* (ہزار, لاکھ, کروڑ, ارب, کھرب) rather than the western million/billion scale.
*
* Every cardinal from 0 to 99 is a real irregular word — 21 is `اکیس`, not a
* composed `بیس ایک` — and the scale words are exact up to 10^15, beyond which
* JavaScript's own floating-point representation loses precision.
*
* The irregular 21-99 table uses one canonical textbook spelling for
* deterministic output. Some words have recognised variant spellings.
*
* @example
* numberToUrduWords(1234567) // "بارہ لاکھ چونتیس ہزار پانچ سو سڑسٹھ"
* numberToUrduWords(1e9) // "ایک ارب"
*/
export declare function numberToUrduWords(value: number): string;
export interface FormatUrduNumberOptions {
/**
* Digit block to emit. The separators follow it: Urdu digits pair with ٬/٫,
* English digits with `,`/`.`.
* @default "urdu"
*/
digits?: "urdu" | "english";
/**
* `"southAsian"` groups as lakh/crore (12٬34٬567), `"western"` as thousands
* (1٬234٬567), `"none"` emits no separators.
* @default "southAsian"
*/
grouping?: "southAsian" | "western" | "none";
/**
* Fraction digits, rounded half-up. Trailing zeros are dropped, so a whole
* number never grows a decimal separator.
*
* Rounding is applied to the binary double, not to its decimal spelling, and
* both language ports do the arithmetic identically. That keeps output
* byte-identical between them, and it means values whose decimal form is not
* exactly representable (2.675, 1.005) can round down rather than up. Asking
* for many digits exposes the representation itself: 1234567.891 at
* `decimals: 15` ends in the double's real binary noise. Keep this at 6 or
* below for display; `decimalPlaces`-style rounding of exact decimals is out
* of scope.
* @default 2
*/
decimals?: number;
}
/**
* Format a number the way Urdu software prints numbers, with South Asian
* digit grouping and Urdu digits by default.
*
* Returns `""` for `NaN` and infinities, matching `formatUrduDate`.
*
* A very large magnitude prints the shortest decimal that represents the
* double, so beyond 2^53 the digits are the value the number really holds.
*
* @example
* formatUrduNumber(1234567) // "۱۲٬۳۴٬۵۶۷"
* formatUrduNumber(1234567, { grouping: "western" }) // "۱٬۲۳۴٬۵۶۷"
* formatUrduNumber(1234, { digits: "english" }) // "1,234"
* formatUrduNumber(-3.14159, { decimals: 3 }) // "-۳٫۱۴۲"
*/
export declare function formatUrduNumber(value: number, options?: FormatUrduNumberOptions): string;
export interface FormatUrduCurrencyOptions extends FormatUrduNumberOptions {
/**
* `"digits"` prints the amount as a formatted number, `"words"` spells it out.
* @default "digits"
*/
style?: "digits" | "words";
/** Unit used for every amount except exactly one. @default "روپے" */
unit?: string;
/** Unit used for an amount of exactly one. @default "روپیہ" */
singularUnit?: string;
}
/**
* Format a money amount with the right Urdu unit for the quantity.
*
* The `"words"` style supports whole amounts only; fractional values throw
* instead of being split into whole units and paisa.
*
* @example
* formatUrduCurrency(1500) // "۱٬۵۰۰ روپے"
* formatUrduCurrency(1) // "۱ روپیہ"
* formatUrduCurrency(1500, { style: "words" }) // "ایک ہزار پانچ سو روپے"
* formatUrduCurrency(2.5, { unit: "ڈالر", singularUnit: "ڈالر" }) // "۲٫۵ ڈالر"
*/
export declare function formatUrduCurrency(value: number, options?: FormatUrduCurrencyOptions): string;
export interface UrduOrdinalOptions {
/**
* Urdu ordinals inflect for gender: `پہلا` (masculine) vs `پہلی` (feminine).
* @default "masculine"
*/
gender?: "masculine" | "feminine";
}
/**
* Spell an ordinal in Urdu — `پہلا`, `دوسرا`, `اکیسواں`, `سواں`.
*
* 1-6 are irregular and come from a table; everything else is the last word of
* the cardinal with the ordinal suffix, so `اکیس` becomes `اکیسواں` and `نوے`
* becomes `نوےواں`. A word-final `و` contracts instead of doubling, which is
* why `نو` becomes `نواں` (not `نوواں`) and `سو` becomes `سواں`.
*
* `0` returns `صفر`, since "zeroth" has no Urdu ordinal form. Returns `""` for
* non-finite input and throws for fractions, like {@link numberToUrduWords}.
*
* These are standalone forms; the function does not infer agreement from a
* following noun or sentence context.
*
* @example
* toUrduOrdinal(1) // "پہلا"
* toUrduOrdinal(1, { gender: "feminine" }) // "پہلی"
* toUrduOrdinal(9) // "نواں"
* toUrduOrdinal(21) // "اکیسواں"
* toUrduOrdinal(100) // "سواں"
* toUrduOrdinal(1100, { gender: "feminine" }) // "ایک ہزار ایک سویں"
*/
export declare function toUrduOrdinal(value: number, options?: UrduOrdinalOptions): string;
export {};