UNPKG

@automattic/format-currency

Version:

JavaScript library for formatting currency.

27 lines 1.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getCachedFormatter = getCachedFormatter; const formatterCache = new Map(); /** * Creating an Intl.NumberFormat is expensive, so this allows caching. * * TODO clk numberFormatCurrency Caching logic now same as numberFormat, except for fallback. * TODO clk numberFormatCurrency This should replace numberFormat's caching logic, after some cleanup (remove console.warn). */ function getCachedFormatter({ locale, fallbackLocale = 'en', options, }) { const cacheKey = JSON.stringify([locale, options]); try { return (formatterCache.get(cacheKey) ?? formatterCache.set(cacheKey, new Intl.NumberFormat(locale, options)).get(cacheKey)); } catch (error) { // If the locale is invalid, creating the NumberFormat will throw. // eslint-disable-next-line no-console console.warn(`Intl.NumberFormat was called with a non-existent locale "${locale}"; falling back to ${fallbackLocale}`); return getCachedFormatter({ locale: fallbackLocale, options, }); } } //# sourceMappingURL=get-cached-formatter.js.map