UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

86 lines (85 loc) 3.94 kB
/** * DevExtreme (esm/__internal/core/localization/globalize/currency.js) * Version: 25.2.7 * Build date: Tue May 05 2026 * * Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ import "../../../core/localization/globalize/core"; import "../../../core/localization/globalize/number"; import "../../../core/localization/currency"; import "globalize/currency"; import config from "../../../../core/config"; import numberLocalization from "../../../core/localization/number"; import openXmlCurrencyFormat from "../../../core/localization/open_xml_currency_format"; import Globalize from "globalize"; const CURRENCY_STYLES = ["symbol", "accounting"]; if (null !== Globalize && void 0 !== Globalize && Globalize.formatCurrency) { if ("en" === Globalize.locale().locale) { Globalize.locale("en") } const formattersCache = {}; const getFormatter = (currency, format) => { let formatter; let formatCacheKey; if ("object" === typeof format) { formatCacheKey = `${Globalize.locale().locale}:${currency}:${JSON.stringify(format)}` } else { formatCacheKey = `${Globalize.locale().locale}:${currency}:${format}` } formatter = formattersCache[formatCacheKey]; if (!formatter) { formatter = Globalize.currencyFormatter(currency, format); formattersCache[formatCacheKey] = formatter } return formatter }; const globalizeCurrencyLocalization = { _formatNumberCore(value, format, formatConfig) { if ("currency" === format) { const currency = (null === formatConfig || void 0 === formatConfig ? void 0 : formatConfig.currency) ?? config().defaultCurrency; return getFormatter(currency, this._normalizeFormatConfig(format, formatConfig, value))(value) } return this.callBase.apply(this, [value, format, formatConfig]) }, _normalizeFormatConfig(format, formatConfig, value) { const normalizedConfig = this.callBase.apply(this, [format, formatConfig, value]); if ("currency" === format) { const useAccountingStyle = formatConfig.useCurrencyAccountingStyle ?? config().defaultUseCurrencyAccountingStyle; normalizedConfig.style = CURRENCY_STYLES[+useAccountingStyle] } return normalizedConfig }, format(value, format) { if ("number" !== typeof value) { return value } format = this._normalizeFormat(format); if (format) { if ("default" === format.currency) { format.currency = config().defaultCurrency } if ("currency" === format.type) { return this._formatNumber(value, this._parseNumberFormatString("currency"), format) } if (!format.type && format.currency) { return getFormatter(format.currency, format)(value) } } return this.callBase.apply(this, [value, format]) }, getCurrencySymbol(currency) { if (!currency) { currency = config().defaultCurrency } return Globalize.cldr.main(`numbers/currencies/${currency}`) }, getOpenXmlCurrencyFormat(currency) { const currencySymbol = this.getCurrencySymbol(currency).symbol; const accountingFormat = Globalize.cldr.main("numbers/currencyFormats-numberSystem-latn").accounting; return openXmlCurrencyFormat(currencySymbol, accountingFormat) } }; numberLocalization.inject(globalizeCurrencyLocalization) }