@scayle/storefront-nuxt
Version:
Nuxt integration for the SCAYLE Commerce Engine and Storefront API
38 lines (37 loc) • 1.58 kB
JavaScript
import { getDefaultFractionDigits } from "@scayle/storefront-core";
import { useCurrentShop } from "./useCurrentShop.js";
export function useFormatHelpers() {
const currentShop = useCurrentShop();
const formatCurrency = (value, options) => {
const locale = options?.locale ?? currentShop.value.locale;
const currencyFractionDigits = options?.currencyFractionDigits ?? currentShop.value.currencyFractionDigits;
const currency = options?.style !== "decimal" ? options?.currency ?? currentShop.value.currency : void 0;
const minimumFractionDigits = options?.style !== "decimal" ? getDefaultFractionDigits(
locale,
options?.currency ?? currentShop.value.currency
) : currencyFractionDigits;
return (value / 100).toLocaleString(locale, {
style: options?.style ?? "currency",
...currencyFractionDigits && {
minimumFractionDigits: minimumFractionDigits !== void 0 ? Math.min(minimumFractionDigits, currencyFractionDigits) : currencyFractionDigits
},
maximumFractionDigits: currencyFractionDigits,
currency
});
};
const formatPercentage = (value, options) => {
const locale = options?.locale ?? currentShop.value.locale;
const minimumFractionDigits = options?.minimumFractionDigits ?? 0;
const maximumFractionDigits = options?.maximumFractionDigits ?? 2;
return value.toLocaleString(locale, {
style: "percent",
minimumFractionDigits,
maximumFractionDigits,
signDisplay: options?.signDisplay
});
};
return {
formatCurrency,
formatPercentage
};
}