UNPKG

@shopify/hydrogen-react

Version:

React components, hooks, and utilities for creating custom Shopify storefronts

73 lines (72 loc) 3.4 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); const require$$0 = require("react"); const ShopifyProvider = require("./ShopifyProvider.js"); function useMoney(money) { const { locale } = ShopifyProvider.useShop(); if (!locale) { throw new Error(`useMoney(): Unable to get 'locale' from 'useShop()', which means that 'locale' was not passed to '<ShopifyProvider/>'. 'locale' is required for 'useMoney()' to work`); } const amount = parseFloat(money.amount); const options = require$$0.useMemo(() => ({ style: "currency", currency: money.currencyCode }), [money.currencyCode]); const defaultFormatter = useLazyFormatter(locale, options); const nameFormatter = useLazyFormatter(locale, { ...options, currencyDisplay: "name" }); const narrowSymbolFormatter = useLazyFormatter(locale, { ...options, currencyDisplay: "narrowSymbol" }); const withoutTrailingZerosFormatter = useLazyFormatter(locale, { ...options, minimumFractionDigits: 0, maximumFractionDigits: 0 }); const withoutCurrencyFormatter = useLazyFormatter(locale); const withoutTrailingZerosOrCurrencyFormatter = useLazyFormatter(locale, { minimumFractionDigits: 0, maximumFractionDigits: 0 }); const isPartCurrency = (part) => part.type === "currency"; const lazyFormatters = require$$0.useMemo(() => ({ original: () => money, currencyCode: () => money.currencyCode, localizedString: () => defaultFormatter().format(amount), parts: () => defaultFormatter().formatToParts(amount), withoutTrailingZeros: () => amount % 1 === 0 ? withoutTrailingZerosFormatter().format(amount) : defaultFormatter().format(amount), withoutTrailingZerosAndCurrency: () => amount % 1 === 0 ? withoutTrailingZerosOrCurrencyFormatter().format(amount) : withoutCurrencyFormatter().format(amount), currencyName: () => { var _a, _b; return (_b = (_a = nameFormatter().formatToParts(amount).find(isPartCurrency)) == null ? void 0 : _a.value) != null ? _b : money.currencyCode; }, currencySymbol: () => { var _a, _b; return (_b = (_a = defaultFormatter().formatToParts(amount).find(isPartCurrency)) == null ? void 0 : _a.value) != null ? _b : money.currencyCode; }, currencyNarrowSymbol: () => { var _a, _b; return (_b = (_a = narrowSymbolFormatter().formatToParts(amount).find(isPartCurrency)) == null ? void 0 : _a.value) != null ? _b : ""; }, amount: () => defaultFormatter().formatToParts(amount).filter((part) => ["decimal", "fraction", "group", "integer", "literal"].includes(part.type)).map((part) => part.value).join("") }), [money, amount, nameFormatter, defaultFormatter, narrowSymbolFormatter, withoutCurrencyFormatter, withoutTrailingZerosFormatter, withoutTrailingZerosOrCurrencyFormatter]); return require$$0.useMemo(() => new Proxy(lazyFormatters, { get: (target, key) => { var _a; return (_a = Reflect.get(target, key)) == null ? void 0 : _a.call(null); } }), [lazyFormatters]); } function useLazyFormatter(locale, options) { return require$$0.useMemo(() => { let memoized; return () => memoized != null ? memoized : memoized = new Intl.NumberFormat(locale, options); }, [locale, options]); } exports.useMoney = useMoney; //# sourceMappingURL=useMoney.js.map