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.
134 lines (133 loc) • 6.1 kB
TypeScript
/**
* Gregorian month names in Urdu (standard literary transliterations).
* Index 0 corresponds to January (جنوری).
*/
export declare const URDU_MONTHS_GREGORIAN: readonly ["جنوری", "فروری", "مارچ", "اپریل", "مئی", "جون", "جولائی", "اگست", "ستمبر", "اکتوبر", "نومبر", "دسمبر"];
/**
* Islamic (Hijri) month names in Urdu.
* Index 0 corresponds to Muharram (محرم).
*/
export declare const URDU_MONTHS_HIJRI: readonly ["محرم", "صفر", "ربیع الاول", "ربیع الثانی", "جمادی الاول", "جمادی الثانی", "رجب", "شعبان", "رمضان المبارک", "شوال", "ذی القعدہ", "ذی الحجہ"];
/**
* Days of the week in Urdu.
* Index 0 corresponds to Sunday (اتوار).
*/
export declare const URDU_WEEKDAYS: readonly ["اتوار", "پیر", "منگل", "بدھ", "جمعرات", "جمعہ", "ہفتہ"];
export interface FormatUrduDateOptions {
/**
* Digit style to use for numeric tokens (e.g. YYYY, MM, DD, HH, mm, ss).
* @default "urdu"
*/
digits?: "urdu" | "english";
/**
* Calendar month naming scheme to use for `MMMM` and `MMM`.
* @default "gregorian"
*/
calendar?: "gregorian" | "hijri";
}
export interface TimeAgoOptions {
/**
* Digit style to use for counts (e.g. "۵ منٹ پہلے" vs "5 منٹ پہلے").
* @default "urdu"
*/
digits?: "urdu" | "english";
/**
* Whether to include the relative suffix/prefix ("پہلے" or "بعد").
* When `false`, returns only the duration string (e.g. "۵ منٹ", "ایک گھنٹہ").
* @default true
*/
addSuffix?: boolean;
}
/**
* Returns the Urdu name for a given month index (0 to 11).
*
* @param monthIndex - 0 for January / Muharram, 11 for December / Dhul Hijjah.
* @param calendar - "gregorian" or "hijri". Default is "gregorian".
*/
export declare function getUrduMonthName(monthIndex: number, calendar?: "gregorian" | "hijri"): string;
/**
* Returns the Urdu name for a day of the week (0 = Sunday, 6 = Saturday).
*/
export declare function getUrduWeekdayName(dayIndex: number): string;
/**
* Formats a Date object or timestamp into an Urdu formatted date string.
*
* Supported formatting tokens:
* - `YYYY`: Full year (e.g. "۲۰۲۶")
* - `YY`: Two-digit year (e.g. "۲۶")
* - `MMMM`: Full Urdu month name (e.g. "اگست")
* - `MMM`: Full Urdu month name (e.g. "اگست")
* - `MM`: 2-digit month with leading zero (e.g. "۰۸")
* - `M`: 1-digit month (e.g. "۸")
* - `DD`: 2-digit day of month with leading zero (e.g. "۰۵")
* - `D`: 1-digit day of month (e.g. "۵")
* - `dddd`: Full day of the week (e.g. "ہفتہ", "جمعہ")
* - `ddd`: Short day of the week (same in Urdu)
* - `HH`: 24-hour hour with leading zero (00-23)
* - `H`: 24-hour hour (0-23)
* - `hh`: 12-hour hour with leading zero (01-12)
* - `h`: 12-hour hour (1-12)
* - `mm`: Minute with leading zero (00-59)
* - `m`: Minute (0-59)
* - `ss`: Second with leading zero (00-59)
* - `s`: Second (0-59)
* - `A`: Day period indicator in Urdu ("صبح", "دوپہر", "شام", "رات")
* - `a`: Concise period indicator ("صبح", "شام")
*
* @example
* formatUrduDate(new Date(2026, 7, 22), "DD MMMM YYYY")
* // "۲۲ اگست ۲۰۲۶"
*
* formatUrduDate(new Date(2026, 7, 22, 14, 30), "dddd، D MMMM YYYY، hh:mm A")
* // "ہفتہ، ۲۲ اگست ۲۰۲۶، ۰۲:۳۰ دوپہر"
*/
export declare function formatUrduDate(date: Date | string | number, pattern?: string, options?: FormatUrduDateOptions): string;
/**
* Returns a human-friendly relative time string in Urdu.
*
* @example
* timeAgoUrdu(Date.now() - 30 * 1000) // "ابھی"
* timeAgoUrdu(Date.now() - 5 * 60 * 1000) // "۵ منٹ پہلے"
* timeAgoUrdu(Date.now() - 3 * 3600 * 1000) // "۳ گھنٹے پہلے"
* timeAgoUrdu(Date.now() - 86400 * 1000) // "کل"
* timeAgoUrdu(Date.now() + 5 * 60 * 1000) // "۵ منٹ بعد"
*/
export declare function timeAgoUrdu(date: Date | string | number, relativeTo?: Date | string | number, options?: TimeAgoOptions): string;
export interface ParseUrduDateOptions {
/**
* Year assumed when the input carries no year, e.g. `۲۲ اگست`.
* @default the current year
*/
defaultYear?: number;
}
/**
* Parse a date written with Urdu or English month names, Urdu or ASCII digits,
* into a `Date`. This is the inverse of {@link formatUrduDate}, so anything
* that function can produce parses back to the same wall-clock moment:
*
* ```ts
* const d = new Date(2026, 7, 22, 14, 30);
* const text = formatUrduDate(d, "dddd، D MMMM YYYY، hh:mm A"); // "ہفتہ، ۲۲ اگست ۲۰۲۶، ۰۲:۳۰ دوپہر"
* parseUrduDate(text)?.getHours(); // 14
* ```
*
* Accepted input:
* - `۲۲ اگست ۲۰۲۶`, `22 August 2026`, `Aug 22, 2026` (Urdu or English month name)
* - `۲۲/۰۸/۲۰۲۶` — day-first, matching Pakistani convention
* - `2026-08-22` and `2026-08-22T14:30` — ISO, `` `T` `` form, no timezone offset
* - an optional preceding weekday name (ignored) and an optional time with
* `صبح دوپہر شام رات` or `am`/`pm`, e.g. `۰۲:۳۰ دوپہر`
*
* Deliberately rejected, returning `null`: absolute instants carrying a `Z` or
* numeric offset (this function never does timezone conversion — it builds the
* wall-clock time in the runtime's own zone, exactly as `formatUrduDate`
* reads it), Hijri month names, and dates that do not exist (`۳۱ فروری`).
* A two-digit year expands as `00-69` -> `20xx` and `70-99` -> `19xx`.
*
* @example
* parseUrduDate("۲۲ اگست ۲۰۲۶") // Date(2026, 7, 22)
* parseUrduDate("۵ جنوری ۲۰۲۶، ۰۹:۱۵ صبح")?.getHours() // 9
* parseUrduDate("5 جنوری") // Date(currentYear, 0, 5)
* parseUrduDate("۳۱ فروری ۲۰۲۶") // null
*/
export declare function parseUrduDate(input: string, options?: ParseUrduDateOptions): Date | null;