@tmllak-erp/formatters
v0.3.0
Published
Number, date, and text formatting for Arabic and English interfaces. Every function returns **Western digits (0–9)** and uses the **Gregorian calendar**, in both languages. Plain `ar-SA` formatting would otherwise switch to Hijri dates and Arabic-Indic di
Downloads
588
Keywords
Readme
@tmllak-erp/formatters
Number, date, and text formatting for Arabic and English interfaces. Every function returns Western digits (0–9) and uses the Gregorian calendar, in both languages. Plain ar-SA formatting would otherwise switch to Hijri dates and Arabic-Indic digits.
No dependencies, no React, side-effect free. It works in browsers, Node, and server components.
Install
npm install @tmllak-erp/formattersESM only, with TypeScript declarations.
Usage
import { formatAmount, formatDate, formatDateTime, formatLongDate, formatNumber, formatPercent, formatRelativeTime } from '@tmllak-erp/formatters';
const date = new Date(2026, 9, 5, 14, 5);
formatNumber(12450.7); // "12,450.7"
formatAmount(12450.7); // "12,450.70" (always two decimals; put the currency in the column header)
formatPercent(0.85); // "85%"
formatDate(date); // "2026/10/05"
formatLongDate(date); // "5 أكتوبر 2026", or "5 October 2026" in English
formatDateTime(date, 'en'); // "5 October 2026 at 14:05"
formatRelativeTime(new Date(Date.now() - 2 * 3_600_000)); // "قبل ساعتين" / "2 hours ago"| Function | Output |
| --- | --- |
| formatNumber(value, { minimumFractionDigits?, maximumFractionDigits?, locale? }) | Grouped number, up to 2 decimals by default. |
| formatAmount(value, { locale? }) | Number with exactly 2 decimals. |
| formatPercent(ratio, fractionDigits?, locale?) | 0.85 → 85% (Western % sign). |
| formatDate(date) | YYYY/MM/DD, the same in both languages. |
| formatLongDate(date, locale?) | Day, month name, year. |
| formatTime(date, locale?), formatDateTime(date, locale?) | Time, or date with time. |
| formatMonthYear, formatMonthName, formatWeekday, formatFullDate | Calendar headers and accessible names. |
| formatRelativeTime(date, now?, locale?) | "2 hours ago", "in 3 days". |
| formatLatinValue(kind, value) | Normalizes phone, email, url, iban, technical-id (see LATIN_VALUE_KINDS). |
| toWesternDigits, normalizeNumericInput | Convert Arabic-Indic digits typed by users to 0–9. |
| stripBidiControls, toLatinDigits | Clean copied text and digits. |
Language
Functions that take an optional locale ('ar' or 'en') use the active locale when you leave it out. The active locale starts as Arabic. Inside an @tmllak-erp/ui application, ErpProvider sets it for you.
import { formatLongDate, getActiveLocale, setActiveLocale } from '@tmllak-erp/formatters';
setActiveLocale('en');
formatLongDate(new Date(2026, 9, 5)); // "5 October 2026"
getActiveLocale(); // "en"
// An explicit locale always wins, which is the safest choice in server code:
formatLongDate(new Date(2026, 9, 5), 'ar'); // "5 أكتوبر 2026"| Export | Purpose |
| --- | --- |
| UI_LOCALES, DEFAULT_UI_LOCALE, isUiLocale | Supported languages (ar, en) and a type guard. Type UiLocale. |
| getActiveLocale(), setActiveLocale(locale) | Read or change the active language. |
| registerRenderLocaleReader(reader) | For UI layers: report the language of the component rendering on a server (see Servers). |
| directionForLocale(locale) | rtl for Arabic, ltr for English. |
| intlLocale(locale) | The BCP 47 tag used with Intl (ar-SA-u-nu-latn-ca-gregory, en-GB-u-nu-latn-ca-gregory). |
Servers
On Node.js 20.16+ / 22.3+, and on edge runtimes that provide AsyncLocalStorage, the active locale is kept per request. Concurrent server renders in different languages therefore do not affect each other, including streaming renders that resume after Suspense. Elsewhere it is a single module-level value.
Some renderers resume a suspended part of the page outside the request's async context. Next.js does this when it streams a Suspense boundary from the RSC payload. For that case a UI layer can report the language of the component being rendered with registerRenderLocaleReader. @tmllak-erp/ui registers one that reads ErpProvider (React 19).
Code outside a React render (route handlers, server actions, async server components that await between formatting calls) should pass locale explicitly.
Versions and upgrades
All @tmllak-erp/* packages share one version number. Upgrade them together and read the CHANGELOG.md of each package. While the version is 0.x, a minor release can contain breaking changes.
