UNPKG

12.7 kBJavaScriptView Raw
1'use strict';
2
3var zhCn = {
4 decimal: ".",
5 thousands: ",",
6 grouping: [3],
7 currency: ["¥", ""]
8};
9
10var svSe = {
11 decimal: ",",
12 thousands: "\xa0",
13 grouping: [3],
14 currency: ["", "SEK"]
15};
16
17var ruRu = {
18 decimal: ",",
19 thousands: "\xa0",
20 grouping: [3],
21 currency: ["", "\xa0руб."]
22};
23
24var ptBr = {
25 decimal: ",",
26 thousands: ".",
27 grouping: [3],
28 currency: ["R$", ""]
29};
30
31var plPl = {
32 decimal: ",",
33 thousands: ".",
34 grouping: [3],
35 currency: ["", "zł"]
36};
37
38var nlNl = {
39 decimal: ",",
40 thousands: ".",
41 grouping: [3],
42 currency: ["€\xa0", ""]
43};
44
45var mkMk = {
46 decimal: ",",
47 thousands: ".",
48 grouping: [3],
49 currency: ["", "\xa0ден."]
50};
51
52var koKr = {
53 decimal: ".",
54 thousands: ",",
55 grouping: [3],
56 currency: ["₩", ""]
57};
58
59var jaJp = {
60 decimal: ".",
61 thousands: ",",
62 grouping: [3],
63 currency: ["", "円"]
64};
65
66var itIt = {
67 decimal: ",",
68 thousands: ".",
69 grouping: [3],
70 currency: ["€", ""]
71};
72
73var huHu = {
74 decimal: ",",
75 thousands: "\xa0",
76 grouping: [3],
77 currency: ["", "\xa0Ft"]
78};
79
80var heIl = {
81 decimal: ".",
82 thousands: ",",
83 grouping: [3],
84 currency: ["₪", ""]
85};
86
87var frFr = {
88 decimal: ",",
89 thousands: ".",
90 grouping: [3],
91 currency: ["", "\xa0€"]
92};
93
94var frCa = {
95 decimal: ",",
96 thousands: "\xa0",
97 grouping: [3],
98 currency: ["", "$"]
99};
100
101var fiFi = {
102 decimal: ",",
103 thousands: "\xa0",
104 grouping: [3],
105 currency: ["", "\xa0€"]
106};
107
108var esEs = {
109 decimal: ",",
110 thousands: ".",
111 grouping: [3],
112 currency: ["", "\xa0€"]
113};
114
115var enUs = {
116 decimal: ".",
117 thousands: ",",
118 grouping: [3],
119 currency: ["$", ""]
120};
121
122var enGb = {
123 decimal: ".",
124 thousands: ",",
125 grouping: [3],
126 currency: ["£", ""]
127};
128
129var enCa = {
130 decimal: ".",
131 thousands: ",",
132 grouping: [3],
133 currency: ["$", ""]
134};
135
136var deDe = {
137 decimal: ",",
138 thousands: ".",
139 grouping: [3],
140 currency: ["", "\xa0€"]
141};
142
143var deCh = {
144 decimal: ",",
145 thousands: "'",
146 grouping: [3],
147 currency: ["", "\xa0CHF"]
148};
149
150var caEs = {
151 decimal: ",",
152 thousands: ".",
153 grouping: [3],
154 currency: ["", "\xa0€"]
155};
156
157// Computes the decimal coefficient and exponent of the specified number x with
158// significant digits p, where x is positive and p is in [1, 21] or undefined.
159// For example, formatDecimal(1.23) returns ["123", 0].
160function formatDecimal(x, p) {
161 if ((i = (x = p ? x.toExponential(p - 1) : x.toExponential()).indexOf("e")) < 0) return null; // NaN, ±Infinity
162 var i, coefficient = x.slice(0, i);
163
164 // The string returned by toExponential either has the form \d\.\d+e[-+]\d+
165 // (e.g., 1.2e+3) or the form \de[-+]\d+ (e.g., 1e+3).
166 return [
167 coefficient.length > 1 ? coefficient[0] + coefficient.slice(2) : coefficient,
168 +x.slice(i + 1)
169 ];
170};
171
172function exponent$1(x) {
173 return x = formatDecimal(Math.abs(x)), x ? x[1] : NaN;
174};
175
176function formatGroup(grouping, thousands) {
177 return function(value, width) {
178 var i = value.length,
179 t = [],
180 j = 0,
181 g = grouping[0],
182 length = 0;
183
184 while (i > 0 && g > 0) {
185 if (length + g + 1 > width) g = Math.max(1, width - length);
186 t.push(value.substring(i -= g, i + g));
187 if ((length += g + 1) > width) break;
188 g = grouping[j = (j + 1) % grouping.length];
189 }
190
191 return t.reverse().join(thousands);
192 };
193};
194
195var prefixExponent;
196
197function formatPrefixAuto(x, p) {
198 var d = formatDecimal(x, p);
199 if (!d) return x + "";
200 var coefficient = d[0],
201 exponent = d[1],
202 i = exponent - (prefixExponent = Math.max(-8, Math.min(8, Math.floor(exponent / 3))) * 3) + 1,
203 n = coefficient.length;
204 return i === n ? coefficient
205 : i > n ? coefficient + new Array(i - n + 1).join("0")
206 : i > 0 ? coefficient.slice(0, i) + "." + coefficient.slice(i)
207 : "0." + new Array(1 - i).join("0") + formatDecimal(x, Math.max(0, p + i - 1))[0]; // less than 1y!
208};
209
210function formatRounded(x, p) {
211 var d = formatDecimal(x, p);
212 if (!d) return x + "";
213 var coefficient = d[0],
214 exponent = d[1];
215 return exponent < 0 ? "0." + new Array(-exponent).join("0") + coefficient
216 : coefficient.length > exponent + 1 ? coefficient.slice(0, exponent + 1) + "." + coefficient.slice(exponent + 1)
217 : coefficient + new Array(exponent - coefficient.length + 2).join("0");
218};
219
220function formatDefault(x, p) {
221 x = x.toPrecision(p);
222
223 out: for (var n = x.length, i = 1, i0 = -1, i1; i < n; ++i) {
224 switch (x[i]) {
225 case ".": i0 = i1 = i; break;
226 case "0": if (i0 === 0) i0 = i; i1 = i; break;
227 case "e": break out;
228 default: if (i0 > 0) i0 = 0; break;
229 }
230 }
231
232 return i0 > 0 ? x.slice(0, i0) + x.slice(i1 + 1) : x;
233};
234
235var formatTypes = {
236 "": formatDefault,
237 "%": function(x, p) { return (x * 100).toFixed(p); },
238 "b": function(x) { return Math.round(x).toString(2); },
239 "c": function(x) { return x + ""; },
240 "d": function(x) { return Math.round(x).toString(10); },
241 "e": function(x, p) { return x.toExponential(p); },
242 "f": function(x, p) { return x.toFixed(p); },
243 "g": function(x, p) { return x.toPrecision(p); },
244 "o": function(x) { return Math.round(x).toString(8); },
245 "p": function(x, p) { return formatRounded(x * 100, p); },
246 "r": formatRounded,
247 "s": formatPrefixAuto,
248 "X": function(x) { return Math.round(x).toString(16).toUpperCase(); },
249 "x": function(x) { return Math.round(x).toString(16); }
250};
251
252// [[fill]align][sign][symbol][0][width][,][.precision][type]
253var re = /^(?:(.)?([<>=^]))?([+\-\( ])?([$#])?(0)?(\d+)?(,)?(\.\d+)?([a-z%])?$/i;
254
255function formatSpecifier(specifier) {
256 return new FormatSpecifier(specifier);
257};
258
259function FormatSpecifier(specifier) {
260 if (!(match = re.exec(specifier))) throw new Error("invalid format: " + specifier);
261
262 var match,
263 fill = match[1] || " ",
264 align = match[2] || ">",
265 sign = match[3] || "-",
266 symbol = match[4] || "",
267 zero = !!match[5],
268 width = match[6] && +match[6],
269 comma = !!match[7],
270 precision = match[8] && +match[8].slice(1),
271 type = match[9] || "";
272
273 // The "n" type is an alias for ",g".
274 if (type === "n") comma = true, type = "g";
275
276 // Map invalid types to the default format.
277 else if (!formatTypes[type]) type = "";
278
279 // If zero fill is specified, padding goes after sign and before digits.
280 if (zero || (fill === "0" && align === "=")) zero = true, fill = "0", align = "=";
281
282 this.fill = fill;
283 this.align = align;
284 this.sign = sign;
285 this.symbol = symbol;
286 this.zero = zero;
287 this.width = width;
288 this.comma = comma;
289 this.precision = precision;
290 this.type = type;
291}
292
293FormatSpecifier.prototype.toString = function() {
294 return this.fill
295 + this.align
296 + this.sign
297 + this.symbol
298 + (this.zero ? "0" : "")
299 + (this.width == null ? "" : Math.max(1, this.width | 0))
300 + (this.comma ? "," : "")
301 + (this.precision == null ? "" : "." + Math.max(0, this.precision | 0))
302 + this.type;
303};
304
305var prefixes = ["y","z","a","f","p","n","µ","m","","k","M","G","T","P","E","Z","Y"];
306
307function identity(x) {
308 return x;
309}
310
311function locale(locale) {
312 var group = locale.grouping && locale.thousands ? formatGroup(locale.grouping, locale.thousands) : identity,
313 currency = locale.currency,
314 decimal = locale.decimal;
315
316 function format(specifier) {
317 specifier = formatSpecifier(specifier);
318
319 var fill = specifier.fill,
320 align = specifier.align,
321 sign = specifier.sign,
322 symbol = specifier.symbol,
323 zero = specifier.zero,
324 width = specifier.width,
325 comma = specifier.comma,
326 precision = specifier.precision,
327 type = specifier.type;
328
329 // Compute the prefix and suffix.
330 // For SI-prefix, the suffix is lazily computed.
331 var prefix = symbol === "$" ? currency[0] : symbol === "#" && /[boxX]/.test(type) ? "0" + type.toLowerCase() : "",
332 suffix = symbol === "$" ? currency[1] : /[%p]/.test(type) ? "%" : "";
333
334 // What format function should we use?
335 // Is this an integer type?
336 // Can this type generate exponential notation?
337 var formatType = formatTypes[type],
338 maybeSuffix = !type || /[defgprs%]/.test(type);
339
340 // Set the default precision if not specified,
341 // or clamp the specified precision to the supported range.
342 // For significant precision, it must be in [1, 21].
343 // For fixed precision, it must be in [0, 20].
344 precision = precision == null ? (type ? 6 : 12)
345 : /[gprs]/.test(type) ? Math.max(1, Math.min(21, precision))
346 : Math.max(0, Math.min(20, precision));
347
348 return function(value) {
349 var valuePrefix = prefix,
350 valueSuffix = suffix;
351
352 if (type === "c") {
353 valueSuffix = formatType(value) + valueSuffix;
354 value = "";
355 } else {
356 value = +value;
357
358 // Convert negative to positive, and compute the prefix.
359 // Note that -0 is not less than 0, but 1 / -0 is!
360 var valueNegative = (value < 0 || 1 / value < 0) && (value *= -1, true);
361
362 // Perform the initial formatting.
363 value = formatType(value, precision);
364
365 // Compute the prefix and suffix.
366 valuePrefix = (valueNegative ? (sign === "(" ? sign : "-") : sign === "-" || sign === "(" ? "" : sign) + valuePrefix;
367 valueSuffix = valueSuffix + (type === "s" ? prefixes[8 + prefixExponent / 3] : "") + (valueNegative && sign === "(" ? ")" : "");
368
369 // Break the formatted value into the integer “value” part that can be
370 // grouped, and fractional or exponential “suffix” part that is not.
371 if (maybeSuffix) {
372 var i = -1, n = value.length, c;
373 while (++i < n) {
374 if (c = value.charCodeAt(i), 48 > c || c > 57) {
375 valueSuffix = (c === 46 ? decimal + value.slice(i + 1) : value.slice(i)) + valueSuffix;
376 value = value.slice(0, i);
377 break;
378 }
379 }
380 }
381 }
382
383 // If the fill character is not "0", grouping is applied before padding.
384 if (comma && !zero) value = group(value, Infinity);
385
386 // Compute the padding.
387 var length = valuePrefix.length + value.length + valueSuffix.length,
388 padding = length < width ? new Array(width - length + 1).join(fill) : "";
389
390 // If the fill character is "0", grouping is applied after padding.
391 if (comma && zero) value = group(padding + value, padding.length ? width - valueSuffix.length : Infinity), padding = "";
392
393 // Reconstruct the final output based on the desired alignment.
394 switch (align) {
395 case "<": return valuePrefix + value + valueSuffix + padding;
396 case "=": return valuePrefix + padding + value + valueSuffix;
397 case "^": return padding.slice(0, length = padding.length >> 1) + valuePrefix + value + valueSuffix + padding.slice(length);
398 }
399 return padding + valuePrefix + value + valueSuffix;
400 };
401 }
402
403 function formatPrefix(specifier, value) {
404 var f = format((specifier = formatSpecifier(specifier), specifier.type = "f", specifier)),
405 e = Math.max(-8, Math.min(8, Math.floor(exponent$1(value) / 3))) * 3,
406 k = Math.pow(10, -e),
407 prefix = prefixes[8 + e / 3];
408 return function(value) {
409 return f(k * value) + prefix;
410 };
411 }
412
413 return {
414 format: format,
415 formatPrefix: formatPrefix
416 };
417};
418
419function precisionRound(step, max) {
420 return Math.max(0, exponent$1(Math.abs(max)) - exponent$1(Math.abs(step))) + 1;
421};
422
423function precisionFixed(step) {
424 return Math.max(0, -exponent$1(Math.abs(step)));
425};
426
427function precisionPrefix(step, value) {
428 return Math.max(0, Math.max(-8, Math.min(8, Math.floor(exponent$1(value) / 3))) * 3 - exponent$1(Math.abs(step)));
429};
430
431var localeDefinitions = {
432 "ca-ES": caEs,
433 "de-CH": deCh,
434 "de-DE": deDe,
435 "en-CA": enCa,
436 "en-GB": enGb,
437 "en-US": enUs,
438 "es-ES": esEs,
439 "fi-FI": fiFi,
440 "fr-CA": frCa,
441 "fr-FR": frFr,
442 "he-IL": heIl,
443 "hu-HU": huHu,
444 "it-IT": itIt,
445 "ja-JP": jaJp,
446 "ko-KR": koKr,
447 "mk-MK": mkMk,
448 "nl-NL": nlNl,
449 "pl-PL": plPl,
450 "pt-BR": ptBr,
451 "ru-RU": ruRu,
452 "sv-SE": svSe,
453 "zh-CN": zhCn
454};
455
456var defaultLocale = locale(enUs);
457var format = defaultLocale.format;
458var formatPrefix = defaultLocale.formatPrefix;
459
460function localeFormat(definition) {
461 if (typeof definition === "string") {
462 if (!localeDefinitions.hasOwnProperty(definition)) return null;
463 definition = localeDefinitions[definition];
464 }
465 return locale(definition);
466};
467
468exports.format = format;
469exports.formatPrefix = formatPrefix;
470exports.localeFormat = localeFormat;
471exports.formatSpecifier = formatSpecifier;
472exports.precisionFixed = precisionFixed;
473exports.precisionPrefix = precisionPrefix;
474exports.precisionRound = precisionRound;
\No newline at end of file