UNPKG

arabicfmt

Version:

Arabic-first formatting for numbers, currency, dates and bidirectional text across all 22 Arab League countries — with correct handling of the 2025–2026 Unicode currency-symbol transition (Saudi Riyal U+20C1, UAE Dirham U+20C3, Omani Rial U+20C4).

147 lines (143 loc) 4.49 kB
import { toArabicDigits } from './chunk-OA4S5ZUV.js'; // src/date/format.ts var HIJRI_MONTHS_AR = [ "\u0645\u062D\u0631\u0651\u0645", "\u0635\u0641\u0631", "\u0631\u0628\u064A\u0639 \u0627\u0644\u0623\u0648\u0644", "\u0631\u0628\u064A\u0639 \u0627\u0644\u0622\u062E\u0631", "\u062C\u0645\u0627\u062F\u0649 \u0627\u0644\u0623\u0648\u0644\u0649", "\u062C\u0645\u0627\u062F\u0649 \u0627\u0644\u0622\u062E\u0631\u0629", "\u0631\u062C\u0628", "\u0634\u0639\u0628\u0627\u0646", "\u0631\u0645\u0636\u0627\u0646", "\u0634\u0648\u0651\u0627\u0644", "\u0630\u0648 \u0627\u0644\u0642\u0639\u062F\u0629", "\u0630\u0648 \u0627\u0644\u062D\u062C\u0629" ]; var HIJRI_MONTHS_EN = [ "Muharram", "Safar", "Rabi al-Awwal", "Rabi al-Thani", "Jumada al-Awwal", "Jumada al-Thani", "Rajab", "Shaban", "Ramadan", "Shawwal", "Dhu al-Qadah", "Dhu al-Hijjah" ]; var HIJRI_ERA_AR = "\u0647\u0640"; var HIJRI_ERA_EN = "AH"; var GREGORIAN_MONTHS_AR = [ "\u064A\u0646\u0627\u064A\u0631", "\u0641\u0628\u0631\u0627\u064A\u0631", "\u0645\u0627\u0631\u0633", "\u0623\u0628\u0631\u064A\u0644", "\u0645\u0627\u064A\u0648", "\u064A\u0648\u0646\u064A\u0648", "\u064A\u0648\u0644\u064A\u0648", "\u0623\u063A\u0633\u0637\u0633", "\u0633\u0628\u062A\u0645\u0628\u0631", "\u0623\u0643\u062A\u0648\u0628\u0631", "\u0646\u0648\u0641\u0645\u0628\u0631", "\u062F\u064A\u0633\u0645\u0628\u0631" ]; var GREGORIAN_MONTHS_EN = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; var ARABIC_WEEKDAYS_AR = [ "\u0627\u0644\u0623\u062D\u062F", "\u0627\u0644\u0627\u062B\u0646\u064A\u0646", "\u0627\u0644\u062B\u0644\u0627\u062B\u0627\u0621", "\u0627\u0644\u0623\u0631\u0628\u0639\u0627\u0621", "\u0627\u0644\u062E\u0645\u064A\u0633", "\u0627\u0644\u062C\u0645\u0639\u0629", "\u0627\u0644\u0633\u0628\u062A" ]; var ARABIC_WEEKDAYS_EN = [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ]; function shape(value, numerals) { return numerals === "arab" ? toArabicDigits(value) : value; } function formatHijriDate(hijri, options = {}) { const locale = options.locale ?? "ar"; const isArabic = locale.toLowerCase().startsWith("ar"); const numerals = options.numerals ?? "latn"; const monthStyle = options.month ?? "long"; const named = monthStyle === "long"; const dayStr = shape( options.day === "2-digit" ? String(hijri.day).padStart(2, "0") : String(hijri.day), numerals ); let monthStr; if (named) { const names = isArabic ? HIJRI_MONTHS_AR : HIJRI_MONTHS_EN; monthStr = names[hijri.month - 1] ?? String(hijri.month); } else { monthStr = shape( monthStyle === "2-digit" ? String(hijri.month).padStart(2, "0") : String(hijri.month), numerals ); } const yearStr = shape(String(hijri.year), numerals); const separator = options.separator ?? (named ? " " : "/"); const order = options.order ?? "dmy"; const fields = order === "ymd" ? [yearStr, monthStr, dayStr] : [dayStr, monthStr, yearStr]; let out = fields.join(separator); if (options.era ?? true) { out += ` ${isArabic ? HIJRI_ERA_AR : HIJRI_ERA_EN}`; } return out; } // src/date/jdn.ts function gregorianToJDN(year, month, day) { const a = Math.floor((14 - month) / 12); const y = year + 4800 - a; const m = month + 12 * a - 3; return day + Math.floor((153 * m + 2) / 5) + 365 * y + Math.floor(y / 4) - Math.floor(y / 100) + Math.floor(y / 400) - 32045; } function jdnToGregorian(jdn) { const a = jdn + 32044; const b = Math.floor((4 * a + 3) / 146097); const c = a - Math.floor(146097 * b / 4); const d = Math.floor((4 * c + 3) / 1461); const e = c - Math.floor(1461 * d / 4); const m = Math.floor((5 * e + 2) / 153); return { day: e - Math.floor((153 * m + 2) / 5) + 1, month: m + 3 - 12 * Math.floor(m / 10), year: 100 * b + d - 4800 + Math.floor(m / 10) }; } function dateToJDN(date) { return gregorianToJDN( date.getUTCFullYear(), date.getUTCMonth() + 1, date.getUTCDate() ); } function jdnToDate(jdn) { const g = jdnToGregorian(jdn); return new Date(Date.UTC(g.year, g.month - 1, g.day)); } export { ARABIC_WEEKDAYS_AR, ARABIC_WEEKDAYS_EN, GREGORIAN_MONTHS_AR, GREGORIAN_MONTHS_EN, HIJRI_ERA_AR, HIJRI_ERA_EN, HIJRI_MONTHS_AR, HIJRI_MONTHS_EN, dateToJDN, formatHijriDate, gregorianToJDN, jdnToDate, jdnToGregorian };