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