Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

d3-format.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. // https://d3js.org/d3-format/ v3.1.0 Copyright 2010-2021 Mike Bostock
  2. (function (global, factory) {
  3. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  4. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  5. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.d3 = global.d3 || {}));
  6. })(this, (function (exports) { 'use strict';
  7. function formatDecimal(x) {
  8. return Math.abs(x = Math.round(x)) >= 1e21
  9. ? x.toLocaleString("en").replace(/,/g, "")
  10. : x.toString(10);
  11. }
  12. // Computes the decimal coefficient and exponent of the specified number x with
  13. // significant digits p, where x is positive and p is in [1, 21] or undefined.
  14. // For example, formatDecimalParts(1.23) returns ["123", 0].
  15. function formatDecimalParts(x, p) {
  16. if ((i = (x = p ? x.toExponential(p - 1) : x.toExponential()).indexOf("e")) < 0) return null; // NaN, ±Infinity
  17. var i, coefficient = x.slice(0, i);
  18. // The string returned by toExponential either has the form \d\.\d+e[-+]\d+
  19. // (e.g., 1.2e+3) or the form \de[-+]\d+ (e.g., 1e+3).
  20. return [
  21. coefficient.length > 1 ? coefficient[0] + coefficient.slice(2) : coefficient,
  22. +x.slice(i + 1)
  23. ];
  24. }
  25. function exponent(x) {
  26. return x = formatDecimalParts(Math.abs(x)), x ? x[1] : NaN;
  27. }
  28. function formatGroup(grouping, thousands) {
  29. return function(value, width) {
  30. var i = value.length,
  31. t = [],
  32. j = 0,
  33. g = grouping[0],
  34. length = 0;
  35. while (i > 0 && g > 0) {
  36. if (length + g + 1 > width) g = Math.max(1, width - length);
  37. t.push(value.substring(i -= g, i + g));
  38. if ((length += g + 1) > width) break;
  39. g = grouping[j = (j + 1) % grouping.length];
  40. }
  41. return t.reverse().join(thousands);
  42. };
  43. }
  44. function formatNumerals(numerals) {
  45. return function(value) {
  46. return value.replace(/[0-9]/g, function(i) {
  47. return numerals[+i];
  48. });
  49. };
  50. }
  51. // [[fill]align][sign][symbol][0][width][,][.precision][~][type]
  52. var re = /^(?:(.)?([<>=^]))?([+\-( ])?([$#])?(0)?(\d+)?(,)?(\.\d+)?(~)?([a-z%])?$/i;
  53. function formatSpecifier(specifier) {
  54. if (!(match = re.exec(specifier))) throw new Error("invalid format: " + specifier);
  55. var match;
  56. return new FormatSpecifier({
  57. fill: match[1],
  58. align: match[2],
  59. sign: match[3],
  60. symbol: match[4],
  61. zero: match[5],
  62. width: match[6],
  63. comma: match[7],
  64. precision: match[8] && match[8].slice(1),
  65. trim: match[9],
  66. type: match[10]
  67. });
  68. }
  69. formatSpecifier.prototype = FormatSpecifier.prototype; // instanceof
  70. function FormatSpecifier(specifier) {
  71. this.fill = specifier.fill === undefined ? " " : specifier.fill + "";
  72. this.align = specifier.align === undefined ? ">" : specifier.align + "";
  73. this.sign = specifier.sign === undefined ? "-" : specifier.sign + "";
  74. this.symbol = specifier.symbol === undefined ? "" : specifier.symbol + "";
  75. this.zero = !!specifier.zero;
  76. this.width = specifier.width === undefined ? undefined : +specifier.width;
  77. this.comma = !!specifier.comma;
  78. this.precision = specifier.precision === undefined ? undefined : +specifier.precision;
  79. this.trim = !!specifier.trim;
  80. this.type = specifier.type === undefined ? "" : specifier.type + "";
  81. }
  82. FormatSpecifier.prototype.toString = function() {
  83. return this.fill
  84. + this.align
  85. + this.sign
  86. + this.symbol
  87. + (this.zero ? "0" : "")
  88. + (this.width === undefined ? "" : Math.max(1, this.width | 0))
  89. + (this.comma ? "," : "")
  90. + (this.precision === undefined ? "" : "." + Math.max(0, this.precision | 0))
  91. + (this.trim ? "~" : "")
  92. + this.type;
  93. };
  94. // Trims insignificant zeros, e.g., replaces 1.2000k with 1.2k.
  95. function formatTrim(s) {
  96. out: for (var n = s.length, i = 1, i0 = -1, i1; i < n; ++i) {
  97. switch (s[i]) {
  98. case ".": i0 = i1 = i; break;
  99. case "0": if (i0 === 0) i0 = i; i1 = i; break;
  100. default: if (!+s[i]) break out; if (i0 > 0) i0 = 0; break;
  101. }
  102. }
  103. return i0 > 0 ? s.slice(0, i0) + s.slice(i1 + 1) : s;
  104. }
  105. var prefixExponent;
  106. function formatPrefixAuto(x, p) {
  107. var d = formatDecimalParts(x, p);
  108. if (!d) return x + "";
  109. var coefficient = d[0],
  110. exponent = d[1],
  111. i = exponent - (prefixExponent = Math.max(-8, Math.min(8, Math.floor(exponent / 3))) * 3) + 1,
  112. n = coefficient.length;
  113. return i === n ? coefficient
  114. : i > n ? coefficient + new Array(i - n + 1).join("0")
  115. : i > 0 ? coefficient.slice(0, i) + "." + coefficient.slice(i)
  116. : "0." + new Array(1 - i).join("0") + formatDecimalParts(x, Math.max(0, p + i - 1))[0]; // less than 1y!
  117. }
  118. function formatRounded(x, p) {
  119. var d = formatDecimalParts(x, p);
  120. if (!d) return x + "";
  121. var coefficient = d[0],
  122. exponent = d[1];
  123. return exponent < 0 ? "0." + new Array(-exponent).join("0") + coefficient
  124. : coefficient.length > exponent + 1 ? coefficient.slice(0, exponent + 1) + "." + coefficient.slice(exponent + 1)
  125. : coefficient + new Array(exponent - coefficient.length + 2).join("0");
  126. }
  127. var formatTypes = {
  128. "%": (x, p) => (x * 100).toFixed(p),
  129. "b": (x) => Math.round(x).toString(2),
  130. "c": (x) => x + "",
  131. "d": formatDecimal,
  132. "e": (x, p) => x.toExponential(p),
  133. "f": (x, p) => x.toFixed(p),
  134. "g": (x, p) => x.toPrecision(p),
  135. "o": (x) => Math.round(x).toString(8),
  136. "p": (x, p) => formatRounded(x * 100, p),
  137. "r": formatRounded,
  138. "s": formatPrefixAuto,
  139. "X": (x) => Math.round(x).toString(16).toUpperCase(),
  140. "x": (x) => Math.round(x).toString(16)
  141. };
  142. function identity(x) {
  143. return x;
  144. }
  145. var map = Array.prototype.map,
  146. prefixes = ["y","z","a","f","p","n","µ","m","","k","M","G","T","P","E","Z","Y"];
  147. function formatLocale(locale) {
  148. var group = locale.grouping === undefined || locale.thousands === undefined ? identity : formatGroup(map.call(locale.grouping, Number), locale.thousands + ""),
  149. currencyPrefix = locale.currency === undefined ? "" : locale.currency[0] + "",
  150. currencySuffix = locale.currency === undefined ? "" : locale.currency[1] + "",
  151. decimal = locale.decimal === undefined ? "." : locale.decimal + "",
  152. numerals = locale.numerals === undefined ? identity : formatNumerals(map.call(locale.numerals, String)),
  153. percent = locale.percent === undefined ? "%" : locale.percent + "",
  154. minus = locale.minus === undefined ? "−" : locale.minus + "",
  155. nan = locale.nan === undefined ? "NaN" : locale.nan + "";
  156. function newFormat(specifier) {
  157. specifier = formatSpecifier(specifier);
  158. var fill = specifier.fill,
  159. align = specifier.align,
  160. sign = specifier.sign,
  161. symbol = specifier.symbol,
  162. zero = specifier.zero,
  163. width = specifier.width,
  164. comma = specifier.comma,
  165. precision = specifier.precision,
  166. trim = specifier.trim,
  167. type = specifier.type;
  168. // The "n" type is an alias for ",g".
  169. if (type === "n") comma = true, type = "g";
  170. // The "" type, and any invalid type, is an alias for ".12~g".
  171. else if (!formatTypes[type]) precision === undefined && (precision = 12), trim = true, type = "g";
  172. // If zero fill is specified, padding goes after sign and before digits.
  173. if (zero || (fill === "0" && align === "=")) zero = true, fill = "0", align = "=";
  174. // Compute the prefix and suffix.
  175. // For SI-prefix, the suffix is lazily computed.
  176. var prefix = symbol === "$" ? currencyPrefix : symbol === "#" && /[boxX]/.test(type) ? "0" + type.toLowerCase() : "",
  177. suffix = symbol === "$" ? currencySuffix : /[%p]/.test(type) ? percent : "";
  178. // What format function should we use?
  179. // Is this an integer type?
  180. // Can this type generate exponential notation?
  181. var formatType = formatTypes[type],
  182. maybeSuffix = /[defgprs%]/.test(type);
  183. // Set the default precision if not specified,
  184. // or clamp the specified precision to the supported range.
  185. // For significant precision, it must be in [1, 21].
  186. // For fixed precision, it must be in [0, 20].
  187. precision = precision === undefined ? 6
  188. : /[gprs]/.test(type) ? Math.max(1, Math.min(21, precision))
  189. : Math.max(0, Math.min(20, precision));
  190. function format(value) {
  191. var valuePrefix = prefix,
  192. valueSuffix = suffix,
  193. i, n, c;
  194. if (type === "c") {
  195. valueSuffix = formatType(value) + valueSuffix;
  196. value = "";
  197. } else {
  198. value = +value;
  199. // Determine the sign. -0 is not less than 0, but 1 / -0 is!
  200. var valueNegative = value < 0 || 1 / value < 0;
  201. // Perform the initial formatting.
  202. value = isNaN(value) ? nan : formatType(Math.abs(value), precision);
  203. // Trim insignificant zeros.
  204. if (trim) value = formatTrim(value);
  205. // If a negative value rounds to zero after formatting, and no explicit positive sign is requested, hide the sign.
  206. if (valueNegative && +value === 0 && sign !== "+") valueNegative = false;
  207. // Compute the prefix and suffix.
  208. valuePrefix = (valueNegative ? (sign === "(" ? sign : minus) : sign === "-" || sign === "(" ? "" : sign) + valuePrefix;
  209. valueSuffix = (type === "s" ? prefixes[8 + prefixExponent / 3] : "") + valueSuffix + (valueNegative && sign === "(" ? ")" : "");
  210. // Break the formatted value into the integer “value” part that can be
  211. // grouped, and fractional or exponential “suffix” part that is not.
  212. if (maybeSuffix) {
  213. i = -1, n = value.length;
  214. while (++i < n) {
  215. if (c = value.charCodeAt(i), 48 > c || c > 57) {
  216. valueSuffix = (c === 46 ? decimal + value.slice(i + 1) : value.slice(i)) + valueSuffix;
  217. value = value.slice(0, i);
  218. break;
  219. }
  220. }
  221. }
  222. }
  223. // If the fill character is not "0", grouping is applied before padding.
  224. if (comma && !zero) value = group(value, Infinity);
  225. // Compute the padding.
  226. var length = valuePrefix.length + value.length + valueSuffix.length,
  227. padding = length < width ? new Array(width - length + 1).join(fill) : "";
  228. // If the fill character is "0", grouping is applied after padding.
  229. if (comma && zero) value = group(padding + value, padding.length ? width - valueSuffix.length : Infinity), padding = "";
  230. // Reconstruct the final output based on the desired alignment.
  231. switch (align) {
  232. case "<": value = valuePrefix + value + valueSuffix + padding; break;
  233. case "=": value = valuePrefix + padding + value + valueSuffix; break;
  234. case "^": value = padding.slice(0, length = padding.length >> 1) + valuePrefix + value + valueSuffix + padding.slice(length); break;
  235. default: value = padding + valuePrefix + value + valueSuffix; break;
  236. }
  237. return numerals(value);
  238. }
  239. format.toString = function() {
  240. return specifier + "";
  241. };
  242. return format;
  243. }
  244. function formatPrefix(specifier, value) {
  245. var f = newFormat((specifier = formatSpecifier(specifier), specifier.type = "f", specifier)),
  246. e = Math.max(-8, Math.min(8, Math.floor(exponent(value) / 3))) * 3,
  247. k = Math.pow(10, -e),
  248. prefix = prefixes[8 + e / 3];
  249. return function(value) {
  250. return f(k * value) + prefix;
  251. };
  252. }
  253. return {
  254. format: newFormat,
  255. formatPrefix: formatPrefix
  256. };
  257. }
  258. var locale;
  259. exports.format = void 0;
  260. exports.formatPrefix = void 0;
  261. defaultLocale({
  262. thousands: ",",
  263. grouping: [3],
  264. currency: ["$", ""]
  265. });
  266. function defaultLocale(definition) {
  267. locale = formatLocale(definition);
  268. exports.format = locale.format;
  269. exports.formatPrefix = locale.formatPrefix;
  270. return locale;
  271. }
  272. function precisionFixed(step) {
  273. return Math.max(0, -exponent(Math.abs(step)));
  274. }
  275. function precisionPrefix(step, value) {
  276. return Math.max(0, Math.max(-8, Math.min(8, Math.floor(exponent(value) / 3))) * 3 - exponent(Math.abs(step)));
  277. }
  278. function precisionRound(step, max) {
  279. step = Math.abs(step), max = Math.abs(max) - step;
  280. return Math.max(0, exponent(max) - exponent(step)) + 1;
  281. }
  282. exports.FormatSpecifier = FormatSpecifier;
  283. exports.formatDefaultLocale = defaultLocale;
  284. exports.formatLocale = formatLocale;
  285. exports.formatSpecifier = formatSpecifier;
  286. exports.precisionFixed = precisionFixed;
  287. exports.precisionPrefix = precisionPrefix;
  288. exports.precisionRound = precisionRound;
  289. Object.defineProperty(exports, '__esModule', { value: true });
  290. }));