@kirnu/arabic-core 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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Kirnu (getkirnu.com)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,193 @@
1
- # Temporary Holding Version
1
+ # @kirnu/arabic-core
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Arabic text, number-to-words (tafgeet) and Hijri date utilities — the engine behind [Kirnu](https://getkirnu.com).
4
+ Plain TypeScript: no React, no DOM, no network, no dependencies. Runs in browsers, Node and workers.
5
+
6
+ > **بالعربية:** مكتبة مفتوحة المصدر لمعالجة العربية: تفقيط الأرقام والمبالغ بقواعد النحو الصحيحة (العدد والمعدود، المثنى، التمييز)، وإزالة التشكيل والتطويل، وتطبيع النص، وتحويل الأرقام العربية والإنجليزية، والتحويل بين التاريخ الهجري (أم القرى) والميلادي. هي المحرّك نفسه الذي تعمل به أدوات [كرنو](https://getkirnu.com/ar/).
7
+
8
+ ```bash
9
+ npm install @kirnu/arabic-core
10
+ ```
11
+
12
+ ```ts
13
+ import { tafgeet, currencyToWords, removeTashkeel, normalizeArabic, gregorianToHijri } from '@kirnu/arabic-core';
14
+
15
+ tafgeet(1001011); // مليون وألف وأحد عشر
16
+ currencyToWords('1250.50', 'SAR', { cheque: true });
17
+ // فقط ألف ومئتان وخمسون ريالاً سعودياً وخمسون هللة لا غير
18
+ removeTashkeel('مُحَمَّدٌ'); // محمد
19
+ normalizeArabic('أحمد علی'); // احمد علي
20
+ gregorianToHijri({ year: 2024, month: 3, day: 11 }); // { year: 1445, month: 9, day: 1 }
21
+ ```
22
+
23
+ ### Try it online
24
+
25
+ Every function is live on getkirnu.com, free and in the browser:
26
+
27
+ | Function | Tool |
28
+ |---|---|
29
+ | `tafgeet` | [تفقيط الأرقام](https://getkirnu.com/ar/text/tafgeet/) |
30
+ | `currencyToWords` | [تحويل المبالغ إلى كلمات](https://getkirnu.com/ar/text/currency-to-words/) |
31
+ | `removeTashkeel` | [إزالة التشكيل](https://getkirnu.com/ar/text/remove-tashkeel/) |
32
+ | `normalizeArabic` | [تطبيع النص العربي](https://getkirnu.com/ar/text/arabic-normalizer/) |
33
+ | `cleanText` / `fixRtl` | [تنظيف النص العربي](https://getkirnu.com/ar/text/clean-text/) · [إصلاح اتجاه النص](https://getkirnu.com/ar/text/rtl-fixer/) |
34
+ | `hijriToGregorian` / `gregorianToHijri` | [هجري إلى ميلادي](https://getkirnu.com/ar/dates/hijri-to-gregorian/) · [ميلادي إلى هجري](https://getkirnu.com/ar/dates/gregorian-to-hijri/) |
35
+ | `ageGregorian` / `ageHijri` | [حساب العمر](https://getkirnu.com/ar/dates/age-calculator/) |
36
+ | `countWorkingDays` | [حساب أيام العمل](https://getkirnu.com/ar/dates/working-days/) |
37
+
38
+ ## Scripts
39
+
40
+ These run inside the Kirnu monorepo.
41
+
42
+ | Command | What it does |
43
+ |---|---|
44
+ | `npm test` | Vitest unit tests |
45
+ | `npm run build` | ESM + CJS + `.d.ts` into `dist/` (tsup) |
46
+ | `npm run review` | Writes `EDITOR-REVIEW.md` — every tafgeet/currency fixture with live output, for the Arabic editor |
47
+ | `npm run generate:ummalqura` | Regenerates the Umm al-Qura table from ICU (only needed to change the range) |
48
+
49
+ ---
50
+
51
+ ## Numbers
52
+
53
+ ### `tafgeet(input, options?)`
54
+
55
+ Number → Arabic words (Modern Standard Arabic), **0 to 999,999,999,999**.
56
+
57
+ - `input`: `number | bigint | string`. Strings may use Western, Arabic-Indic (٠–٩) or Persian (۰–۹) digits;
58
+ `,` `٬` and spaces are thousands separators, `.` or `٫` is the decimal point. Use strings for exact values.
59
+ - `options.gender`: `'masculine'` (default) | `'feminine'` — gender of the **counted noun**. Numbers 3–10 take the opposite form (ثلاثة كتب / ثلاث هللات).
60
+ - `options.case`: `'nominative'` (default) | `'accusative'` | `'genitive'` — changes duals, 12 and the tens (اثنان/اثنين، عشرون/عشرين، مئتان/مئتين).
61
+ - `options.cheque`: wraps the result in «فقط … لا غير».
62
+ - `options.spelling`: `'modern'` (مئة, default) | `'classic'` (مائة).
63
+ - Fractions are read after «فاصلة» as a whole number; leading zeros are read as «صفر» (3.05 → ثلاثة فاصلة صفر خمسة); trailing zeros are dropped.
64
+ - Negative numbers are prefixed with «سالب».
65
+ - Throws `SyntaxError` for non-numeric input and `RangeError` outside the supported range.
66
+
67
+ #### Grammar rules applied
68
+
69
+ Every number + noun phrase (including ألف / مليون / مليار and currency names) follows one rule set:
70
+
71
+ | Count | Noun form | Example |
72
+ |---|---|---|
73
+ | 1 | the noun alone | ألف · ريال سعودي واحد |
74
+ | 2 | dual | ألفان · ريالان سعوديان |
75
+ | 3–10 | number in the opposite gender + genitive plural | ثلاثة آلاف · ثلاث هللات |
76
+ | 11–99 | number + accusative singular (tamyiz) | أحد عشر ألفاً · خمسون هللة |
77
+ | whole hundreds (last two digits 00) | number + genitive singular | مئة ألف · ثلاثة آلاف ريال |
78
+
79
+ - For counts above 100, the noun follows the **last** part (strict MSA): 102,000 → «مئة وألفان».
80
+ - A dual directly followed by its noun takes the construct form: «مئتا ألف», «ألفا ريال».
81
+ - Thousands, millions and billions (مليار) are masculine nouns, so their counts always use the masculine-noun form.
82
+ - All expected outputs are tracked in `test/fixtures/tafgeet.cases.ts` with status `TODO-VERIFY` until a native Arabic editor approves them (see `EDITOR-REVIEW.md`).
83
+
84
+ ### `currencyToWords(amount, currency, options?)`
85
+
86
+ | Code | Unit | Sub-unit | Sub-units per unit |
87
+ |---|---|---|---|
88
+ | SAR | ريال سعودي | هللة (feminine) | 100 |
89
+ | AED | درهم إماراتي | فلس | 100 |
90
+ | EGP | جنيه مصري | قرش | 100 |
91
+ | QAR | ريال قطري | درهم | 100 |
92
+ | KWD | دينار كويتي | فلس | 1000 |
93
+ | BHD | دينار بحريني | فلس | 1000 |
94
+ | OMR | ريال عماني | بيسة (feminine) | 1000 |
95
+
96
+ - Amounts are split into whole and minor units **without floating point**; extra fraction digits are rounded half-up (19.995 SAR → 20 SAR).
97
+ - A zero main amount is omitted (0.50 SAR → «خمسون هللة»); zero overall → «صفر ريال سعودي».
98
+ - Adjectives agree: «ريالاً سعودياً» (11–99), «ريالان سعوديان» (2), «ريالات سعودية» (3–10, non-human plural → feminine singular).
99
+ - Options: `cheque`, `case`, `spelling` as for `tafgeet`. Negative amounts throw `RangeError`.
100
+ - `CURRENCIES` exports the metadata (Arabic names, decimals) for building UIs.
101
+
102
+ ### `parseNumberInput(input)` / `toMinorUnits(parsed, decimals)`
103
+
104
+ These are the low-level parsers used above, exported so UIs can validate input the same way the engine does.
105
+
106
+ ---
107
+
108
+ ## Text
109
+
110
+ ### `removeTashkeel(text, options?)`
111
+
112
+ 1. The text is first normalised with `text.normalize('NFC')`, so a decomposed hamza (ا + U+0654) becomes أ and is **kept**.
113
+ 2. **Always removed:** U+064B–U+0652 (tanween, fatha, damma, kasra, shadda, sukun), U+0653–U+065F (maddah, small hamza and other marks), U+0670 (superscript alef).
114
+ 3. `honorifics` (default **true**): removes U+0610–U+061A.
115
+ 4. `quranicMarks` (default **true**): removes an **explicit list** of Quranic annotation marks (U+06D6–U+06DC, U+06DF–U+06E4, U+06E7, U+06E8, U+06EA–U+06ED; see `src/text/unicode.ts`). Marks not on the list are kept.
116
+ 5. `tatweel` (default **false**): removes ـ U+0640.
117
+
118
+ **Never removed:** U+06DD (end of ayah), U+06DE (rub el hizb), U+06E5 (small waw), U+06E6 (small yeh). U+06E9 (place of sajdah) is also kept because it isn't on the list.
119
+
120
+ ### `normalizeArabic(text, options?)`
121
+
122
+ The input is NFC-normalised first. Each rule has its own toggle, and they run in this order:
123
+
124
+ | Option | Default | Rule |
125
+ |---|---|---|
126
+ | `tatweel` | on | Remove ـ (U+0640). |
127
+ | `persian` | on | Persian keheh ک (U+06A9) → ك (U+0643); Farsi yeh ی (U+06CC) → ي (U+064A). |
128
+ | `alef` | on | أ إ آ ٱ ٲ ٳ → ا. Does **not** change ؤ, ئ or standalone ء. |
129
+ | `yeh` | `'toYeh'` | `'toYeh'`: every ى → ي. `'toAlefMaksura'`: word-final ي → ى (Egyptian spelling). `'none'`: unchanged. |
130
+ | `tehMarbuta` | off | ة → ه (lossy, so off by default). |
131
+ | `whitespace` | on | Runs of spaces/tabs/NBSP → one space; spaces trimmed at line edges; 3+ line breaks → one blank line; whole text trimmed. |
132
+
133
+ `normalizeArabic` doesn't remove tashkeel; use `removeTashkeel` for that.
134
+
135
+ ### `convertNumerals(text, direction, options?)`
136
+
137
+ - `'toWestern'`: ٠–٩ and ۰–۹ → 0–9.
138
+ - `'toArabicIndic'`: 0–9 and ۰–۹ → ٠–٩.
139
+ - `separators` (default on): `.` ↔ ٫ and `,` ↔ ٬, **only between digits**. Sentence punctuation is untouched.
140
+
141
+ ### Cleanup: `removeTatweel`, `cleanText`, `formatText`, `fixRtl`
142
+
143
+ - `removeTatweel(text)`: removes ـ (U+0640) only; `countTatweel(text)` counts them.
144
+ - `cleanText(text, options?)`: `whitespace` (default on) collapses runs of spaces; `lineBreaks` (on) trims lines and limits blank lines; `invisible` (off) removes zero-width spaces, BOM, soft hyphens, word joiners and bidi marks but **keeps ZWNJ/ZWJ**; `tatweel` and `tashkeel` (off) remove those.
145
+ - `formatText(text, options?)`: `trimLines`, `collapseSpaces`, `removeEmptyLines`, and `join`: `'none'` | `'all'` (one line) | `'paragraphs'` (join lines within paragraphs).
146
+ - `fixRtl(text, options?)`: makes mixed Arabic/English text display correctly in RTL contexts by adding an RLM at the start of each line and isolating Latin/number runs in LRI…PDI. The visible text is unchanged. `stripBidiControls` removes such marks; `countBidiControls` counts them.
147
+
148
+ ### `countWords(text)` / `countChars(text)`
149
+
150
+ - A word is a run of letters, combining marks, digits or tatweel. Tashkeel and tatweel never split a word. Numbers like 1,250.50 count as one word. Arabic punctuation (، ؛ ؟) separates words.
151
+ - `countChars` counts Unicode code points after NFC, with CRLF counted as one line break. It returns `characters`, `charactersNoSpaces`, `charactersNoDiacritics`, `letters` (tatweel excluded), `diacritics`, `digits`, `spaces`, `words`, `lines` and `paragraphs`.
152
+
153
+ ---
154
+
155
+ ## Dates
156
+
157
+ All dates are `{ year, month, day }` objects, with `month` 1-based. There are no `Date` objects and no time zones, so results never shift by a day.
158
+
159
+ ### `hijriToGregorian(date)` / `gregorianToHijri(date)`
160
+
161
+ - These use the **Umm al-Qura** calendar from a bundled month-length table covering **1343–1500 AH (2 Aug 1924 – 16 Nov 2077)**.
162
+ - The table is generated from ICU's Umm al-Qura data (`npm run generate:ummalqura`). Tests compare every day in the range against `Intl`'s `islamic-umalqura` calendar in both directions.
163
+ - Out-of-range or invalid dates throw `HijriRangeError`, which is a `RangeError`.
164
+ - Umm al-Qura is the official calendar of Saudi Arabia. Religious dates announced after moon sighting can differ by a day.
165
+
166
+ Helpers: `hijriMonthLength`, `isValidHijriDate`, `weekday` (0 = Sunday), `HIJRI_RANGE`, `HIJRI_MONTHS`, `GREGORIAN_MONTHS`, `SYRIAC_MONTHS`, `WEEKDAYS`.
167
+
168
+ ### `dateDifference(from, to)` / `hijriDateDifference(from, to)`
169
+
170
+ These return `{ sign, years, months, days, totalDays, totalWeeks, remainderDays, totalMonths }`.
171
+
172
+ - The order of the arguments doesn't matter; `sign` gives the direction.
173
+ - Months are added with the day clamped to the target month's length, so 31 Jan → 28 Feb counts as one whole month.
174
+
175
+ ### `ageGregorian(birth, on)` / `ageHijri(birth, on)`
176
+
177
+ These return the age in years, months and days, plus the totals, `nextBirthday` and `daysUntilNextBirthday`.
178
+
179
+ - A 29 February birthday falls on 28 February in common years.
180
+ - A Hijri 30th falls on the 29th in 29-day months.
181
+
182
+ ### `addDays(date, days)` / `daysBetween(from, to)` / `countWorkingDays(from, to, weekend, inclusive?)`
183
+
184
+ - `addDays` accepts negative numbers; it throws `RangeError` for non-integer days or results outside years 1–9999.
185
+ - `countWorkingDays` takes the weekend as weekday numbers (0 = Sunday … 6 = Saturday), e.g. `[5, 6]` for Friday–Saturday, and returns `{ workingDays, weekendDays, totalDays }`. Public holidays are not included (they vary by country and year).
186
+
187
+ ---
188
+
189
+ ## Data and license
190
+
191
+ - The Umm al-Qura month-length table is generated from the calendar data in [ICU](https://icu.unicode.org/) (the Unicode library built into browsers and Node), used under the [Unicode License](https://www.unicode.org/license.txt).
192
+ - Arabic grammar output is tracked case by case and under review by a native Arabic editor; corrections are welcome as issues.
193
+ - Code: [MIT](./LICENSE) © Kirnu ([getkirnu.com](https://getkirnu.com)).