number.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /**
  2. * DevExtreme (localization/ldml/number.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. var fitIntoRange = require("../../core/utils/math").fitIntoRange;
  11. var toFixed = require("../utils").toFixed;
  12. var DEFAULT_CONFIG = {
  13. thousandsSeparator: ",",
  14. decimalSeparator: "."
  15. };
  16. var ESCAPING_CHAR = "'";
  17. var MAXIMUM_NUMBER_LENGTH = 15;
  18. function getGroupSizes(formatString) {
  19. return formatString.split(",").slice(1).map(function(str) {
  20. return str.split("").filter(function(char) {
  21. return "#" === char || "0" === char
  22. }).length
  23. })
  24. }
  25. function getSignParts(format) {
  26. var signParts = format.split(";");
  27. if (1 === signParts.length) {
  28. signParts.push("-" + signParts[0])
  29. }
  30. return signParts
  31. }
  32. function reverseString(str) {
  33. return str.toString().split("").reverse().join("")
  34. }
  35. function isPercentFormat(format) {
  36. return format.indexOf("%") !== -1 && !format.match(/'[^']*%[^']*'/g)
  37. }
  38. function getNonRequiredDigitCount(floatFormat) {
  39. if (!floatFormat) {
  40. return 0
  41. }
  42. return floatFormat.length - floatFormat.replace(/[#]/g, "").length
  43. }
  44. function getRequiredDigitCount(floatFormat) {
  45. if (!floatFormat) {
  46. return 0
  47. }
  48. return floatFormat.length - floatFormat.replace(/[0]/g, "").length
  49. }
  50. function normalizeValueString(valuePart, minDigitCount, maxDigitCount) {
  51. if (!valuePart) {
  52. return ""
  53. }
  54. if (valuePart.length > maxDigitCount) {
  55. valuePart = valuePart.substr(0, maxDigitCount)
  56. }
  57. while (valuePart.length > minDigitCount && "0" === valuePart.slice(-1)) {
  58. valuePart = valuePart.substr(0, valuePart.length - 1)
  59. }
  60. while (valuePart.length < minDigitCount) {
  61. valuePart += "0"
  62. }
  63. return valuePart
  64. }
  65. function applyGroups(valueString, groupSizes, thousandsSeparator) {
  66. if (!groupSizes.length) {
  67. return valueString
  68. }
  69. var groups = [];
  70. var index = 0;
  71. while (valueString) {
  72. var groupSize = groupSizes[index];
  73. groups.push(valueString.slice(0, groupSize));
  74. valueString = valueString.slice(groupSize);
  75. if (index < groupSizes.length - 1) {
  76. index++
  77. }
  78. }
  79. return groups.join(thousandsSeparator)
  80. }
  81. function formatNumberPart(format, valueString) {
  82. return format.split(ESCAPING_CHAR).map(function(formatPart, escapeIndex) {
  83. var isEscape = escapeIndex % 2;
  84. if (!formatPart && isEscape) {
  85. return ESCAPING_CHAR
  86. }
  87. return isEscape ? formatPart : formatPart.replace(/[,#0]+/, valueString)
  88. }).join("")
  89. }
  90. function getFloatPointIndex(format) {
  91. var isEscape = false;
  92. for (var index = 0; index < format.length; index++) {
  93. if ("'" === format[index]) {
  94. isEscape = !isEscape
  95. }
  96. if ("." === format[index] && !isEscape) {
  97. return index
  98. }
  99. }
  100. return format.length
  101. }
  102. function getFormatter(format, config) {
  103. config = config || DEFAULT_CONFIG;
  104. return function(value) {
  105. if ("number" !== typeof value || isNaN(value)) {
  106. return ""
  107. }
  108. var signFormatParts = getSignParts(format);
  109. var isPositiveZero = 1 / value === 1 / 0;
  110. var isPositive = value > 0 || isPositiveZero;
  111. var numberFormat = signFormatParts[isPositive ? 0 : 1];
  112. if (isPercentFormat(numberFormat)) {
  113. value = 100 * value
  114. }
  115. if (!isPositive) {
  116. value = -value
  117. }
  118. var floatPointIndex = getFloatPointIndex(numberFormat);
  119. var floatFormatParts = [numberFormat.substr(0, floatPointIndex), numberFormat.substr(floatPointIndex + 1)];
  120. var minFloatPrecision = getRequiredDigitCount(floatFormatParts[1]);
  121. var maxFloatPrecision = minFloatPrecision + getNonRequiredDigitCount(floatFormatParts[1]);
  122. var minIntegerPrecision = getRequiredDigitCount(floatFormatParts[0]);
  123. var maxIntegerPrecision = getNonRequiredDigitCount(floatFormatParts[0]) ? void 0 : minIntegerPrecision;
  124. var integerLength = Math.floor(value).toString().length;
  125. var floatPrecision = fitIntoRange(maxFloatPrecision, 0, MAXIMUM_NUMBER_LENGTH - integerLength);
  126. var groupSizes = getGroupSizes(floatFormatParts[0]).reverse();
  127. var valueParts = toFixed(value, floatPrecision < 0 ? 0 : floatPrecision).split(".");
  128. var valueIntegerPart = normalizeValueString(reverseString(valueParts[0]), minIntegerPrecision, maxIntegerPrecision);
  129. var valueFloatPart = normalizeValueString(valueParts[1], minFloatPrecision, maxFloatPrecision);
  130. valueIntegerPart = applyGroups(valueIntegerPart, groupSizes, config.thousandsSeparator);
  131. var integerString = reverseString(formatNumberPart(reverseString(floatFormatParts[0]), valueIntegerPart));
  132. var floatString = maxFloatPrecision ? formatNumberPart(floatFormatParts[1], valueFloatPart) : "";
  133. var result = integerString + (floatString.match(/\d/) ? config.decimalSeparator : "") + floatString;
  134. return result
  135. }
  136. }
  137. function parseValue(text, isPercent, isNegative) {
  138. var value = (isPercent ? .01 : 1) * parseFloat(text) || 0;
  139. return isNegative ? -value : value
  140. }
  141. function prepareValueText(valueText, formatter, isPercent, isIntegerPart) {
  142. var nextValueText = valueText;
  143. var char;
  144. var text;
  145. var nextText;
  146. do {
  147. if (nextText) {
  148. char = text.length === nextText.length ? "0" : "1";
  149. valueText = isIntegerPart ? char + valueText : valueText + char
  150. }
  151. text = nextText || formatter(parseValue(nextValueText, isPercent));
  152. nextValueText = isIntegerPart ? "1" + nextValueText : nextValueText + "1";
  153. nextText = formatter(parseValue(nextValueText, isPercent))
  154. } while (text !== nextText && (isIntegerPart ? text.length === nextText.length : text.length <= nextText.length));
  155. if (isIntegerPart && nextText.length > text.length) {
  156. var hasGroups = formatter(12345).indexOf("12345") === -1;
  157. do {
  158. valueText = "1" + valueText
  159. } while (hasGroups && parseValue(valueText, isPercent) < 1e5)
  160. }
  161. return valueText
  162. }
  163. function getFormatByValueText(valueText, formatter, isPercent, isNegative) {
  164. var format = formatter(parseValue(valueText, isPercent, isNegative));
  165. var valueTextParts = valueText.split(".");
  166. var valueTextWithModifiedFloat = valueTextParts[0] + ".3" + valueTextParts[1].slice(1);
  167. var valueWithModifiedFloat = parseValue(valueTextWithModifiedFloat, isPercent, isNegative);
  168. var decimalSeparatorIndex = formatter(valueWithModifiedFloat).indexOf("3") - 1;
  169. format = format.replace(/(\d)\D(\d)/g, "$1,$2");
  170. if (decimalSeparatorIndex >= 0) {
  171. format = format.slice(0, decimalSeparatorIndex) + "." + format.slice(decimalSeparatorIndex + 1)
  172. }
  173. format = format.replace(/1+/, "1").replace(/1/g, "#");
  174. if (!isPercent) {
  175. format = format.replace("%", "'%'")
  176. }
  177. return format
  178. }
  179. function getFormat(formatter) {
  180. var valueText = ".";
  181. var isPercent = formatter(1).indexOf("100") >= 0;
  182. valueText = prepareValueText(valueText, formatter, isPercent, true);
  183. valueText = prepareValueText(valueText, formatter, isPercent, false);
  184. var positiveFormat = getFormatByValueText(valueText, formatter, isPercent, false);
  185. var negativeFormat = getFormatByValueText(valueText, formatter, isPercent, true);
  186. return negativeFormat === "-" + positiveFormat ? positiveFormat : positiveFormat + ";" + negativeFormat
  187. }
  188. exports.getFormatter = getFormatter;
  189. exports.getFormat = getFormat;