degachejs
Version:
A Tunisian utility library for working with CIN, phone numbers, addresses, and more
47 lines (46 loc) • 1.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatDate = exports.formatCurrency = exports.formatPhoneNumber = void 0;
const constants_1 = require("../constants");
/**
* Formats a Tunisian phone number
* @param phoneNumber - The phone number to format
* @returns formatted phone number with country code and proper spacing
*/
const formatPhoneNumber = (phoneNumber) => {
const cleaned = phoneNumber.replace(/\D/g, "");
if (cleaned.length !== 8)
return phoneNumber;
return `${constants_1.COUNTRY_CODE} ${cleaned.slice(0, 2)} ${cleaned.slice(2, 5)} ${cleaned.slice(5)}`;
};
exports.formatPhoneNumber = formatPhoneNumber;
/**
* Formats a monetary amount in Tunisian Dinar
* @param amount - The amount to format
* @param options - Formatting options
* @returns formatted amount with currency symbol
*/
const formatCurrency = (amount, options = {}) => {
const formatter = new Intl.NumberFormat("ar-TN", {
style: "currency",
currency: constants_1.CURRENCY.CODE,
currencyDisplay: options.code ? "code" : options.symbol ? "symbol" : "name",
});
return formatter.format(amount);
};
exports.formatCurrency = formatCurrency;
/**
* Formats a date according to Tunisian locale
* @param date - The date to format
* @param options - Intl.DateTimeFormat options
* @returns formatted date string
*/
const formatDate = (date, options = {
year: "numeric",
month: "long",
day: "numeric",
}) => {
const formatter = new Intl.DateTimeFormat("ar-TN", options);
return formatter.format(date);
};
exports.formatDate = formatDate;