| 1 | import exponent from "./exponent.js";
|
|---|
| 2 | import formatGroup from "./formatGroup.js";
|
|---|
| 3 | import formatNumerals from "./formatNumerals.js";
|
|---|
| 4 | import formatSpecifier from "./formatSpecifier.js";
|
|---|
| 5 | import formatTrim from "./formatTrim.js";
|
|---|
| 6 | import formatTypes from "./formatTypes.js";
|
|---|
| 7 | import {prefixExponent} from "./formatPrefixAuto.js";
|
|---|
| 8 | import identity from "./identity.js";
|
|---|
| 9 |
|
|---|
| 10 | var map = Array.prototype.map,
|
|---|
| 11 | prefixes = ["y","z","a","f","p","n","µ","m","","k","M","G","T","P","E","Z","Y"];
|
|---|
| 12 |
|
|---|
| 13 | export default function(locale) {
|
|---|
| 14 | var group = locale.grouping === undefined || locale.thousands === undefined ? identity : formatGroup(map.call(locale.grouping, Number), locale.thousands + ""),
|
|---|
| 15 | currencyPrefix = locale.currency === undefined ? "" : locale.currency[0] + "",
|
|---|
| 16 | currencySuffix = locale.currency === undefined ? "" : locale.currency[1] + "",
|
|---|
| 17 | decimal = locale.decimal === undefined ? "." : locale.decimal + "",
|
|---|
| 18 | numerals = locale.numerals === undefined ? identity : formatNumerals(map.call(locale.numerals, String)),
|
|---|
| 19 | percent = locale.percent === undefined ? "%" : locale.percent + "",
|
|---|
| 20 | minus = locale.minus === undefined ? "−" : locale.minus + "",
|
|---|
| 21 | nan = locale.nan === undefined ? "NaN" : locale.nan + "";
|
|---|
| 22 |
|
|---|
| 23 | function newFormat(specifier, options) {
|
|---|
| 24 | specifier = formatSpecifier(specifier);
|
|---|
| 25 |
|
|---|
| 26 | var fill = specifier.fill,
|
|---|
| 27 | align = specifier.align,
|
|---|
| 28 | sign = specifier.sign,
|
|---|
| 29 | symbol = specifier.symbol,
|
|---|
| 30 | zero = specifier.zero,
|
|---|
| 31 | width = specifier.width,
|
|---|
| 32 | comma = specifier.comma,
|
|---|
| 33 | precision = specifier.precision,
|
|---|
| 34 | trim = specifier.trim,
|
|---|
| 35 | type = specifier.type;
|
|---|
| 36 |
|
|---|
| 37 | // The "n" type is an alias for ",g".
|
|---|
| 38 | if (type === "n") comma = true, type = "g";
|
|---|
| 39 |
|
|---|
| 40 | // The "" type, and any invalid type, is an alias for ".12~g".
|
|---|
| 41 | else if (!formatTypes[type]) precision === undefined && (precision = 12), trim = true, type = "g";
|
|---|
| 42 |
|
|---|
| 43 | // If zero fill is specified, padding goes after sign and before digits.
|
|---|
| 44 | if (zero || (fill === "0" && align === "=")) zero = true, fill = "0", align = "=";
|
|---|
| 45 |
|
|---|
| 46 | // Compute the prefix and suffix.
|
|---|
| 47 | // For SI-prefix, the suffix is lazily computed.
|
|---|
| 48 | var prefix = (options && options.prefix !== undefined ? options.prefix : "") + (symbol === "$" ? currencyPrefix : symbol === "#" && /[boxX]/.test(type) ? "0" + type.toLowerCase() : ""),
|
|---|
| 49 | suffix = (symbol === "$" ? currencySuffix : /[%p]/.test(type) ? percent : "") + (options && options.suffix !== undefined ? options.suffix : "");
|
|---|
| 50 |
|
|---|
| 51 | // What format function should we use?
|
|---|
| 52 | // Is this an integer type?
|
|---|
| 53 | // Can this type generate exponential notation?
|
|---|
| 54 | var formatType = formatTypes[type],
|
|---|
| 55 | maybeSuffix = /[defgprs%]/.test(type);
|
|---|
| 56 |
|
|---|
| 57 | // Set the default precision if not specified,
|
|---|
| 58 | // or clamp the specified precision to the supported range.
|
|---|
| 59 | // For significant precision, it must be in [1, 21].
|
|---|
| 60 | // For fixed precision, it must be in [0, 20].
|
|---|
| 61 | precision = precision === undefined ? 6
|
|---|
| 62 | : /[gprs]/.test(type) ? Math.max(1, Math.min(21, precision))
|
|---|
| 63 | : Math.max(0, Math.min(20, precision));
|
|---|
| 64 |
|
|---|
| 65 | function format(value) {
|
|---|
| 66 | var valuePrefix = prefix,
|
|---|
| 67 | valueSuffix = suffix,
|
|---|
| 68 | i, n, c;
|
|---|
| 69 |
|
|---|
| 70 | if (type === "c") {
|
|---|
| 71 | valueSuffix = formatType(value) + valueSuffix;
|
|---|
| 72 | value = "";
|
|---|
| 73 | } else {
|
|---|
| 74 | value = +value;
|
|---|
| 75 |
|
|---|
| 76 | // Determine the sign. -0 is not less than 0, but 1 / -0 is!
|
|---|
| 77 | var valueNegative = value < 0 || 1 / value < 0;
|
|---|
| 78 |
|
|---|
| 79 | // Perform the initial formatting.
|
|---|
| 80 | value = isNaN(value) ? nan : formatType(Math.abs(value), precision);
|
|---|
| 81 |
|
|---|
| 82 | // Trim insignificant zeros.
|
|---|
| 83 | if (trim) value = formatTrim(value);
|
|---|
| 84 |
|
|---|
| 85 | // If a negative value rounds to zero after formatting, and no explicit positive sign is requested, hide the sign.
|
|---|
| 86 | if (valueNegative && +value === 0 && sign !== "+") valueNegative = false;
|
|---|
| 87 |
|
|---|
| 88 | // Compute the prefix and suffix.
|
|---|
| 89 | valuePrefix = (valueNegative ? (sign === "(" ? sign : minus) : sign === "-" || sign === "(" ? "" : sign) + valuePrefix;
|
|---|
| 90 | valueSuffix = (type === "s" && !isNaN(value) && prefixExponent !== undefined ? prefixes[8 + prefixExponent / 3] : "") + valueSuffix + (valueNegative && sign === "(" ? ")" : "");
|
|---|
| 91 |
|
|---|
| 92 | // Break the formatted value into the integer “value” part that can be
|
|---|
| 93 | // grouped, and fractional or exponential “suffix” part that is not.
|
|---|
| 94 | if (maybeSuffix) {
|
|---|
| 95 | i = -1, n = value.length;
|
|---|
| 96 | while (++i < n) {
|
|---|
| 97 | if (c = value.charCodeAt(i), 48 > c || c > 57) {
|
|---|
| 98 | valueSuffix = (c === 46 ? decimal + value.slice(i + 1) : value.slice(i)) + valueSuffix;
|
|---|
| 99 | value = value.slice(0, i);
|
|---|
| 100 | break;
|
|---|
| 101 | }
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | // If the fill character is not "0", grouping is applied before padding.
|
|---|
| 107 | if (comma && !zero) value = group(value, Infinity);
|
|---|
| 108 |
|
|---|
| 109 | // Compute the padding.
|
|---|
| 110 | var length = valuePrefix.length + value.length + valueSuffix.length,
|
|---|
| 111 | padding = length < width ? new Array(width - length + 1).join(fill) : "";
|
|---|
| 112 |
|
|---|
| 113 | // If the fill character is "0", grouping is applied after padding.
|
|---|
| 114 | if (comma && zero) value = group(padding + value, padding.length ? width - valueSuffix.length : Infinity), padding = "";
|
|---|
| 115 |
|
|---|
| 116 | // Reconstruct the final output based on the desired alignment.
|
|---|
| 117 | switch (align) {
|
|---|
| 118 | case "<": value = valuePrefix + value + valueSuffix + padding; break;
|
|---|
| 119 | case "=": value = valuePrefix + padding + value + valueSuffix; break;
|
|---|
| 120 | case "^": value = padding.slice(0, length = padding.length >> 1) + valuePrefix + value + valueSuffix + padding.slice(length); break;
|
|---|
| 121 | default: value = padding + valuePrefix + value + valueSuffix; break;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | return numerals(value);
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | format.toString = function() {
|
|---|
| 128 | return specifier + "";
|
|---|
| 129 | };
|
|---|
| 130 |
|
|---|
| 131 | return format;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | function formatPrefix(specifier, value) {
|
|---|
| 135 | var e = Math.max(-8, Math.min(8, Math.floor(exponent(value) / 3))) * 3,
|
|---|
| 136 | k = Math.pow(10, -e),
|
|---|
| 137 | f = newFormat((specifier = formatSpecifier(specifier), specifier.type = "f", specifier), {suffix: prefixes[8 + e / 3]});
|
|---|
| 138 | return function(value) {
|
|---|
| 139 | return f(k * value);
|
|---|
| 140 | };
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | return {
|
|---|
| 144 | format: newFormat,
|
|---|
| 145 | formatPrefix: formatPrefix
|
|---|
| 146 | };
|
|---|
| 147 | }
|
|---|