1 | import exponent from "./exponent";
|
2 | import formatGroup from "./formatGroup";
|
3 | import formatNumerals from "./formatNumerals";
|
4 | import formatSpecifier from "./formatSpecifier";
|
5 | import formatTrim from "./formatTrim";
|
6 | import formatTypes from "./formatTypes";
|
7 | import {prefixExponent} from "./formatPrefixAuto";
|
8 | import identity from "./identity";
|
9 |
|
10 | var prefixes = ["y","z","a","f","p","n","µ","m","","k","M","G","T","P","E","Z","Y"];
|
11 |
|
12 | export default function(locale) {
|
13 | var group = locale.grouping && locale.thousands ? formatGroup(locale.grouping, locale.thousands) : identity,
|
14 | currency = locale.currency,
|
15 | decimal = locale.decimal,
|
16 | numerals = locale.numerals ? formatNumerals(locale.numerals) : identity,
|
17 | percent = locale.percent || "%";
|
18 |
|
19 | function newFormat(specifier) {
|
20 | specifier = formatSpecifier(specifier);
|
21 |
|
22 | var fill = specifier.fill,
|
23 | align = specifier.align,
|
24 | sign = specifier.sign,
|
25 | symbol = specifier.symbol,
|
26 | zero = specifier.zero,
|
27 | width = specifier.width,
|
28 | comma = specifier.comma,
|
29 | precision = specifier.precision,
|
30 | trim = specifier.trim,
|
31 | type = specifier.type;
|
32 |
|
33 |
|
34 | if (type === "n") comma = true, type = "g";
|
35 |
|
36 |
|
37 | else if (!formatTypes[type]) precision == null && (precision = 12), trim = true, type = "g";
|
38 |
|
39 |
|
40 | if (zero || (fill === "0" && align === "=")) zero = true, fill = "0", align = "=";
|
41 |
|
42 |
|
43 |
|
44 | var prefix = symbol === "$" ? currency[0] : symbol === "#" && /[boxX]/.test(type) ? "0" + type.toLowerCase() : "",
|
45 | suffix = symbol === "$" ? currency[1] : /[%p]/.test(type) ? percent : "";
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | var formatType = formatTypes[type],
|
51 | maybeSuffix = /[defgprs%]/.test(type);
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | precision = precision == null ? 6
|
58 | : /[gprs]/.test(type) ? Math.max(1, Math.min(21, precision))
|
59 | : Math.max(0, Math.min(20, precision));
|
60 |
|
61 | function format(value) {
|
62 | var valuePrefix = prefix,
|
63 | valueSuffix = suffix,
|
64 | i, n, c;
|
65 |
|
66 | if (type === "c") {
|
67 | valueSuffix = formatType(value) + valueSuffix;
|
68 | value = "";
|
69 | } else {
|
70 | value = +value;
|
71 |
|
72 |
|
73 | var valueNegative = value < 0;
|
74 | value = formatType(Math.abs(value), precision);
|
75 |
|
76 |
|
77 | if (trim) value = formatTrim(value);
|
78 |
|
79 |
|
80 | if (valueNegative && +value === 0) valueNegative = false;
|
81 |
|
82 |
|
83 | valuePrefix = (valueNegative ? (sign === "(" ? sign : "-") : sign === "-" || sign === "(" ? "" : sign) + valuePrefix;
|
84 | valueSuffix = (type === "s" ? prefixes[8 + prefixExponent / 3] : "") + valueSuffix + (valueNegative && sign === "(" ? ")" : "");
|
85 |
|
86 |
|
87 |
|
88 | if (maybeSuffix) {
|
89 | i = -1, n = value.length;
|
90 | while (++i < n) {
|
91 | if (c = value.charCodeAt(i), 48 > c || c > 57) {
|
92 | valueSuffix = (c === 46 ? decimal + value.slice(i + 1) : value.slice(i)) + valueSuffix;
|
93 | value = value.slice(0, i);
|
94 | break;
|
95 | }
|
96 | }
|
97 | }
|
98 | }
|
99 |
|
100 |
|
101 | if (comma && !zero) value = group(value, Infinity);
|
102 |
|
103 |
|
104 | var length = valuePrefix.length + value.length + valueSuffix.length,
|
105 | padding = length < width ? new Array(width - length + 1).join(fill) : "";
|
106 |
|
107 |
|
108 | if (comma && zero) value = group(padding + value, padding.length ? width - valueSuffix.length : Infinity), padding = "";
|
109 |
|
110 |
|
111 | switch (align) {
|
112 | case "<": value = valuePrefix + value + valueSuffix + padding; break;
|
113 | case "=": value = valuePrefix + padding + value + valueSuffix; break;
|
114 | case "^": value = padding.slice(0, length = padding.length >> 1) + valuePrefix + value + valueSuffix + padding.slice(length); break;
|
115 | default: value = padding + valuePrefix + value + valueSuffix; break;
|
116 | }
|
117 |
|
118 | return numerals(value);
|
119 | }
|
120 |
|
121 | format.toString = function() {
|
122 | return specifier + "";
|
123 | };
|
124 |
|
125 | return format;
|
126 | }
|
127 |
|
128 | function formatPrefix(specifier, value) {
|
129 | var f = newFormat((specifier = formatSpecifier(specifier), specifier.type = "f", specifier)),
|
130 | e = Math.max(-8, Math.min(8, Math.floor(exponent(value) / 3))) * 3,
|
131 | k = Math.pow(10, -e),
|
132 | prefix = prefixes[8 + e / 3];
|
133 | return function(value) {
|
134 | return f(k * value) + prefix;
|
135 | };
|
136 | }
|
137 |
|
138 | return {
|
139 | format: newFormat,
|
140 | formatPrefix: formatPrefix
|
141 | };
|
142 | }
|