@open-mena-mcp/arabic-tools 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Offline Hijri <-> Gregorian conversion using the Umm al-Qura calendar.
3
+ *
4
+ * The conversion is done by the JavaScript runtime itself: `Intl.DateTimeFormat` with
5
+ * `calendar: "islamic-umalqura"` carries the Umm al-Qura month table, so this module
6
+ * makes no network calls and needs no API key. Node 20+ ships with full ICU data.
7
+ *
8
+ * Umm al-Qura is tabulated for 1300-1600 AH. Outside that window ICU falls back to an
9
+ * arithmetic approximation, so this module rejects dates outside the tabulated range
10
+ * instead of returning a plausible but wrong day.
11
+ */
12
+ export type HijriDirection = "gregorian_to_hijri" | "hijri_to_gregorian";
13
+ export interface CalendarDateParts {
14
+ /** Year in that calendar. */
15
+ year: number;
16
+ /** Month number, 1-12. */
17
+ month: number;
18
+ /** Day of month. */
19
+ day: number;
20
+ /** ISO style YYYY-MM-DD in that calendar. */
21
+ iso: string;
22
+ /** Localised month name. */
23
+ monthName: string;
24
+ /** Localised weekday name. */
25
+ weekday: string;
26
+ }
27
+ export interface HijriConversionResult {
28
+ /** Which way the conversion ran. */
29
+ direction: HijriDirection;
30
+ /** Calendar used for the Hijri side. */
31
+ calendar: "islamic-umalqura";
32
+ /** True: the conversion ran locally with no network access. */
33
+ offline: true;
34
+ /** The Gregorian date. */
35
+ gregorian: CalendarDateParts;
36
+ /** The Hijri date, with pre-formatted strings in both languages. */
37
+ hijri: CalendarDateParts & {
38
+ /** Example: "٢٥ جمادى الآخرة ١٤٤٨ هـ". */
39
+ formattedAr: string;
40
+ /** Example: "25 Jumada al-Akhirah 1448 AH". */
41
+ formattedEn: string;
42
+ };
43
+ }
44
+ /** First Hijri year in the tabulated Umm al-Qura range. */
45
+ export declare const HIJRI_MIN_YEAR = 1300;
46
+ /** Last Hijri year in the tabulated Umm al-Qura range. */
47
+ export declare const HIJRI_MAX_YEAR = 1600;
48
+ /** Number of days in a Hijri month (29 or 30) according to Umm al-Qura. */
49
+ export declare function hijriMonthLength(year: number, month: number): number;
50
+ /** Convert a Hijri date to the Gregorian date that carries it. */
51
+ export declare function hijriToGregorianDate(year: number, month: number, day: number): Date;
52
+ /** Convert a Gregorian date (YYYY-MM-DD) to its Umm al-Qura Hijri date. */
53
+ export declare function gregorianToHijri(iso: string): HijriConversionResult;
54
+ /** Convert a Hijri date (YYYY-MM-DD, AH) to its Gregorian date. */
55
+ export declare function hijriToGregorian(iso: string): HijriConversionResult;
56
+ /** Convert a date in either direction. */
57
+ export declare function convertHijriGregorian(iso: string, direction: HijriDirection): HijriConversionResult;
@@ -0,0 +1,270 @@
1
+ /**
2
+ * Offline Hijri <-> Gregorian conversion using the Umm al-Qura calendar.
3
+ *
4
+ * The conversion is done by the JavaScript runtime itself: `Intl.DateTimeFormat` with
5
+ * `calendar: "islamic-umalqura"` carries the Umm al-Qura month table, so this module
6
+ * makes no network calls and needs no API key. Node 20+ ships with full ICU data.
7
+ *
8
+ * Umm al-Qura is tabulated for 1300-1600 AH. Outside that window ICU falls back to an
9
+ * arithmetic approximation, so this module rejects dates outside the tabulated range
10
+ * instead of returning a plausible but wrong day.
11
+ */
12
+ import { McpToolError } from "@open-mena-mcp/core";
13
+ import { toEasternDigits } from "./numerals.js";
14
+ /** First Hijri year in the tabulated Umm al-Qura range. */
15
+ export const HIJRI_MIN_YEAR = 1300;
16
+ /** Last Hijri year in the tabulated Umm al-Qura range. */
17
+ export const HIJRI_MAX_YEAR = 1600;
18
+ const MS_PER_DAY = 86_400_000;
19
+ const MEAN_HIJRI_YEAR_DAYS = 354.36707;
20
+ const MEAN_HIJRI_MONTH_DAYS = 29.530588;
21
+ /** Seed for the search below: 1 Muharram 1 AH (Julian), only used to land in the right decade. */
22
+ const HIJRI_EPOCH_MS = Date.UTC(622, 6, 19);
23
+ const HIJRI_MONTHS_AR = [
24
+ "محرم",
25
+ "صفر",
26
+ "ربيع الأول",
27
+ "ربيع الآخر",
28
+ "جمادى الأولى",
29
+ "جمادى الآخرة",
30
+ "رجب",
31
+ "شعبان",
32
+ "رمضان",
33
+ "شوال",
34
+ "ذو القعدة",
35
+ "ذو الحجة",
36
+ ];
37
+ const HIJRI_MONTHS_EN = [
38
+ "Muharram",
39
+ "Safar",
40
+ "Rabi' al-Awwal",
41
+ "Rabi' al-Thani",
42
+ "Jumada al-Ula",
43
+ "Jumada al-Akhirah",
44
+ "Rajab",
45
+ "Sha'ban",
46
+ "Ramadan",
47
+ "Shawwal",
48
+ "Dhu al-Qi'dah",
49
+ "Dhu al-Hijjah",
50
+ ];
51
+ const GREGORIAN_MONTHS_AR = [
52
+ "يناير",
53
+ "فبراير",
54
+ "مارس",
55
+ "أبريل",
56
+ "مايو",
57
+ "يونيو",
58
+ "يوليو",
59
+ "أغسطس",
60
+ "سبتمبر",
61
+ "أكتوبر",
62
+ "نوفمبر",
63
+ "ديسمبر",
64
+ ];
65
+ const GREGORIAN_MONTHS_EN = [
66
+ "January",
67
+ "February",
68
+ "March",
69
+ "April",
70
+ "May",
71
+ "June",
72
+ "July",
73
+ "August",
74
+ "September",
75
+ "October",
76
+ "November",
77
+ "December",
78
+ ];
79
+ const WEEKDAYS_AR = [
80
+ "الأحد",
81
+ "الاثنين",
82
+ "الثلاثاء",
83
+ "الأربعاء",
84
+ "الخميس",
85
+ "الجمعة",
86
+ "السبت",
87
+ ];
88
+ const WEEKDAYS_EN = [
89
+ "Sunday",
90
+ "Monday",
91
+ "Tuesday",
92
+ "Wednesday",
93
+ "Thursday",
94
+ "Friday",
95
+ "Saturday",
96
+ ];
97
+ const hijriFormatter = new Intl.DateTimeFormat("en-US-u-ca-islamic-umalqura-nu-latn", {
98
+ timeZone: "UTC",
99
+ year: "numeric",
100
+ month: "numeric",
101
+ day: "numeric",
102
+ });
103
+ const ISO_DATE = /^(\d{4})-(\d{2})-(\d{2})$/u;
104
+ function pad2(value) {
105
+ return value < 10 ? `0${String(value)}` : String(value);
106
+ }
107
+ function hijriPartsOf(date) {
108
+ const parts = hijriFormatter.formatToParts(date);
109
+ let year = Number.NaN;
110
+ let month = Number.NaN;
111
+ let day = Number.NaN;
112
+ for (const part of parts) {
113
+ if (part.type === "year")
114
+ year = Number.parseInt(part.value, 10);
115
+ else if (part.type === "month")
116
+ month = Number.parseInt(part.value, 10);
117
+ else if (part.type === "day")
118
+ day = Number.parseInt(part.value, 10);
119
+ }
120
+ if (!Number.isFinite(year) || !Number.isFinite(month) || !Number.isFinite(day)) {
121
+ throw new McpToolError("INTERNAL", "The runtime could not format a Hijri date");
122
+ }
123
+ return { year, month, day };
124
+ }
125
+ function hijriDayNumber(year, month, day) {
126
+ return year * MEAN_HIJRI_YEAR_DAYS + (month - 1) * MEAN_HIJRI_MONTH_DAYS + (day - 1);
127
+ }
128
+ function assertHijriRange(year) {
129
+ if (year < HIJRI_MIN_YEAR || year > HIJRI_MAX_YEAR) {
130
+ throw new McpToolError("INVALID_INPUT", `Hijri year ${String(year)} is outside the tabulated Umm al-Qura range ` +
131
+ `${String(HIJRI_MIN_YEAR)}-${String(HIJRI_MAX_YEAR)} AH`);
132
+ }
133
+ }
134
+ function parseIsoParts(iso, calendarLabel) {
135
+ const match = ISO_DATE.exec(iso.trim());
136
+ if (match === null) {
137
+ throw new McpToolError("INVALID_INPUT", `Expected a ${calendarLabel} date in YYYY-MM-DD format, received "${iso}"`);
138
+ }
139
+ const year = Number.parseInt(match[1] ?? "", 10);
140
+ const month = Number.parseInt(match[2] ?? "", 10);
141
+ const day = Number.parseInt(match[3] ?? "", 10);
142
+ if (!Number.isFinite(year) || !Number.isFinite(month) || !Number.isFinite(day)) {
143
+ throw new McpToolError("INVALID_INPUT", `"${iso}" is not a valid ${calendarLabel} date`);
144
+ }
145
+ return { year, month, day };
146
+ }
147
+ function gregorianDate(parts, iso) {
148
+ const date = new Date(Date.UTC(parts.year, parts.month - 1, parts.day));
149
+ if (date.getUTCFullYear() !== parts.year ||
150
+ date.getUTCMonth() !== parts.month - 1 ||
151
+ date.getUTCDate() !== parts.day) {
152
+ throw new McpToolError("INVALID_INPUT", `"${iso}" is not a valid Gregorian date`);
153
+ }
154
+ return date;
155
+ }
156
+ /** Number of days in a Hijri month (29 or 30) according to Umm al-Qura. */
157
+ export function hijriMonthLength(year, month) {
158
+ const first = hijriToGregorianDate(year, month, 1);
159
+ for (let offset = 1; offset <= 30; offset += 1) {
160
+ const probe = new Date(first.getTime() + offset * MS_PER_DAY);
161
+ const parts = hijriPartsOf(probe);
162
+ if (parts.month !== month || parts.year !== year)
163
+ return offset;
164
+ }
165
+ return 30;
166
+ }
167
+ /** Convert a Hijri date to the Gregorian date that carries it. */
168
+ export function hijriToGregorianDate(year, month, day) {
169
+ assertHijriRange(year);
170
+ if (month < 1 || month > 12) {
171
+ throw new McpToolError("INVALID_INPUT", `Hijri month must be between 1 and 12, got ${String(month)}`);
172
+ }
173
+ if (day < 1 || day > 30) {
174
+ throw new McpToolError("INVALID_INPUT", `Hijri day must be between 1 and 30, got ${String(day)}`);
175
+ }
176
+ const target = hijriDayNumber(year, month, day);
177
+ let probeMs = HIJRI_EPOCH_MS + Math.round(target * MS_PER_DAY);
178
+ let found = null;
179
+ for (let attempt = 0; attempt < 12; attempt += 1) {
180
+ const probe = new Date(probeMs);
181
+ const parts = hijriPartsOf(probe);
182
+ if (parts.year === year && parts.month === month && parts.day === day) {
183
+ found = probe;
184
+ break;
185
+ }
186
+ const step = Math.round(target - hijriDayNumber(parts.year, parts.month, parts.day));
187
+ if (step === 0)
188
+ break;
189
+ probeMs += step * MS_PER_DAY;
190
+ }
191
+ if (found === null) {
192
+ for (let offset = -3; offset <= 3 && found === null; offset += 1) {
193
+ const probe = new Date(probeMs + offset * MS_PER_DAY);
194
+ const parts = hijriPartsOf(probe);
195
+ if (parts.year === year && parts.month === month && parts.day === day)
196
+ found = probe;
197
+ }
198
+ }
199
+ if (found === null) {
200
+ const length = hijriMonthLength(year, month);
201
+ throw new McpToolError("INVALID_INPUT", `Hijri date ${String(year)}-${pad2(month)}-${pad2(day)} does not exist: month ${String(month)} ` +
202
+ `of ${String(year)} AH has ${String(length)} days`);
203
+ }
204
+ return found;
205
+ }
206
+ function gregorianParts(date) {
207
+ const year = date.getUTCFullYear();
208
+ const month = date.getUTCMonth() + 1;
209
+ const day = date.getUTCDate();
210
+ const weekday = date.getUTCDay();
211
+ return {
212
+ year,
213
+ month,
214
+ day,
215
+ iso: `${String(year)}-${pad2(month)}-${pad2(day)}`,
216
+ monthName: `${GREGORIAN_MONTHS_EN[month - 1] ?? ""} / ${GREGORIAN_MONTHS_AR[month - 1] ?? ""}`,
217
+ weekday: `${WEEKDAYS_EN[weekday] ?? ""} / ${WEEKDAYS_AR[weekday] ?? ""}`,
218
+ };
219
+ }
220
+ function hijriDateParts(parts) {
221
+ const monthAr = HIJRI_MONTHS_AR[parts.month - 1] ?? "";
222
+ const monthEn = HIJRI_MONTHS_EN[parts.month - 1] ?? "";
223
+ return {
224
+ year: parts.year,
225
+ month: parts.month,
226
+ day: parts.day,
227
+ iso: `${String(parts.year)}-${pad2(parts.month)}-${pad2(parts.day)}`,
228
+ monthName: `${monthEn} / ${monthAr}`,
229
+ weekday: "",
230
+ formattedAr: `${toEasternDigits(String(parts.day))} ${monthAr} ${toEasternDigits(String(parts.year))} هـ`,
231
+ formattedEn: `${String(parts.day)} ${monthEn} ${String(parts.year)} AH`,
232
+ };
233
+ }
234
+ /** Convert a Gregorian date (YYYY-MM-DD) to its Umm al-Qura Hijri date. */
235
+ export function gregorianToHijri(iso) {
236
+ const parsed = parseIsoParts(iso, "Gregorian");
237
+ const date = gregorianDate(parsed, iso);
238
+ const hijriParts = hijriPartsOf(date);
239
+ assertHijriRange(hijriParts.year);
240
+ const hijri = hijriDateParts(hijriParts);
241
+ const gregorian = gregorianParts(date);
242
+ hijri.weekday = gregorian.weekday;
243
+ return {
244
+ direction: "gregorian_to_hijri",
245
+ calendar: "islamic-umalqura",
246
+ offline: true,
247
+ gregorian,
248
+ hijri,
249
+ };
250
+ }
251
+ /** Convert a Hijri date (YYYY-MM-DD, AH) to its Gregorian date. */
252
+ export function hijriToGregorian(iso) {
253
+ const parsed = parseIsoParts(iso, "Hijri");
254
+ const date = hijriToGregorianDate(parsed.year, parsed.month, parsed.day);
255
+ const gregorian = gregorianParts(date);
256
+ const hijri = hijriDateParts(hijriPartsOf(date));
257
+ hijri.weekday = gregorian.weekday;
258
+ return {
259
+ direction: "hijri_to_gregorian",
260
+ calendar: "islamic-umalqura",
261
+ offline: true,
262
+ gregorian,
263
+ hijri,
264
+ };
265
+ }
266
+ /** Convert a date in either direction. */
267
+ export function convertHijriGregorian(iso, direction) {
268
+ return direction === "gregorian_to_hijri" ? gregorianToHijri(iso) : hijriToGregorian(iso);
269
+ }
270
+ //# sourceMappingURL=hijri.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hijri.js","sourceRoot":"","sources":["../../src/text/hijri.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEnD,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAqChD,2DAA2D;AAC3D,MAAM,CAAC,MAAM,cAAc,GAAG,IAAI,CAAC;AACnC,0DAA0D;AAC1D,MAAM,CAAC,MAAM,cAAc,GAAG,IAAI,CAAC;AAEnC,MAAM,UAAU,GAAG,UAAU,CAAC;AAC9B,MAAM,oBAAoB,GAAG,SAAS,CAAC;AACvC,MAAM,qBAAqB,GAAG,SAAS,CAAC;AACxC,kGAAkG;AAClG,MAAM,cAAc,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC;AAE5C,MAAM,eAAe,GAAG;IACtB,MAAM;IACN,KAAK;IACL,YAAY;IACZ,YAAY;IACZ,cAAc;IACd,cAAc;IACd,KAAK;IACL,OAAO;IACP,OAAO;IACP,MAAM;IACN,WAAW;IAC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BAAgB,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;IAClC,MAAM,KAAK,GAAG,cAAc,CAAC,UAAU,CAAC,CAAC;IACzC,MAAM,SAAS,GAAG,cAAc,CAAC,IAAI,CAAC,CAAC;IACvC,KAAK,CAAC,OAAO,GAAG,SAAS,CAAC,OAAO,CAAC;IAClC,OAAO;QACL,SAAS,EAAE,oBAAoB;QAC/B,QAAQ,EAAE,kBAAkB;QAC5B,OAAO,EAAE,IAAI;QACb,SAAS;QACT,KAAK;KACN,CAAC;AACJ,CAAC;AAED,mEAAmE;AACnE,MAAM,UAAU,gBAAgB,CAAC,GAAW;IAC1C,MAAM,MAAM,GAAG,aAAa,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;IAC3C,MAAM,IAAI,GAAG,oBAAoB,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC;IACzE,MAAM,SAAS,GAAG,cAAc,CAAC,IAAI,CAAC,CAAC;IACvC,MAAM,KAAK,GAAG,cAAc,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC,CAAC;IACjD,KAAK,CAAC,OAAO,GAAG,SAAS,CAAC,OAAO,CAAC;IAClC,OAAO;QACL,SAAS,EAAE,oBAAoB;QAC/B,QAAQ,EAAE,kBAAkB;QAC5B,OAAO,EAAE,IAAI;QACb,SAAS;QACT,KAAK;KACN,CAAC;AACJ,CAAC;AAED,0CAA0C;AAC1C,MAAM,UAAU,qBAAqB,CACnC,GAAW,EACX,SAAyB;IAEzB,OAAO,SAAS,KAAK,oBAAoB,CAAC,CAAC,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC;AAC5F,CAAC"}
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Arabic text normalization.
3
+ *
4
+ * Folding the letter variants that Arabic writers use interchangeably is what makes
5
+ * search, deduplication and keyword matching work on real-world Arabic text. Every
6
+ * step is optional so callers can keep the parts they need (for example a Quranic
7
+ * pipeline keeps diacritics).
8
+ */
9
+ export interface NormalizeArabicOptions {
10
+ /** Remove tashkeel (harakat, tanween, shadda, sukun) before folding letters. */
11
+ stripDiacritics: boolean;
12
+ /** Fold all alef variants (آ أ إ ٱ ٲ ٳ) to a bare alef (ا). */
13
+ unifyAlef: boolean;
14
+ /** Fold alef maqsura (ى) to yaa (ي). */
15
+ unifyYaa: boolean;
16
+ /** Fold hamza carriers: ؤ to و and ئ to ي. */
17
+ unifyHamza: boolean;
18
+ /** Fold taa marbuta (ة) to haa (ه). */
19
+ unifyTaaMarbuta: boolean;
20
+ /** Remove tatweel / kashida (U+0640). */
21
+ removeTatweel: boolean;
22
+ /** Decompose Arabic presentation forms (U+FB50..U+FDFF, U+FE70..U+FEFF) to base letters. */
23
+ decomposePresentationForms: boolean;
24
+ /** Remove C0/C1 control characters, keeping newline and tab. */
25
+ removeControlChars: boolean;
26
+ /** Collapse runs of spaces (including non-breaking and Unicode spaces) to one space. */
27
+ collapseWhitespace: boolean;
28
+ }
29
+ export interface NormalizeArabicResult {
30
+ /** The normalized text. */
31
+ text: string;
32
+ /** Number of diacritic code points removed. */
33
+ removedDiacritics: number;
34
+ /** Number of letter variants folded to their base letter. */
35
+ replacedLetters: number;
36
+ /** Number of presentation-form characters decomposed. */
37
+ replacedPresentationForms: number;
38
+ /** Number of tatweel characters removed. */
39
+ removedTatweel: number;
40
+ /** Number of control characters removed. */
41
+ removedControlChars: number;
42
+ /** Code points in the input. */
43
+ inputLength: number;
44
+ /** Code points in the output. */
45
+ outputLength: number;
46
+ }
47
+ export declare const DEFAULT_NORMALIZE_OPTIONS: NormalizeArabicOptions;
48
+ /** Normalize Arabic text: fold letter variants and clean up marks and spacing. */
49
+ export declare function normalizeArabic(input: string, options?: Partial<NormalizeArabicOptions>): NormalizeArabicResult;
@@ -0,0 +1,148 @@
1
+ /**
2
+ * Arabic text normalization.
3
+ *
4
+ * Folding the letter variants that Arabic writers use interchangeably is what makes
5
+ * search, deduplication and keyword matching work on real-world Arabic text. Every
6
+ * step is optional so callers can keep the parts they need (for example a Quranic
7
+ * pipeline keeps diacritics).
8
+ */
9
+ import { stripDiacritics } from "./diacritics.js";
10
+ export const DEFAULT_NORMALIZE_OPTIONS = {
11
+ stripDiacritics: true,
12
+ unifyAlef: true,
13
+ unifyYaa: true,
14
+ unifyHamza: true,
15
+ unifyTaaMarbuta: true,
16
+ removeTatweel: true,
17
+ decomposePresentationForms: true,
18
+ removeControlChars: true,
19
+ collapseWhitespace: true,
20
+ };
21
+ const ALEF = "\u0627";
22
+ const YAA = "\u064A";
23
+ const HAA = "\u0647";
24
+ const WAW = "\u0648";
25
+ const TATWEEL_CHAR = "\u0640";
26
+ const ALEF_VARIANTS = ["\u0622", "\u0623", "\u0625", "\u0671", "\u0672", "\u0673"];
27
+ const YAA_VARIANTS = ["\u0649"];
28
+ const HAMZA_CARRIERS = new Map([
29
+ ["\u0624", WAW],
30
+ ["\u0626", YAA],
31
+ ]);
32
+ const TAA_MARBUTA = "\u0629";
33
+ const PRESENTATION_FORM = /[\uFB50-\uFDFF\uFE70-\uFEFF]/u;
34
+ const SPACE_RUN = /[ \t\u00a0\u1680\u2000-\u200a\u202f\u205f\u3000]+/gu;
35
+ function isControlChar(codePoint) {
36
+ if (codePoint === 0x0a || codePoint === 0x09)
37
+ return false;
38
+ return codePoint < 0x20 || (codePoint >= 0x7f && codePoint <= 0x9f);
39
+ }
40
+ function buildLetterMap(options) {
41
+ const map = new Map();
42
+ if (options.unifyAlef) {
43
+ for (const variant of ALEF_VARIANTS)
44
+ map.set(variant, ALEF);
45
+ }
46
+ if (options.unifyYaa) {
47
+ for (const variant of YAA_VARIANTS)
48
+ map.set(variant, YAA);
49
+ }
50
+ if (options.unifyHamza) {
51
+ for (const [from, to] of HAMZA_CARRIERS)
52
+ map.set(from, to);
53
+ }
54
+ if (options.unifyTaaMarbuta)
55
+ map.set(TAA_MARBUTA, HAA);
56
+ return { map };
57
+ }
58
+ /** Normalize Arabic text: fold letter variants and clean up marks and spacing. */
59
+ export function normalizeArabic(input, options = {}) {
60
+ const merged = {
61
+ stripDiacritics: options.stripDiacritics ?? DEFAULT_NORMALIZE_OPTIONS.stripDiacritics,
62
+ unifyAlef: options.unifyAlef ?? DEFAULT_NORMALIZE_OPTIONS.unifyAlef,
63
+ unifyYaa: options.unifyYaa ?? DEFAULT_NORMALIZE_OPTIONS.unifyYaa,
64
+ unifyHamza: options.unifyHamza ?? DEFAULT_NORMALIZE_OPTIONS.unifyHamza,
65
+ unifyTaaMarbuta: options.unifyTaaMarbuta ?? DEFAULT_NORMALIZE_OPTIONS.unifyTaaMarbuta,
66
+ removeTatweel: options.removeTatweel ?? DEFAULT_NORMALIZE_OPTIONS.removeTatweel,
67
+ decomposePresentationForms: options.decomposePresentationForms ?? DEFAULT_NORMALIZE_OPTIONS.decomposePresentationForms,
68
+ removeControlChars: options.removeControlChars ?? DEFAULT_NORMALIZE_OPTIONS.removeControlChars,
69
+ collapseWhitespace: options.collapseWhitespace ?? DEFAULT_NORMALIZE_OPTIONS.collapseWhitespace,
70
+ };
71
+ let text = input;
72
+ let removedDiacritics = 0;
73
+ let replacedLetters = 0;
74
+ let replacedPresentationForms = 0;
75
+ let removedTatweel = 0;
76
+ let removedControlChars = 0;
77
+ const inputLength = Array.from(text).length;
78
+ if (merged.decomposePresentationForms) {
79
+ let next = "";
80
+ for (const char of text) {
81
+ if (PRESENTATION_FORM.test(char)) {
82
+ const decomposed = char.normalize("NFKC");
83
+ if (decomposed !== char)
84
+ replacedPresentationForms += 1;
85
+ next += decomposed;
86
+ }
87
+ else {
88
+ next += char;
89
+ }
90
+ }
91
+ text = next;
92
+ }
93
+ if (merged.stripDiacritics) {
94
+ const stripped = stripDiacritics(text, { removeTatweel: false });
95
+ removedDiacritics = stripped.removed;
96
+ text = stripped.text;
97
+ }
98
+ const { map } = buildLetterMap(merged);
99
+ let folded = "";
100
+ for (const char of text) {
101
+ const replacement = map.get(char);
102
+ if (replacement === undefined) {
103
+ folded += char;
104
+ }
105
+ else {
106
+ folded += replacement;
107
+ replacedLetters += 1;
108
+ }
109
+ }
110
+ text = folded;
111
+ if (merged.removeTatweel && text.includes(TATWEEL_CHAR)) {
112
+ let next = "";
113
+ for (const char of text) {
114
+ if (char === TATWEEL_CHAR) {
115
+ removedTatweel += 1;
116
+ continue;
117
+ }
118
+ next += char;
119
+ }
120
+ text = next;
121
+ }
122
+ if (merged.removeControlChars) {
123
+ let next = "";
124
+ for (const char of text) {
125
+ const codePoint = char.codePointAt(0) ?? 0;
126
+ if (isControlChar(codePoint)) {
127
+ removedControlChars += 1;
128
+ continue;
129
+ }
130
+ next += char;
131
+ }
132
+ text = next;
133
+ }
134
+ if (merged.collapseWhitespace) {
135
+ text = text.replace(SPACE_RUN, " ").trim();
136
+ }
137
+ return {
138
+ text,
139
+ removedDiacritics,
140
+ replacedLetters,
141
+ replacedPresentationForms,
142
+ removedTatweel,
143
+ removedControlChars,
144
+ inputLength,
145
+ outputLength: Array.from(text).length,
146
+ };
147
+ }
148
+ //# sourceMappingURL=normalize.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"normalize.js","sourceRoot":"","sources":["../../src/text/normalize.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AA0ClD,MAAM,CAAC,MAAM,yBAAyB,GAA2B;IAC/D,eAAe,EAAE,IAAI;IACrB,SAAS,EAAE,IAAI;IACf,QAAQ,EAAE,IAAI;IACd,UAAU,EAAE,IAAI;IAChB,eAAe,EAAE,IAAI;IACrB,aAAa,EAAE,IAAI;IACnB,0BAA0B,EAAE,IAAI;IAChC,kBAAkB,EAAE,IAAI;IACxB,kBAAkB,EAAE,IAAI;CACzB,CAAC;AAEF,MAAM,IAAI,GAAG,QAAQ,CAAC;AACtB,MAAM,GAAG,GAAG,QAAQ,CAAC;AACrB,MAAM,GAAG,GAAG,QA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AI,IAAI,IAAI,EAAE,CAAC;YACxB,IAAI,IAAI,KAAK,YAAY,EAAE,CAAC;gBAC1B,cAAc,IAAI,CAAC,CAAC;gBACpB,SAAS;YACX,CAAC;YACD,IAAI,IAAI,IAAI,CAAC;QACf,CAAC;QACD,IAAI,GAAG,IAAI,CAAC;IACd,CAAC;IAED,IAAI,MAAM,CAAC,kBAAkB,EAAE,CAAC;QAC9B,IAAI,IAAI,GAAG,EAAE,CAAC;QACd,KAAK,MAAM,IAAI,IAAI,IAAI,EAAE,CAAC;YACxB,MAAM,SAAS,GAAG,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;YAC3C,IAAI,aAAa,CAAC,SAAS,CAAC,EAAE,CAAC;gBAC7B,mBAAmB,IAAI,CAAC,CAAC;gBACzB,SAAS;YACX,CAAC;YACD,IAAI,IAAI,IAAI,CAAC;QACf,CAAC;QACD,IAAI,GAAG,IAAI,CAAC;IACd,CAAC;IAED,IAAI,MAAM,CAAC,kBAAkB,EAAE,CAAC;QAC9B,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;IAC7C,CAAC;IAED,OAAO;QACL,IAAI;QACJ,iBAAiB;QACjB,eAAe;QACf,yBAAyB;QACzB,cAAc;QACd,mBAAmB;QACnB,WAAW;QACX,YAAY,EAAE,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM;KACtC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Numeral system conversion between Western (0-9), Eastern Arabic-Indic (٠-٩)
3
+ * and Persian / Extended Arabic-Indic (۰-۹) digits.
4
+ *
5
+ * Arabic text mixes all three systems, sometimes in the same line, so `from: "auto"`
6
+ * converts whichever digits it finds without needing the caller to detect them.
7
+ */
8
+ export type NumeralSystem = "western" | "eastern" | "persian";
9
+ export interface ConvertNumeralsOptions {
10
+ /** Source digit system, or "auto" to convert digits from any system. */
11
+ from: NumeralSystem | "auto";
12
+ /** Target digit system. */
13
+ to: NumeralSystem;
14
+ /** Also convert the decimal and thousands separators (U+066B, U+066C). */
15
+ convertSeparators: boolean;
16
+ }
17
+ export interface ConvertNumeralsResult {
18
+ /** Text with digits rewritten in the target system. */
19
+ text: string;
20
+ /** Number of characters converted. */
21
+ converted: number;
22
+ /** Source system that was applied. */
23
+ from: NumeralSystem | "auto";
24
+ /** Target system that was applied. */
25
+ to: NumeralSystem;
26
+ }
27
+ /** Convert the digits of a string to another numeral system. */
28
+ export declare function convertNumerals(input: string, options?: Partial<ConvertNumeralsOptions>): ConvertNumeralsResult;
29
+ /** Convert Western digits to Eastern Arabic-Indic digits. */
30
+ export declare function toEasternDigits(input: string): string;