|
| 1 | +/** |
| 2 | + * Arabic number-to-words converter |
| 3 | + * Uses masculine default forms with standard group-of-3 pattern |
| 4 | + * @module languages/ar |
| 5 | + */ |
| 6 | + |
| 7 | +const AR_ONES = Object.freeze(['', 'واحد', 'اثنان', 'ثلاثة', 'أربعة', 'خمسة', 'ستة', 'سبعة', 'ثمانية', 'تسعة']); |
| 8 | +const AR_TEENS = Object.freeze(['عشرة', 'أحد عشر', 'اثنا عشر', 'ثلاثة عشر', 'أربعة عشر', 'خمسة عشر', 'ستة عشر', 'سبعة عشر', 'ثمانية عشر', 'تسعة عشر']); |
| 9 | +const AR_TENS = Object.freeze(['', '', 'عشرون', 'ثلاثون', 'أربعون', 'خمسون', 'ستون', 'سبعون', 'ثمانون', 'تسعون']); |
| 10 | +const AR_HUNDREDS = Object.freeze(['', 'مائة', 'مائتان', 'ثلاثمائة', 'أربعمائة', 'خمسمائة', 'ستمائة', 'سبعمائة', 'ثمانمائة', 'تسعمائة']); |
| 11 | +const AR_ILLIONS = Object.freeze([ |
| 12 | + ['', '', ''], // ones |
| 13 | + ['ألف', 'ألفان', 'آلاف'], // thousands |
| 14 | + ['مليون', 'مليونان', 'ملايين'], // millions |
| 15 | + ['مليار', 'ملياران', 'مليارات'], // billions |
| 16 | + ['تريليون', 'تريليونان', 'تريليونات'], // trillions |
| 17 | + ['كوادريليون', 'كوادريليونان', 'كوادريليونات'], // quadrillions |
| 18 | + ['كوينتيليون', 'كوينتيليونان', 'كوينتيليونات'], // quintillions |
| 19 | + ['سكستيليون', 'سكستيليونان', 'سكستيليونات'], // sextillions |
| 20 | + ['سبتيليون', 'سبتيليونان', 'سبتيليونات'], // septillions |
| 21 | + ['أوكتيليون', 'أوكتيليونان', 'أوكتيليونات'], // octillions |
| 22 | + ['نونيليون', 'نونيليونان', 'نونيليونات'], // nonillions |
| 23 | + ['ديسيليون', 'ديسيليونان', 'ديسيليونات'] // decillions |
| 24 | +]); |
| 25 | + |
| 26 | +/** Maximum supported value (10^36 - 1, up to decillions) */ |
| 27 | +const MAX_VALUE = 10n ** 36n - 1n; |
| 28 | + |
| 29 | +const group = (n) => Math.ceil(n.toString().length / 3) - 1; |
| 30 | +const power = (g) => 10n ** BigInt(g * 3); |
| 31 | +const segment = (n, g) => n % power(g + 1); |
| 32 | +const hundment = (n, g) => Number(segment(n, g) / power(g)); |
| 33 | +const tenment = (n, g) => hundment(n, g) % 100; |
| 34 | + |
| 35 | +/** |
| 36 | + * Get the correct Arabic scale word form |
| 37 | + * Arabic has 3 forms: singular (1), dual (2), plural (3-10) |
| 38 | + * For 11+, use the singular form |
| 39 | + */ |
| 40 | +const getArScale = (n, forms) => { |
| 41 | + if (n === 1) return forms[0]; |
| 42 | + if (n === 2) return forms[1]; |
| 43 | + if (n >= 3 && n <= 10) return forms[2]; |
| 44 | + return forms[0]; // 11+ uses singular |
| 45 | +}; |
| 46 | + |
| 47 | +const hundredAr = (n) => { |
| 48 | + if (n < 100 || n >= 1000) return ''; |
| 49 | + return AR_HUNDREDS[Math.floor(n / 100)]; |
| 50 | +}; |
| 51 | + |
| 52 | +const tenAr = (n) => { |
| 53 | + if (n === 0) return ''; |
| 54 | + if (n < 10) return AR_ONES[n]; |
| 55 | + if (n < 20) return AR_TEENS[n - 10]; |
| 56 | + const onesDigit = n % 10; |
| 57 | + const tensDigit = Math.floor(n / 10); |
| 58 | + if (onesDigit === 0) return AR_TENS[tensDigit]; |
| 59 | + // In Arabic, ones come before tens: واحد وعشرون (one and twenty) |
| 60 | + return `${AR_ONES[onesDigit]} و${AR_TENS[tensDigit]}`; |
| 61 | +}; |
| 62 | + |
| 63 | +/** |
| 64 | + * Convert a number to Arabic words |
| 65 | + * @param {number|bigint} n - The number to convert |
| 66 | + * @returns {string|false} The Arabic word representation |
| 67 | + * |
| 68 | + * @example |
| 69 | + * arabic(42) // 'اثنان وأربعون' |
| 70 | + * arabic(1000) // 'ألف' |
| 71 | + */ |
| 72 | +const arabic = (n) => { |
| 73 | + let num; |
| 74 | + |
| 75 | + if (typeof n === 'bigint') { |
| 76 | + if (n < 0n || n > MAX_VALUE) return false; |
| 77 | + num = n; |
| 78 | + } else if (typeof n === 'number') { |
| 79 | + if (isNaN(n) || n < 0 || !Number.isInteger(n)) return false; |
| 80 | + if (n > Number.MAX_SAFE_INTEGER) return false; |
| 81 | + num = BigInt(n); |
| 82 | + } else { |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
| 86 | + if (num === 0n) return 'صفر'; |
| 87 | + |
| 88 | + const parts = []; |
| 89 | + for (let i = group(num); i >= 0; i--) { |
| 90 | + const h = hundment(num, i); |
| 91 | + if (h === 0) continue; |
| 92 | + |
| 93 | + let chunk = ''; |
| 94 | + const hund = hundredAr(h); |
| 95 | + const t = tenment(num, i); |
| 96 | + const tenWord = tenAr(t); |
| 97 | + |
| 98 | + if (hund && tenWord) { |
| 99 | + chunk = `${hund} و${tenWord}`; |
| 100 | + } else if (hund) { |
| 101 | + chunk = hund; |
| 102 | + } else if (tenWord) { |
| 103 | + chunk = tenWord; |
| 104 | + } |
| 105 | + |
| 106 | + if (i > 0) { |
| 107 | + const forms = AR_ILLIONS[i]; |
| 108 | + if (h === 1) { |
| 109 | + // Just the scale word alone (e.g., ألف for 1000) |
| 110 | + chunk = forms[0]; |
| 111 | + } else if (h === 2) { |
| 112 | + // Dual form (e.g., ألفان for 2000) |
| 113 | + chunk = forms[1]; |
| 114 | + } else { |
| 115 | + // 3+: chunk + scale word |
| 116 | + const scaleWord = getArScale(h, forms); |
| 117 | + chunk = `${chunk} ${scaleWord}`; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + parts.push(chunk); |
| 122 | + } |
| 123 | + |
| 124 | + return parts.join(' و'); |
| 125 | +}; |
| 126 | + |
| 127 | +export default arabic; |
| 128 | +export { arabic, AR_ONES, AR_TENS, AR_TEENS, AR_HUNDREDS, AR_ILLIONS, MAX_VALUE }; |
0 commit comments