UNPKG

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