UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

113 lines (111 loc) 4.24 kB
/** * DevExtreme (cjs/__internal/core/localization/globalize/number.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/ */ "use strict"; require("../../../core/localization/globalize/core"); require("globalize/number"); var _errors = _interopRequireDefault(require("../../../../core/errors")); var _number2 = _interopRequireDefault(require("../../../core/localization/number")); var _globalize = _interopRequireDefault(require("globalize")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e } } const MAX_FRACTION_DIGITS = 20; if (null !== _globalize.default && void 0 !== _globalize.default && _globalize.default.formatNumber) { if ("en" === _globalize.default.locale().locale) { _globalize.default.locale("en") } const formattersCache = {}; const getFormatter = format => { let formatter; let formatCacheKey; if ("object" === typeof format) { formatCacheKey = `${_globalize.default.locale().locale}:${JSON.stringify(format)}` } else { formatCacheKey = `${_globalize.default.locale().locale}:${format}` } formatter = formattersCache[formatCacheKey]; if (!formatter) { formatter = _globalize.default.numberFormatter(format); formattersCache[formatCacheKey] = formatter } return formatter }; const globalizeNumberLocalization = { engine: () => "globalize", _formatNumberCore(value, format, formatConfig) { if ("exponential" === format) { return this.callBase.apply(this, [value, format, formatConfig]) } return getFormatter(this._normalizeFormatConfig(format, formatConfig, value))(value) }, _normalizeFormatConfig(format, formatConfig, value) { let config; if ("decimal" === format) { config = { minimumIntegerDigits: formatConfig.precision || 1, useGrouping: false, minimumFractionDigits: 0, maximumFractionDigits: 20, round: value < 0 ? "ceil" : "floor" } } else { config = this._getPrecisionConfig(formatConfig.precision) } if ("percent" === format) { config.style = "percent" } return config }, _getPrecisionConfig(precision) { let config; if (null === precision) { config = { minimumFractionDigits: 0, maximumFractionDigits: 20 } } else { config = { minimumFractionDigits: precision ?? 0, maximumFractionDigits: precision ?? 0 } } return config }, format(value, format) { if ("number" !== typeof value) { return value } format = this._normalizeFormat(format); if (!format || "function" !== typeof format && !format.type && !format.formatter) { return getFormatter(format)(value) } return this.callBase.apply(this, [value, format]) }, parse(text, format) { if (!text) { return } if (format && ("string" === typeof format || format.parser)) { return this.callBase.apply(this, [text, format]) } if (format) { _errors.default.log("W0011") } let result = _globalize.default.parseNumber(text); if (isNaN(result)) { result = this.callBase.apply(this, [text, format]) } return result } }; _number2.default.resetInjection(); _number2.default.inject(globalizeNumberLocalization) }