UNPKG

13.2 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(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 // If the original value was negative, it may be rounded to zero during
366 // formatting; treat this as (positive) zero.
367 if (valueNegative) {
368 var i = -1, n = value.length, c;
369 valueNegative = false;
370 while (++i < n) {
371 if (c = value.charCodeAt(i), (48 < c && c < 58)
372 || (type === "x" && 96 < c && c < 103)
373 || (type === "X" && 64 < c && c < 71)) {
374 valueNegative = true;
375 break;
376 }
377 }
378 }
379
380 // Compute the prefix and suffix.
381 valuePrefix = (valueNegative ? (sign === "(" ? sign : "-") : sign === "-" || sign === "(" ? "" : sign) + valuePrefix;
382 valueSuffix = valueSuffix + (type === "s" ? prefixes[8 + prefixExponent / 3] : "") + (valueNegative && sign === "(" ? ")" : "");
383
384 // Break the formatted value into the integer “value” part that can be
385 // grouped, and fractional or exponential “suffix” part that is not.
386 if (maybeSuffix) {
387 var i = -1, n = value.length, c;
388 while (++i < n) {
389 if (c = value.charCodeAt(i), 48 > c || c > 57) {
390 valueSuffix = (c === 46 ? decimal + value.slice(i + 1) : value.slice(i)) + valueSuffix;
391 value = value.slice(0, i);
392 break;
393 }
394 }
395 }
396 }
397
398 // If the fill character is not "0", grouping is applied before padding.
399 if (comma && !zero) value = group(value, Infinity);
400
401 // Compute the padding.
402 var length = valuePrefix.length + value.length + valueSuffix.length,
403 padding = length < width ? new Array(width - length + 1).join(fill) : "";
404
405 // If the fill character is "0", grouping is applied after padding.
406 if (comma && zero) value = group(padding + value, padding.length ? width - valueSuffix.length : Infinity), padding = "";
407
408 // Reconstruct the final output based on the desired alignment.
409 switch (align) {
410 case "<": return valuePrefix + value + valueSuffix + padding;
411 case "=": return valuePrefix + padding + value + valueSuffix;
412 case "^": return padding.slice(0, length = padding.length >> 1) + valuePrefix + value + valueSuffix + padding.slice(length);
413 }
414 return padding + valuePrefix + value + valueSuffix;
415 };
416 }
417
418 function formatPrefix(specifier, value) {
419 var f = format((specifier = formatSpecifier(specifier), specifier.type = "f", specifier)),
420 e = Math.max(-8, Math.min(8, Math.floor(exponent(value) / 3))) * 3,
421 k = Math.pow(10, -e),
422 prefix = prefixes[8 + e / 3];
423 return function(value) {
424 return f(k * value) + prefix;
425 };
426 }
427
428 return {
429 format: format,
430 formatPrefix: formatPrefix
431 };
432};
433
434function precisionFixed(step) {
435 return Math.max(0, -exponent(Math.abs(step)));
436};
437
438function precisionPrefix(step, value) {
439 return Math.max(0, Math.max(-8, Math.min(8, Math.floor(exponent(value) / 3))) * 3 - exponent(Math.abs(step)));
440};
441
442function precisionRound(step, max) {
443 return Math.max(0, exponent(Math.abs(max)) - exponent(Math.abs(step))) + 1;
444};
445
446var localeDefinitions = {
447 "ca-ES": caEs,
448 "de-CH": deCh,
449 "de-DE": deDe,
450 "en-CA": enCa,
451 "en-GB": enGb,
452 "en-US": enUs,
453 "es-ES": esEs,
454 "fi-FI": fiFi,
455 "fr-CA": frCa,
456 "fr-FR": frFr,
457 "he-IL": heIl,
458 "hu-HU": huHu,
459 "it-IT": itIt,
460 "ja-JP": jaJp,
461 "ko-KR": koKr,
462 "mk-MK": mkMk,
463 "nl-NL": nlNl,
464 "pl-PL": plPl,
465 "pt-BR": ptBr,
466 "ru-RU": ruRu,
467 "sv-SE": svSe,
468 "zh-CN": zhCn
469};
470
471var defaultLocale = locale(enUs);
472var format = defaultLocale.format;
473var formatPrefix = defaultLocale.formatPrefix;
474
475function localeFormat(definition) {
476 if (typeof definition === "string") {
477 if (!localeDefinitions.hasOwnProperty(definition)) return null;
478 definition = localeDefinitions[definition];
479 }
480 return locale(definition);
481};
482
483exports.format = format;
484exports.formatPrefix = formatPrefix;
485exports.localeFormat = localeFormat;
486exports.formatSpecifier = formatSpecifier;
487exports.precisionFixed = precisionFixed;
488exports.precisionPrefix = precisionPrefix;
489exports.precisionRound = precisionRound;