currency.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * DevExtreme (localization/globalize/currency.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. function _typeof(obj) {
  11. "@babel/helpers - typeof";
  12. return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(obj) {
  13. return typeof obj
  14. } : function(obj) {
  15. return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj
  16. }, _typeof(obj)
  17. }
  18. require("./core");
  19. require("./number");
  20. require("../currency");
  21. require("globalize/currency");
  22. var enCurrencyUSD = {
  23. main: {
  24. en: {
  25. identity: {
  26. version: {
  27. _cldrVersion: "28",
  28. _number: "$Revision: 11972 $"
  29. },
  30. language: "en"
  31. },
  32. numbers: {
  33. currencies: {
  34. USD: {
  35. displayName: "US Dollar",
  36. "displayName-count-one": "US dollar",
  37. "displayName-count-other": "US dollars",
  38. symbol: "$",
  39. "symbol-alt-narrow": "$"
  40. }
  41. }
  42. }
  43. }
  44. }
  45. };
  46. var currencyData = {
  47. supplemental: {
  48. version: {
  49. _cldrVersion: "28",
  50. _unicodeVersion: "8.0.0",
  51. _number: "$Revision: 11969 $"
  52. },
  53. currencyData: {
  54. fractions: {
  55. DEFAULT: {
  56. _rounding: "0",
  57. _digits: "2"
  58. }
  59. }
  60. }
  61. }
  62. };
  63. var Globalize = require("globalize");
  64. var config = require("../../core/config");
  65. var numberLocalization = require("../number");
  66. if (Globalize && Globalize.formatCurrency) {
  67. if ("en" === Globalize.locale().locale) {
  68. Globalize.load(enCurrencyUSD, currencyData);
  69. Globalize.locale("en")
  70. }
  71. var formattersCache = {};
  72. var getFormatter = function(currency, format) {
  73. var formatter;
  74. var formatCacheKey;
  75. if ("object" === _typeof(format)) {
  76. formatCacheKey = Globalize.locale().locale + ":" + currency + ":" + JSON.stringify(format)
  77. } else {
  78. formatCacheKey = Globalize.locale().locale + ":" + currency + ":" + format
  79. }
  80. formatter = formattersCache[formatCacheKey];
  81. if (!formatter) {
  82. formatter = formattersCache[formatCacheKey] = Globalize.currencyFormatter(currency, format)
  83. }
  84. return formatter
  85. };
  86. var globalizeCurrencyLocalization = {
  87. _formatNumberCore: function(value, format, formatConfig) {
  88. if ("currency" === format) {
  89. var currency = formatConfig && formatConfig.currency || config().defaultCurrency;
  90. return getFormatter(currency, this._normalizeFormatConfig(format, formatConfig, value))(value)
  91. }
  92. return this.callBase.apply(this, arguments)
  93. },
  94. _normalizeFormatConfig: function(format, formatConfig, value) {
  95. var config = this.callBase(format, formatConfig, value);
  96. if ("currency" === format) {
  97. config.style = "accounting"
  98. }
  99. return config
  100. },
  101. format: function(value, _format) {
  102. if ("number" !== typeof value) {
  103. return value
  104. }
  105. _format = this._normalizeFormat(_format);
  106. if (_format) {
  107. if ("default" === _format.currency) {
  108. _format.currency = config().defaultCurrency
  109. }
  110. if ("currency" === _format.type) {
  111. return this._formatNumber(value, this._parseNumberFormatString("currency"), _format)
  112. } else {
  113. if (!_format.type && _format.currency) {
  114. return getFormatter(_format.currency, _format)(value)
  115. }
  116. }
  117. }
  118. return this.callBase.apply(this, arguments)
  119. },
  120. getCurrencySymbol: function(currency) {
  121. if (!currency) {
  122. currency = config().defaultCurrency
  123. }
  124. return Globalize.cldr.main("numbers/currencies/" + currency)
  125. },
  126. getOpenXmlCurrencyFormat: function(currency) {
  127. var currencySymbol = this.getCurrencySymbol(currency).symbol;
  128. var currencyFormat = Globalize.cldr.main("numbers/currencyFormats-numberSystem-latn");
  129. var i;
  130. var result;
  131. var symbol;
  132. var encodeSymbols;
  133. if (currencyFormat.accounting) {
  134. encodeSymbols = {
  135. ".00": "{0}",
  136. "'": "\\'",
  137. "\\(": "\\(",
  138. "\\)": "\\)",
  139. " ": "\\ ",
  140. '"': """,
  141. "\\\xa4": currencySymbol
  142. };
  143. result = currencyFormat.accounting.split(";");
  144. for (i = 0; i < result.length; i++) {
  145. for (symbol in encodeSymbols) {
  146. if (Object.prototype.hasOwnProperty.call(encodeSymbols, symbol)) {
  147. result[i] = result[i].replace(new RegExp(symbol, "g"), encodeSymbols[symbol])
  148. }
  149. }
  150. }
  151. return 2 === result.length ? result[0] + "_);" + result[1] : result[0]
  152. }
  153. }
  154. };
  155. numberLocalization.inject(globalizeCurrencyLocalization)
  156. }