99 lines
3.1 KiB
JavaScript
99 lines
3.1 KiB
JavaScript
import { u as useRegionStore } from "./region-0mvXqaMM.js";
|
|
function useFormatter() {
|
|
const regionStore = useRegionStore();
|
|
function formatNumber(value, options) {
|
|
const locale = regionStore.activeLocale || "en-EU";
|
|
try {
|
|
return new Intl.NumberFormat(locale, options).format(value);
|
|
} catch {
|
|
return new Intl.NumberFormat("en-EU", options).format(value);
|
|
}
|
|
}
|
|
function formatCurrency(value, currency, options) {
|
|
const locale = regionStore.activeLocale || "en-EU";
|
|
const curr = currency || regionStore.activeCurrency || "EUR";
|
|
try {
|
|
return new Intl.NumberFormat(locale, {
|
|
style: "currency",
|
|
currency: curr,
|
|
minimumFractionDigits: 2,
|
|
maximumFractionDigits: 2,
|
|
...options
|
|
}).format(value);
|
|
} catch {
|
|
return `${value.toFixed(2)} ${curr}`;
|
|
}
|
|
}
|
|
function formatDate(date, options) {
|
|
const locale = regionStore.activeLocale || "en-EU";
|
|
const timezone = regionStore.activeTimezone || "Europe/Brussels";
|
|
const d = typeof date === "string" || typeof date === "number" ? new Date(date) : date;
|
|
try {
|
|
return new Intl.DateTimeFormat(locale, {
|
|
timezone,
|
|
year: "numeric",
|
|
month: "2-digit",
|
|
day: "2-digit",
|
|
...options
|
|
}).format(d);
|
|
} catch {
|
|
return d.toLocaleDateString("en-GB");
|
|
}
|
|
}
|
|
function formatDateTime(date, options) {
|
|
const locale = regionStore.activeLocale || "en-EU";
|
|
const timezone = regionStore.activeTimezone || "Europe/Brussels";
|
|
const d = typeof date === "string" || typeof date === "number" ? new Date(date) : date;
|
|
try {
|
|
return new Intl.DateTimeFormat(locale, {
|
|
timezone,
|
|
year: "numeric",
|
|
month: "2-digit",
|
|
day: "2-digit",
|
|
hour: "2-digit",
|
|
minute: "2-digit",
|
|
...options
|
|
}).format(d);
|
|
} catch {
|
|
return d.toLocaleString("en-GB");
|
|
}
|
|
}
|
|
function formatPercent(value, options) {
|
|
const locale = regionStore.activeLocale || "en-EU";
|
|
try {
|
|
return new Intl.NumberFormat(locale, {
|
|
style: "percent",
|
|
minimumFractionDigits: 0,
|
|
maximumFractionDigits: 1,
|
|
...options
|
|
}).format(value / 100);
|
|
} catch {
|
|
return `${value}%`;
|
|
}
|
|
}
|
|
function calculatePriceWithVat(netPrice, vatRate) {
|
|
const rate = vatRate ?? regionStore.activeVatRate ?? 0;
|
|
const vat = netPrice * (rate / 100);
|
|
const gross = netPrice + vat;
|
|
return { net: netPrice, vat, gross, vatRate: rate };
|
|
}
|
|
function formatPriceWithVat(netPrice, vatRate, currency) {
|
|
const { net, vat, gross, vatRate: rate } = calculatePriceWithVat(netPrice, vatRate);
|
|
const curr = currency || regionStore.activeCurrency || "EUR";
|
|
return `Net: ${formatCurrency(net, curr)} | VAT: ${formatCurrency(vat, curr)} (${rate}%) | Gross: ${formatCurrency(gross, curr)}`;
|
|
}
|
|
return {
|
|
formatNumber,
|
|
formatCurrency,
|
|
formatDate,
|
|
formatDateTime,
|
|
formatPercent,
|
|
calculatePriceWithVat,
|
|
formatPriceWithVat
|
|
};
|
|
}
|
|
export {
|
|
useFormatter as u
|
|
};
|
|
//# sourceMappingURL=useFormatter-hycy3urM.js.map
|