arabic-number-formatter 1.0.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 +21 -0
- package/README.md +40 -0
- package/arabic-number-formatter-1.0.0.tgz +0 -0
- package/package.json +21 -0
- package/src/ArabicNumberFormatter.js +236 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alexander
|
|
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
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# Arabic Number Formatter
|
|
2
|
+
|
|
3
|
+
Convert numbers to Arabic words with full support for:
|
|
4
|
+
|
|
5
|
+
- Gender: masculine / feminine
|
|
6
|
+
- Case: رفع / نصب
|
|
7
|
+
- Definite: ال
|
|
8
|
+
- Currency (main / sub / special sub-unit)
|
|
9
|
+
- Up to billions
|
|
10
|
+
|
|
11
|
+
## Example
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import { createArabicNumberFormatter } from "./src/ArabicNumberFormatter";
|
|
15
|
+
|
|
16
|
+
const formatter = createArabicNumberFormatter({
|
|
17
|
+
gender: "feminine",
|
|
18
|
+
case: "nasb",
|
|
19
|
+
});
|
|
20
|
+
console.log(formatter.format(23));
|
|
21
|
+
// ثلاث و عشرين
|
|
22
|
+
|
|
23
|
+
const dinarFormatter = createArabicNumberFormatter({
|
|
24
|
+
currency: {
|
|
25
|
+
main: "دينار اردني",
|
|
26
|
+
sub: "قرش",
|
|
27
|
+
subUnder10: "قروش",
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
dinarFormatter.format(1234.05);
|
|
32
|
+
// الف و مائتان و أربعة و ثلاثون دينار اردني و خمسة قروش
|
|
33
|
+
|
|
34
|
+
dinarFormatter.format(1234.75);
|
|
35
|
+
// الف و مائتان و أربعة و ثلاثون دينار اردني و خمسة و سبعون قرش
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
For ordinal numbers, the current support is up to 19.
|
|
39
|
+
|
|
40
|
+
<!-- Defaults: Masculine/ Definite:false , Case:rafe3-->
|
|
Binary file
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "arabic-number-formatter",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Convert numbers to Arabic words with gender, case, definite, and currency support.",
|
|
5
|
+
"main": "src/ArabicNumberFormatter.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "node test.js"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"arabic",
|
|
12
|
+
"numbers",
|
|
13
|
+
"formatter",
|
|
14
|
+
"currency",
|
|
15
|
+
"words",
|
|
16
|
+
"masculine",
|
|
17
|
+
"feminine"
|
|
18
|
+
],
|
|
19
|
+
"author": "Alexander",
|
|
20
|
+
"license": "MIT"
|
|
21
|
+
}
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
/* ===================================================== */
|
|
2
|
+
/* ARABIC NUMBER FORMATTER ENGINE */
|
|
3
|
+
/* ===================================================== */
|
|
4
|
+
|
|
5
|
+
const dictionaries = {
|
|
6
|
+
masculine: [
|
|
7
|
+
"",
|
|
8
|
+
"واحد",
|
|
9
|
+
"اثنان",
|
|
10
|
+
"ثلاثة",
|
|
11
|
+
"أربعة",
|
|
12
|
+
"خمسة",
|
|
13
|
+
"ستة",
|
|
14
|
+
"سبعة",
|
|
15
|
+
"ثمانية",
|
|
16
|
+
"تسعة",
|
|
17
|
+
],
|
|
18
|
+
feminine: [
|
|
19
|
+
"",
|
|
20
|
+
"واحدة",
|
|
21
|
+
"اثنتان",
|
|
22
|
+
"ثلاث",
|
|
23
|
+
"أربع",
|
|
24
|
+
"خمس",
|
|
25
|
+
"ست",
|
|
26
|
+
"سبع",
|
|
27
|
+
"ثمان",
|
|
28
|
+
"تسع",
|
|
29
|
+
],
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const ordinals = {
|
|
33
|
+
masculine: [
|
|
34
|
+
"",
|
|
35
|
+
"الأول",
|
|
36
|
+
"الثاني",
|
|
37
|
+
"الثالث",
|
|
38
|
+
"الرابع",
|
|
39
|
+
"الخامس",
|
|
40
|
+
"السادس",
|
|
41
|
+
"السابع",
|
|
42
|
+
"الثامن",
|
|
43
|
+
"التاسع",
|
|
44
|
+
"العاشر",
|
|
45
|
+
],
|
|
46
|
+
feminine: [
|
|
47
|
+
"",
|
|
48
|
+
"الأولى",
|
|
49
|
+
"الثانية",
|
|
50
|
+
"الثالثة",
|
|
51
|
+
"الرابعة",
|
|
52
|
+
"الخامسة",
|
|
53
|
+
"السادسة",
|
|
54
|
+
"السابعة",
|
|
55
|
+
"الثامنة",
|
|
56
|
+
"التاسعة",
|
|
57
|
+
"العاشرة",
|
|
58
|
+
],
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const tens = [
|
|
62
|
+
"",
|
|
63
|
+
"عشر",
|
|
64
|
+
"عشرون",
|
|
65
|
+
"ثلاثون",
|
|
66
|
+
"أربعون",
|
|
67
|
+
"خمسون",
|
|
68
|
+
"ستون",
|
|
69
|
+
"سبعون",
|
|
70
|
+
"ثمانون",
|
|
71
|
+
"تسعون",
|
|
72
|
+
];
|
|
73
|
+
|
|
74
|
+
const hundreds = [
|
|
75
|
+
"",
|
|
76
|
+
"مائة",
|
|
77
|
+
"مائتان",
|
|
78
|
+
"ثلاثمائة",
|
|
79
|
+
"أربعمائة",
|
|
80
|
+
"خمسمائة",
|
|
81
|
+
"ستمائة",
|
|
82
|
+
"سبعمائة",
|
|
83
|
+
"ثمانمائة",
|
|
84
|
+
"تسعمائة",
|
|
85
|
+
];
|
|
86
|
+
|
|
87
|
+
const sectors = [
|
|
88
|
+
"", // units
|
|
89
|
+
{ singular: "الف", dual: "الفان", plural: "الاف" },
|
|
90
|
+
{ singular: "مليون", dual: "مليونان", plural: "ملايين" },
|
|
91
|
+
{ singular: "مليار", dual: "ملياران", plural: "مليارات" },
|
|
92
|
+
];
|
|
93
|
+
|
|
94
|
+
/* ===================================================== */
|
|
95
|
+
/* FACTORY FUNCTION */
|
|
96
|
+
/* ===================================================== */
|
|
97
|
+
|
|
98
|
+
export const createArabicNumberFormatter = (defaultOptions = {}) => {
|
|
99
|
+
const {
|
|
100
|
+
gender = "masculine",
|
|
101
|
+
grammaticalCase = "raf3", // raf3 | nasb
|
|
102
|
+
definite = false,
|
|
103
|
+
ordinal = false, // true = positions
|
|
104
|
+
currency = null, // { main, sub, subUnder10 }
|
|
105
|
+
} = defaultOptions;
|
|
106
|
+
|
|
107
|
+
const ones = ordinal ? ordinals[gender] : dictionaries[gender];
|
|
108
|
+
|
|
109
|
+
if (!ones) throw new Error("Invalid gender option");
|
|
110
|
+
|
|
111
|
+
const adjustCase = (word) => {
|
|
112
|
+
if (!word) return word;
|
|
113
|
+
if (grammaticalCase === "nasb") {
|
|
114
|
+
if (word.endsWith("ون")) return word.replace("ون", "ين");
|
|
115
|
+
if (word.endsWith("ان")) return word.replace("ان", "ين");
|
|
116
|
+
}
|
|
117
|
+
return word;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const applyDefinite = (word) => {
|
|
121
|
+
if (!definite || !word) return word;
|
|
122
|
+
if (word.startsWith("ال")) return word;
|
|
123
|
+
return "ال" + word;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const numberToTriplets = (num) => {
|
|
127
|
+
return num
|
|
128
|
+
.toString()
|
|
129
|
+
.padStart(Math.ceil(num.toString().length / 3) * 3, "0")
|
|
130
|
+
.match(/.{1,3}/g)
|
|
131
|
+
.map((g) => g.split("").map(Number));
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const convertTriplet = ([h = 0, t = 0, u = 0]) => {
|
|
135
|
+
if (ordinal) {
|
|
136
|
+
// 1–10 → direct ordinal
|
|
137
|
+
if (h === 0 && t === 0 && u > 0 && u <= 10) return ones[u];
|
|
138
|
+
|
|
139
|
+
// 11–19 → special ordinal
|
|
140
|
+
if (h === 0 && t === 1 && u > 0) {
|
|
141
|
+
if (gender === "masculine") {
|
|
142
|
+
if (u === 1) return "الحادي عشر";
|
|
143
|
+
if (u === 2) return "الثاني عشر";
|
|
144
|
+
return ordinals.masculine[u] + " عشر";
|
|
145
|
+
} else {
|
|
146
|
+
if (u === 1) return "الحادية عشرة";
|
|
147
|
+
if (u === 2) return "الثانية عشرة";
|
|
148
|
+
return ordinals.feminine[u] + " عشرة";
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Regular cardinal numbers
|
|
154
|
+
let parts = [];
|
|
155
|
+
if (h) parts.push(hundreds[h]);
|
|
156
|
+
|
|
157
|
+
if (t === 1 && u > 0) {
|
|
158
|
+
if (u === 1) parts.push("أحد عشر");
|
|
159
|
+
else if (u === 2) parts.push("اثنا عشر");
|
|
160
|
+
else parts.push(dictionaries[gender][u] + " عشر");
|
|
161
|
+
} else {
|
|
162
|
+
if (u) parts.push(dictionaries[gender][u]);
|
|
163
|
+
if (t) parts.push(adjustCase(tens[t]));
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return parts.join(" و ");
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const getSectorLabel = (value, sectorIndex) => {
|
|
170
|
+
if (sectorIndex === 0) return "";
|
|
171
|
+
|
|
172
|
+
let label;
|
|
173
|
+
|
|
174
|
+
if (value === 1) label = sectors[sectorIndex].singular;
|
|
175
|
+
else if (value === 2) label = sectors[sectorIndex].dual;
|
|
176
|
+
else if (value >= 3 && value <= 10) label = sectors[sectorIndex].plural;
|
|
177
|
+
else label = sectors[sectorIndex].singular;
|
|
178
|
+
|
|
179
|
+
label = adjustCase(label);
|
|
180
|
+
label = applyDefinite(label);
|
|
181
|
+
|
|
182
|
+
return label;
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
const formatNumber = (num) => {
|
|
186
|
+
if (num === 0) return "صفر";
|
|
187
|
+
|
|
188
|
+
const triplets = numberToTriplets(num);
|
|
189
|
+
let result = [];
|
|
190
|
+
|
|
191
|
+
for (let i = 0; i < triplets.length; i++) {
|
|
192
|
+
const sectorIndex = triplets.length - 1 - i;
|
|
193
|
+
const value = parseInt(triplets[i].join(""));
|
|
194
|
+
if (value === 0) continue;
|
|
195
|
+
|
|
196
|
+
const words = convertTriplet(triplets[i]);
|
|
197
|
+
const label = getSectorLabel(value, sectorIndex);
|
|
198
|
+
|
|
199
|
+
if ((value === 1 || value === 2) && sectorIndex > 0) {
|
|
200
|
+
result.push(label);
|
|
201
|
+
} else {
|
|
202
|
+
result.push(words + (label ? " " + label : ""));
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return result.join(" و ");
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
const formatCurrency = (amount) => {
|
|
210
|
+
const [mainPart, subPartStr] = amount.toFixed(2).split(".");
|
|
211
|
+
const mainNum = parseInt(mainPart);
|
|
212
|
+
const subNum = parseInt(subPartStr);
|
|
213
|
+
|
|
214
|
+
const mainText = formatNumber(mainNum);
|
|
215
|
+
|
|
216
|
+
let subText = "";
|
|
217
|
+
if (subNum > 0) {
|
|
218
|
+
const subLabel =
|
|
219
|
+
subNum <= 10 && currency.subUnder10
|
|
220
|
+
? currency.subUnder10
|
|
221
|
+
: currency.sub;
|
|
222
|
+
|
|
223
|
+
subText = `${formatNumber(subNum)} ${subLabel}`;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (subText) return `${mainText} ${currency.main} و ${subText}`;
|
|
227
|
+
return `${mainText} ${currency.main}`;
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
return {
|
|
231
|
+
format: (num) => {
|
|
232
|
+
if (currency) return formatCurrency(num);
|
|
233
|
+
return formatNumber(num);
|
|
234
|
+
},
|
|
235
|
+
};
|
|
236
|
+
};
|