Comparing version 1.0.2 to 1.1.0
@@ -1,329 +0,330 @@ | ||
// https://d3js.org/d3-format/ Version 1.0.2. Copyright 2016 Mike Bostock. | ||
// https://d3js.org/d3-format/ Version 1.1.0. Copyright 2017 Mike Bostock. | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(factory((global.d3 = global.d3 || {}))); | ||
}(this, function (exports) { 'use strict'; | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(factory((global.d3 = global.d3 || {}))); | ||
}(this, (function (exports) { 'use strict'; | ||
// Computes the decimal coefficient and exponent of the specified number x with | ||
// significant digits p, where x is positive and p is in [1, 21] or undefined. | ||
// For example, formatDecimal(1.23) returns ["123", 0]. | ||
function formatDecimal(x, p) { | ||
if ((i = (x = p ? x.toExponential(p - 1) : x.toExponential()).indexOf("e")) < 0) return null; // NaN, ±Infinity | ||
var i, coefficient = x.slice(0, i); | ||
// Computes the decimal coefficient and exponent of the specified number x with | ||
// significant digits p, where x is positive and p is in [1, 21] or undefined. | ||
// For example, formatDecimal(1.23) returns ["123", 0]. | ||
var formatDecimal = function(x, p) { | ||
if ((i = (x = p ? x.toExponential(p - 1) : x.toExponential()).indexOf("e")) < 0) return null; // NaN, ±Infinity | ||
var i, coefficient = x.slice(0, i); | ||
// The string returned by toExponential either has the form \d\.\d+e[-+]\d+ | ||
// (e.g., 1.2e+3) or the form \de[-+]\d+ (e.g., 1e+3). | ||
return [ | ||
coefficient.length > 1 ? coefficient[0] + coefficient.slice(2) : coefficient, | ||
+x.slice(i + 1) | ||
]; | ||
} | ||
// The string returned by toExponential either has the form \d\.\d+e[-+]\d+ | ||
// (e.g., 1.2e+3) or the form \de[-+]\d+ (e.g., 1e+3). | ||
return [ | ||
coefficient.length > 1 ? coefficient[0] + coefficient.slice(2) : coefficient, | ||
+x.slice(i + 1) | ||
]; | ||
}; | ||
function exponent(x) { | ||
return x = formatDecimal(Math.abs(x)), x ? x[1] : NaN; | ||
} | ||
var exponent = function(x) { | ||
return x = formatDecimal(Math.abs(x)), x ? x[1] : NaN; | ||
}; | ||
function formatGroup(grouping, thousands) { | ||
return function(value, width) { | ||
var i = value.length, | ||
t = [], | ||
j = 0, | ||
g = grouping[0], | ||
length = 0; | ||
var formatGroup = function(grouping, thousands) { | ||
return function(value, width) { | ||
var i = value.length, | ||
t = [], | ||
j = 0, | ||
g = grouping[0], | ||
length = 0; | ||
while (i > 0 && g > 0) { | ||
if (length + g + 1 > width) g = Math.max(1, width - length); | ||
t.push(value.substring(i -= g, i + g)); | ||
if ((length += g + 1) > width) break; | ||
g = grouping[j = (j + 1) % grouping.length]; | ||
} | ||
while (i > 0 && g > 0) { | ||
if (length + g + 1 > width) g = Math.max(1, width - length); | ||
t.push(value.substring(i -= g, i + g)); | ||
if ((length += g + 1) > width) break; | ||
g = grouping[j = (j + 1) % grouping.length]; | ||
} | ||
return t.reverse().join(thousands); | ||
}; | ||
} | ||
return t.reverse().join(thousands); | ||
}; | ||
}; | ||
function formatDefault(x, p) { | ||
x = x.toPrecision(p); | ||
var formatNumerals = function(numerals) { | ||
return function(value) { | ||
return value.replace(/[0-9]/g, function(i) { | ||
return numerals[+i]; | ||
}); | ||
}; | ||
}; | ||
out: for (var n = x.length, i = 1, i0 = -1, i1; i < n; ++i) { | ||
switch (x[i]) { | ||
case ".": i0 = i1 = i; break; | ||
case "0": if (i0 === 0) i0 = i; i1 = i; break; | ||
case "e": break out; | ||
default: if (i0 > 0) i0 = 0; break; | ||
} | ||
var formatDefault = function(x, p) { | ||
x = x.toPrecision(p); | ||
out: for (var n = x.length, i = 1, i0 = -1, i1; i < n; ++i) { | ||
switch (x[i]) { | ||
case ".": i0 = i1 = i; break; | ||
case "0": if (i0 === 0) i0 = i; i1 = i; break; | ||
case "e": break out; | ||
default: if (i0 > 0) i0 = 0; break; | ||
} | ||
return i0 > 0 ? x.slice(0, i0) + x.slice(i1 + 1) : x; | ||
} | ||
var prefixExponent; | ||
return i0 > 0 ? x.slice(0, i0) + x.slice(i1 + 1) : x; | ||
}; | ||
function formatPrefixAuto(x, p) { | ||
var d = formatDecimal(x, p); | ||
if (!d) return x + ""; | ||
var coefficient = d[0], | ||
exponent = d[1], | ||
i = exponent - (prefixExponent = Math.max(-8, Math.min(8, Math.floor(exponent / 3))) * 3) + 1, | ||
n = coefficient.length; | ||
return i === n ? coefficient | ||
: i > n ? coefficient + new Array(i - n + 1).join("0") | ||
: i > 0 ? coefficient.slice(0, i) + "." + coefficient.slice(i) | ||
: "0." + new Array(1 - i).join("0") + formatDecimal(x, Math.max(0, p + i - 1))[0]; // less than 1y! | ||
} | ||
var prefixExponent; | ||
function formatRounded(x, p) { | ||
var d = formatDecimal(x, p); | ||
if (!d) return x + ""; | ||
var coefficient = d[0], | ||
exponent = d[1]; | ||
return exponent < 0 ? "0." + new Array(-exponent).join("0") + coefficient | ||
: coefficient.length > exponent + 1 ? coefficient.slice(0, exponent + 1) + "." + coefficient.slice(exponent + 1) | ||
: coefficient + new Array(exponent - coefficient.length + 2).join("0"); | ||
} | ||
var formatPrefixAuto = function(x, p) { | ||
var d = formatDecimal(x, p); | ||
if (!d) return x + ""; | ||
var coefficient = d[0], | ||
exponent = d[1], | ||
i = exponent - (prefixExponent = Math.max(-8, Math.min(8, Math.floor(exponent / 3))) * 3) + 1, | ||
n = coefficient.length; | ||
return i === n ? coefficient | ||
: i > n ? coefficient + new Array(i - n + 1).join("0") | ||
: i > 0 ? coefficient.slice(0, i) + "." + coefficient.slice(i) | ||
: "0." + new Array(1 - i).join("0") + formatDecimal(x, Math.max(0, p + i - 1))[0]; // less than 1y! | ||
}; | ||
var formatTypes = { | ||
"": formatDefault, | ||
"%": function(x, p) { return (x * 100).toFixed(p); }, | ||
"b": function(x) { return Math.round(x).toString(2); }, | ||
"c": function(x) { return x + ""; }, | ||
"d": function(x) { return Math.round(x).toString(10); }, | ||
"e": function(x, p) { return x.toExponential(p); }, | ||
"f": function(x, p) { return x.toFixed(p); }, | ||
"g": function(x, p) { return x.toPrecision(p); }, | ||
"o": function(x) { return Math.round(x).toString(8); }, | ||
"p": function(x, p) { return formatRounded(x * 100, p); }, | ||
"r": formatRounded, | ||
"s": formatPrefixAuto, | ||
"X": function(x) { return Math.round(x).toString(16).toUpperCase(); }, | ||
"x": function(x) { return Math.round(x).toString(16); } | ||
}; | ||
var formatRounded = function(x, p) { | ||
var d = formatDecimal(x, p); | ||
if (!d) return x + ""; | ||
var coefficient = d[0], | ||
exponent = d[1]; | ||
return exponent < 0 ? "0." + new Array(-exponent).join("0") + coefficient | ||
: coefficient.length > exponent + 1 ? coefficient.slice(0, exponent + 1) + "." + coefficient.slice(exponent + 1) | ||
: coefficient + new Array(exponent - coefficient.length + 2).join("0"); | ||
}; | ||
// [[fill]align][sign][symbol][0][width][,][.precision][type] | ||
var re = /^(?:(.)?([<>=^]))?([+\-\( ])?([$#])?(0)?(\d+)?(,)?(\.\d+)?([a-z%])?$/i; | ||
var formatTypes = { | ||
"": formatDefault, | ||
"%": function(x, p) { return (x * 100).toFixed(p); }, | ||
"b": function(x) { return Math.round(x).toString(2); }, | ||
"c": function(x) { return x + ""; }, | ||
"d": function(x) { return Math.round(x).toString(10); }, | ||
"e": function(x, p) { return x.toExponential(p); }, | ||
"f": function(x, p) { return x.toFixed(p); }, | ||
"g": function(x, p) { return x.toPrecision(p); }, | ||
"o": function(x) { return Math.round(x).toString(8); }, | ||
"p": function(x, p) { return formatRounded(x * 100, p); }, | ||
"r": formatRounded, | ||
"s": formatPrefixAuto, | ||
"X": function(x) { return Math.round(x).toString(16).toUpperCase(); }, | ||
"x": function(x) { return Math.round(x).toString(16); } | ||
}; | ||
function formatSpecifier(specifier) { | ||
return new FormatSpecifier(specifier); | ||
} | ||
// [[fill]align][sign][symbol][0][width][,][.precision][type] | ||
var re = /^(?:(.)?([<>=^]))?([+\-\( ])?([$#])?(0)?(\d+)?(,)?(\.\d+)?([a-z%])?$/i; | ||
function FormatSpecifier(specifier) { | ||
if (!(match = re.exec(specifier))) throw new Error("invalid format: " + specifier); | ||
function formatSpecifier(specifier) { | ||
return new FormatSpecifier(specifier); | ||
} | ||
var match, | ||
fill = match[1] || " ", | ||
align = match[2] || ">", | ||
sign = match[3] || "-", | ||
symbol = match[4] || "", | ||
zero = !!match[5], | ||
width = match[6] && +match[6], | ||
comma = !!match[7], | ||
precision = match[8] && +match[8].slice(1), | ||
type = match[9] || ""; | ||
formatSpecifier.prototype = FormatSpecifier.prototype; // instanceof | ||
// The "n" type is an alias for ",g". | ||
if (type === "n") comma = true, type = "g"; | ||
function FormatSpecifier(specifier) { | ||
if (!(match = re.exec(specifier))) throw new Error("invalid format: " + specifier); | ||
// Map invalid types to the default format. | ||
else if (!formatTypes[type]) type = ""; | ||
var match, | ||
fill = match[1] || " ", | ||
align = match[2] || ">", | ||
sign = match[3] || "-", | ||
symbol = match[4] || "", | ||
zero = !!match[5], | ||
width = match[6] && +match[6], | ||
comma = !!match[7], | ||
precision = match[8] && +match[8].slice(1), | ||
type = match[9] || ""; | ||
// If zero fill is specified, padding goes after sign and before digits. | ||
if (zero || (fill === "0" && align === "=")) zero = true, fill = "0", align = "="; | ||
// The "n" type is an alias for ",g". | ||
if (type === "n") comma = true, type = "g"; | ||
this.fill = fill; | ||
this.align = align; | ||
this.sign = sign; | ||
this.symbol = symbol; | ||
this.zero = zero; | ||
this.width = width; | ||
this.comma = comma; | ||
this.precision = precision; | ||
this.type = type; | ||
} | ||
// Map invalid types to the default format. | ||
else if (!formatTypes[type]) type = ""; | ||
FormatSpecifier.prototype.toString = function() { | ||
return this.fill | ||
+ this.align | ||
+ this.sign | ||
+ this.symbol | ||
+ (this.zero ? "0" : "") | ||
+ (this.width == null ? "" : Math.max(1, this.width | 0)) | ||
+ (this.comma ? "," : "") | ||
+ (this.precision == null ? "" : "." + Math.max(0, this.precision | 0)) | ||
+ this.type; | ||
}; | ||
// If zero fill is specified, padding goes after sign and before digits. | ||
if (zero || (fill === "0" && align === "=")) zero = true, fill = "0", align = "="; | ||
var prefixes = ["y","z","a","f","p","n","µ","m","","k","M","G","T","P","E","Z","Y"]; | ||
this.fill = fill; | ||
this.align = align; | ||
this.sign = sign; | ||
this.symbol = symbol; | ||
this.zero = zero; | ||
this.width = width; | ||
this.comma = comma; | ||
this.precision = precision; | ||
this.type = type; | ||
} | ||
function identity(x) { | ||
return x; | ||
} | ||
FormatSpecifier.prototype.toString = function() { | ||
return this.fill | ||
+ this.align | ||
+ this.sign | ||
+ this.symbol | ||
+ (this.zero ? "0" : "") | ||
+ (this.width == null ? "" : Math.max(1, this.width | 0)) | ||
+ (this.comma ? "," : "") | ||
+ (this.precision == null ? "" : "." + Math.max(0, this.precision | 0)) | ||
+ this.type; | ||
}; | ||
function formatLocale(locale) { | ||
var group = locale.grouping && locale.thousands ? formatGroup(locale.grouping, locale.thousands) : identity, | ||
currency = locale.currency, | ||
decimal = locale.decimal; | ||
var identity = function(x) { | ||
return x; | ||
}; | ||
function newFormat(specifier) { | ||
specifier = formatSpecifier(specifier); | ||
var prefixes = ["y","z","a","f","p","n","µ","m","","k","M","G","T","P","E","Z","Y"]; | ||
var fill = specifier.fill, | ||
align = specifier.align, | ||
sign = specifier.sign, | ||
symbol = specifier.symbol, | ||
zero = specifier.zero, | ||
width = specifier.width, | ||
comma = specifier.comma, | ||
precision = specifier.precision, | ||
type = specifier.type; | ||
var formatLocale = function(locale) { | ||
var group = locale.grouping && locale.thousands ? formatGroup(locale.grouping, locale.thousands) : identity, | ||
currency = locale.currency, | ||
decimal = locale.decimal, | ||
numerals = locale.numerals ? formatNumerals(locale.numerals) : identity; | ||
// Compute the prefix and suffix. | ||
// For SI-prefix, the suffix is lazily computed. | ||
var prefix = symbol === "$" ? currency[0] : symbol === "#" && /[boxX]/.test(type) ? "0" + type.toLowerCase() : "", | ||
suffix = symbol === "$" ? currency[1] : /[%p]/.test(type) ? "%" : ""; | ||
function newFormat(specifier) { | ||
specifier = formatSpecifier(specifier); | ||
// What format function should we use? | ||
// Is this an integer type? | ||
// Can this type generate exponential notation? | ||
var formatType = formatTypes[type], | ||
maybeSuffix = !type || /[defgprs%]/.test(type); | ||
var fill = specifier.fill, | ||
align = specifier.align, | ||
sign = specifier.sign, | ||
symbol = specifier.symbol, | ||
zero = specifier.zero, | ||
width = specifier.width, | ||
comma = specifier.comma, | ||
precision = specifier.precision, | ||
type = specifier.type; | ||
// Set the default precision if not specified, | ||
// or clamp the specified precision to the supported range. | ||
// For significant precision, it must be in [1, 21]. | ||
// For fixed precision, it must be in [0, 20]. | ||
precision = precision == null ? (type ? 6 : 12) | ||
: /[gprs]/.test(type) ? Math.max(1, Math.min(21, precision)) | ||
: Math.max(0, Math.min(20, precision)); | ||
// Compute the prefix and suffix. | ||
// For SI-prefix, the suffix is lazily computed. | ||
var prefix = symbol === "$" ? currency[0] : symbol === "#" && /[boxX]/.test(type) ? "0" + type.toLowerCase() : "", | ||
suffix = symbol === "$" ? currency[1] : /[%p]/.test(type) ? "%" : ""; | ||
function format(value) { | ||
var valuePrefix = prefix, | ||
valueSuffix = suffix, | ||
i, n, c; | ||
// What format function should we use? | ||
// Is this an integer type? | ||
// Can this type generate exponential notation? | ||
var formatType = formatTypes[type], | ||
maybeSuffix = !type || /[defgprs%]/.test(type); | ||
if (type === "c") { | ||
valueSuffix = formatType(value) + valueSuffix; | ||
value = ""; | ||
} else { | ||
value = +value; | ||
// Set the default precision if not specified, | ||
// or clamp the specified precision to the supported range. | ||
// For significant precision, it must be in [1, 21]. | ||
// For fixed precision, it must be in [0, 20]. | ||
precision = precision == null ? (type ? 6 : 12) | ||
: /[gprs]/.test(type) ? Math.max(1, Math.min(21, precision)) | ||
: Math.max(0, Math.min(20, precision)); | ||
// Convert negative to positive, and compute the prefix. | ||
// Note that -0 is not less than 0, but 1 / -0 is! | ||
var valueNegative = (value < 0 || 1 / value < 0) && (value *= -1, true); | ||
function format(value) { | ||
var valuePrefix = prefix, | ||
valueSuffix = suffix, | ||
i, n, c; | ||
// Perform the initial formatting. | ||
value = formatType(value, precision); | ||
if (type === "c") { | ||
valueSuffix = formatType(value) + valueSuffix; | ||
value = ""; | ||
} else { | ||
value = +value; | ||
// If the original value was negative, it may be rounded to zero during | ||
// formatting; treat this as (positive) zero. | ||
if (valueNegative) { | ||
i = -1, n = value.length; | ||
valueNegative = false; | ||
while (++i < n) { | ||
if (c = value.charCodeAt(i), (48 < c && c < 58) | ||
|| (type === "x" && 96 < c && c < 103) | ||
|| (type === "X" && 64 < c && c < 71)) { | ||
valueNegative = true; | ||
break; | ||
} | ||
} | ||
} | ||
// Perform the initial formatting. | ||
var valueNegative = value < 0; | ||
value = formatType(Math.abs(value), precision); | ||
// Compute the prefix and suffix. | ||
valuePrefix = (valueNegative ? (sign === "(" ? sign : "-") : sign === "-" || sign === "(" ? "" : sign) + valuePrefix; | ||
valueSuffix = valueSuffix + (type === "s" ? prefixes[8 + prefixExponent / 3] : "") + (valueNegative && sign === "(" ? ")" : ""); | ||
// If a negative value rounds to zero during formatting, treat as positive. | ||
if (valueNegative && +value === 0) valueNegative = false; | ||
// Break the formatted value into the integer “value” part that can be | ||
// grouped, and fractional or exponential “suffix” part that is not. | ||
if (maybeSuffix) { | ||
i = -1, n = value.length; | ||
while (++i < n) { | ||
if (c = value.charCodeAt(i), 48 > c || c > 57) { | ||
valueSuffix = (c === 46 ? decimal + value.slice(i + 1) : value.slice(i)) + valueSuffix; | ||
value = value.slice(0, i); | ||
break; | ||
} | ||
// Compute the prefix and suffix. | ||
valuePrefix = (valueNegative ? (sign === "(" ? sign : "-") : sign === "-" || sign === "(" ? "" : sign) + valuePrefix; | ||
valueSuffix = valueSuffix + (type === "s" ? prefixes[8 + prefixExponent / 3] : "") + (valueNegative && sign === "(" ? ")" : ""); | ||
// Break the formatted value into the integer “value” part that can be | ||
// grouped, and fractional or exponential “suffix” part that is not. | ||
if (maybeSuffix) { | ||
i = -1, n = value.length; | ||
while (++i < n) { | ||
if (c = value.charCodeAt(i), 48 > c || c > 57) { | ||
valueSuffix = (c === 46 ? decimal + value.slice(i + 1) : value.slice(i)) + valueSuffix; | ||
value = value.slice(0, i); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
// If the fill character is not "0", grouping is applied before padding. | ||
if (comma && !zero) value = group(value, Infinity); | ||
// If the fill character is not "0", grouping is applied before padding. | ||
if (comma && !zero) value = group(value, Infinity); | ||
// Compute the padding. | ||
var length = valuePrefix.length + value.length + valueSuffix.length, | ||
padding = length < width ? new Array(width - length + 1).join(fill) : ""; | ||
// Compute the padding. | ||
var length = valuePrefix.length + value.length + valueSuffix.length, | ||
padding = length < width ? new Array(width - length + 1).join(fill) : ""; | ||
// If the fill character is "0", grouping is applied after padding. | ||
if (comma && zero) value = group(padding + value, padding.length ? width - valueSuffix.length : Infinity), padding = ""; | ||
// If the fill character is "0", grouping is applied after padding. | ||
if (comma && zero) value = group(padding + value, padding.length ? width - valueSuffix.length : Infinity), padding = ""; | ||
// Reconstruct the final output based on the desired alignment. | ||
switch (align) { | ||
case "<": return valuePrefix + value + valueSuffix + padding; | ||
case "=": return valuePrefix + padding + value + valueSuffix; | ||
case "^": return padding.slice(0, length = padding.length >> 1) + valuePrefix + value + valueSuffix + padding.slice(length); | ||
} | ||
return padding + valuePrefix + value + valueSuffix; | ||
// Reconstruct the final output based on the desired alignment. | ||
switch (align) { | ||
case "<": value = valuePrefix + value + valueSuffix + padding; break; | ||
case "=": value = valuePrefix + padding + value + valueSuffix; break; | ||
case "^": value = padding.slice(0, length = padding.length >> 1) + valuePrefix + value + valueSuffix + padding.slice(length); break; | ||
default: value = padding + valuePrefix + value + valueSuffix; break; | ||
} | ||
format.toString = function() { | ||
return specifier + ""; | ||
}; | ||
return format; | ||
return numerals(value); | ||
} | ||
function formatPrefix(specifier, value) { | ||
var f = newFormat((specifier = formatSpecifier(specifier), specifier.type = "f", specifier)), | ||
e = Math.max(-8, Math.min(8, Math.floor(exponent(value) / 3))) * 3, | ||
k = Math.pow(10, -e), | ||
prefix = prefixes[8 + e / 3]; | ||
return function(value) { | ||
return f(k * value) + prefix; | ||
}; | ||
} | ||
format.toString = function() { | ||
return specifier + ""; | ||
}; | ||
return { | ||
format: newFormat, | ||
formatPrefix: formatPrefix | ||
return format; | ||
} | ||
function formatPrefix(specifier, value) { | ||
var f = newFormat((specifier = formatSpecifier(specifier), specifier.type = "f", specifier)), | ||
e = Math.max(-8, Math.min(8, Math.floor(exponent(value) / 3))) * 3, | ||
k = Math.pow(10, -e), | ||
prefix = prefixes[8 + e / 3]; | ||
return function(value) { | ||
return f(k * value) + prefix; | ||
}; | ||
} | ||
var locale; | ||
defaultLocale({ | ||
decimal: ".", | ||
thousands: ",", | ||
grouping: [3], | ||
currency: ["$", ""] | ||
}); | ||
return { | ||
format: newFormat, | ||
formatPrefix: formatPrefix | ||
}; | ||
}; | ||
function defaultLocale(definition) { | ||
locale = formatLocale(definition); | ||
exports.format = locale.format; | ||
exports.formatPrefix = locale.formatPrefix; | ||
return locale; | ||
} | ||
var locale; | ||
function precisionFixed(step) { | ||
return Math.max(0, -exponent(Math.abs(step))); | ||
} | ||
function precisionPrefix(step, value) { | ||
return Math.max(0, Math.max(-8, Math.min(8, Math.floor(exponent(value) / 3))) * 3 - exponent(Math.abs(step))); | ||
} | ||
function precisionRound(step, max) { | ||
step = Math.abs(step), max = Math.abs(max) - step; | ||
return Math.max(0, exponent(max) - exponent(step)) + 1; | ||
} | ||
defaultLocale({ | ||
decimal: ".", | ||
thousands: ",", | ||
grouping: [3], | ||
currency: ["$", ""] | ||
}); | ||
exports.formatDefaultLocale = defaultLocale; | ||
exports.formatLocale = formatLocale; | ||
exports.formatSpecifier = formatSpecifier; | ||
exports.precisionFixed = precisionFixed; | ||
exports.precisionPrefix = precisionPrefix; | ||
exports.precisionRound = precisionRound; | ||
function defaultLocale(definition) { | ||
locale = formatLocale(definition); | ||
exports.format = locale.format; | ||
exports.formatPrefix = locale.formatPrefix; | ||
return locale; | ||
} | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
var precisionFixed = function(step) { | ||
return Math.max(0, -exponent(Math.abs(step))); | ||
}; | ||
})); | ||
var precisionPrefix = function(step, value) { | ||
return Math.max(0, Math.max(-8, Math.min(8, Math.floor(exponent(value) / 3))) * 3 - exponent(Math.abs(step))); | ||
}; | ||
var precisionRound = function(step, max) { | ||
step = Math.abs(step), max = Math.abs(max) - step; | ||
return Math.max(0, exponent(max) - exponent(step)) + 1; | ||
}; | ||
exports.formatDefaultLocale = defaultLocale; | ||
exports.formatLocale = formatLocale; | ||
exports.formatSpecifier = formatSpecifier; | ||
exports.precisionFixed = precisionFixed; | ||
exports.precisionPrefix = precisionPrefix; | ||
exports.precisionRound = precisionRound; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
}))); |
@@ -1,2 +0,2 @@ | ||
// https://d3js.org/d3-format/ Version 1.0.2. Copyright 2016 Mike Bostock. | ||
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n(t.d3=t.d3||{})}(this,function(t){"use strict";function n(t,n){if((r=(t=n?t.toExponential(n-1):t.toExponential()).indexOf("e"))<0)return null;var r,e=t.slice(0,r);return[e.length>1?e[0]+e.slice(2):e,+t.slice(r+1)]}function r(t){return t=n(Math.abs(t)),t?t[1]:NaN}function e(t,n){return function(r,e){for(var i=r.length,o=[],a=0,u=t[0],s=0;i>0&&u>0&&(s+u+1>e&&(u=Math.max(1,e-s)),o.push(r.substring(i-=u,i+u)),!((s+=u+1)>e));)u=t[a=(a+1)%t.length];return o.reverse().join(n)}}function i(t,n){t=t.toPrecision(n);t:for(var r,e=t.length,i=1,o=-1;i<e;++i)switch(t[i]){case".":o=r=i;break;case"0":0===o&&(o=i),r=i;break;case"e":break t;default:o>0&&(o=0)}return o>0?t.slice(0,o)+t.slice(r+1):t}function o(t,r){var e=n(t,r);if(!e)return t+"";var i=e[0],o=e[1],a=o-(d=3*Math.max(-8,Math.min(8,Math.floor(o/3))))+1,u=i.length;return a===u?i:a>u?i+new Array(a-u+1).join("0"):a>0?i.slice(0,a)+"."+i.slice(a):"0."+new Array(1-a).join("0")+n(t,Math.max(0,r+a-1))[0]}function a(t,r){var e=n(t,r);if(!e)return t+"";var i=e[0],o=e[1];return o<0?"0."+new Array((-o)).join("0")+i:i.length>o+1?i.slice(0,o+1)+"."+i.slice(o+1):i+new Array(o-i.length+2).join("0")}function u(t){return new s(t)}function s(t){if(!(n=M.exec(t)))throw new Error("invalid format: "+t);var n,r=n[1]||" ",e=n[2]||">",i=n[3]||"-",o=n[4]||"",a=!!n[5],u=n[6]&&+n[6],s=!!n[7],c=n[8]&&+n[8].slice(1),f=n[9]||"";"n"===f?(s=!0,f="g"):p[f]||(f=""),(a||"0"===r&&"="===e)&&(a=!0,r="0",e="="),this.fill=r,this.align=e,this.sign=i,this.symbol=o,this.zero=a,this.width=u,this.comma=s,this.precision=c,this.type=f}function c(t){return t}function f(t){function n(t){function n(t){var n,a,u,c=M,p=x;if("c"===g)p=v(t)+p,t="";else{t=+t;var w=(t<0||1/t<0)&&(t*=-1,!0);if(t=v(t,m),w)for(n=-1,a=t.length,w=!1;++n<a;)if(u=t.charCodeAt(n),48<u&&u<58||"x"===g&&96<u&&u<103||"X"===g&&64<u&&u<71){w=!0;break}if(c=(w?"("===i?i:"-":"-"===i||"("===i?"":i)+c,p=p+("s"===g?y[8+d/3]:"")+(w&&"("===i?")":""),b)for(n=-1,a=t.length;++n<a;)if(u=t.charCodeAt(n),48>u||u>57){p=(46===u?s+t.slice(n+1):t.slice(n))+p,t=t.slice(0,n);break}}l&&!f&&(t=o(t,1/0));var j=c.length+t.length+p.length,P=j<h?new Array(h-j+1).join(r):"";switch(l&&f&&(t=o(P+t,P.length?h-p.length:1/0),P=""),e){case"<":return c+t+p+P;case"=":return c+P+t+p;case"^":return P.slice(0,j=P.length>>1)+c+t+p+P.slice(j)}return P+c+t+p}t=u(t);var r=t.fill,e=t.align,i=t.sign,c=t.symbol,f=t.zero,h=t.width,l=t.comma,m=t.precision,g=t.type,M="$"===c?a[0]:"#"===c&&/[boxX]/.test(g)?"0"+g.toLowerCase():"",x="$"===c?a[1]:/[%p]/.test(g)?"%":"",v=p[g],b=!g||/[defgprs%]/.test(g);return m=null==m?g?6:12:/[gprs]/.test(g)?Math.max(1,Math.min(21,m)):Math.max(0,Math.min(20,m)),n.toString=function(){return t+""},n}function i(t,e){var i=n((t=u(t),t.type="f",t)),o=3*Math.max(-8,Math.min(8,Math.floor(r(e)/3))),a=Math.pow(10,-o),s=y[8+o/3];return function(t){return i(a*t)+s}}var o=t.grouping&&t.thousands?e(t.grouping,t.thousands):c,a=t.currency,s=t.decimal;return{format:n,formatPrefix:i}}function h(n){return x=f(n),t.format=x.format,t.formatPrefix=x.formatPrefix,x}function l(t){return Math.max(0,-r(Math.abs(t)))}function m(t,n){return Math.max(0,3*Math.max(-8,Math.min(8,Math.floor(r(n)/3)))-r(Math.abs(t)))}function g(t,n){return t=Math.abs(t),n=Math.abs(n)-t,Math.max(0,r(n)-r(t))+1}var d,p={"":i,"%":function(t,n){return(100*t).toFixed(n)},b:function(t){return Math.round(t).toString(2)},c:function(t){return t+""},d:function(t){return Math.round(t).toString(10)},e:function(t,n){return t.toExponential(n)},f:function(t,n){return t.toFixed(n)},g:function(t,n){return t.toPrecision(n)},o:function(t){return Math.round(t).toString(8)},p:function(t,n){return a(100*t,n)},r:a,s:o,X:function(t){return Math.round(t).toString(16).toUpperCase()},x:function(t){return Math.round(t).toString(16)}},M=/^(?:(.)?([<>=^]))?([+\-\( ])?([$#])?(0)?(\d+)?(,)?(\.\d+)?([a-z%])?$/i;s.prototype.toString=function(){return this.fill+this.align+this.sign+this.symbol+(this.zero?"0":"")+(null==this.width?"":Math.max(1,0|this.width))+(this.comma?",":"")+(null==this.precision?"":"."+Math.max(0,0|this.precision))+this.type};var x,y=["y","z","a","f","p","n","µ","m","","k","M","G","T","P","E","Z","Y"];h({decimal:".",thousands:",",grouping:[3],currency:["$",""]}),t.formatDefaultLocale=h,t.formatLocale=f,t.formatSpecifier=u,t.precisionFixed=l,t.precisionPrefix=m,t.precisionRound=g,Object.defineProperty(t,"__esModule",{value:!0})}); | ||
// https://d3js.org/d3-format/ Version 1.1.0. Copyright 2017 Mike Bostock. | ||
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n(t.d3=t.d3||{})}(this,function(t){"use strict";function n(t){return new r(t)}function r(t){if(!(n=m.exec(t)))throw new Error("invalid format: "+t);var n,r=n[1]||" ",e=n[2]||">",i=n[3]||"-",o=n[4]||"",a=!!n[5],u=n[6]&&+n[6],s=!!n[7],c=n[8]&&+n[8].slice(1),f=n[9]||"";"n"===f?(s=!0,f="g"):l[f]||(f=""),(a||"0"===r&&"="===e)&&(a=!0,r="0",e="="),this.fill=r,this.align=e,this.sign=i,this.symbol=o,this.zero=a,this.width=u,this.comma=s,this.precision=c,this.type=f}function e(n){return p=M(n),t.format=p.format,t.formatPrefix=p.formatPrefix,p}var i,o=function(t,n){if((r=(t=n?t.toExponential(n-1):t.toExponential()).indexOf("e"))<0)return null;var r,e=t.slice(0,r);return[e.length>1?e[0]+e.slice(2):e,+t.slice(r+1)]},a=function(t){return t=o(Math.abs(t)),t?t[1]:NaN},u=function(t,n){return function(r,e){for(var i=r.length,o=[],a=0,u=t[0],s=0;i>0&&u>0&&(s+u+1>e&&(u=Math.max(1,e-s)),o.push(r.substring(i-=u,i+u)),!((s+=u+1)>e));)u=t[a=(a+1)%t.length];return o.reverse().join(n)}},s=function(t){return function(n){return n.replace(/[0-9]/g,function(n){return t[+n]})}},c=function(t,n){t=t.toPrecision(n);t:for(var r,e=t.length,i=1,o=-1;i<e;++i)switch(t[i]){case".":o=r=i;break;case"0":0===o&&(o=i),r=i;break;case"e":break t;default:o>0&&(o=0)}return o>0?t.slice(0,o)+t.slice(r+1):t},f=function(t,n){var r=o(t,n);if(!r)return t+"";var e=r[0],a=r[1],u=a-(i=3*Math.max(-8,Math.min(8,Math.floor(a/3))))+1,s=e.length;return u===s?e:u>s?e+new Array(u-s+1).join("0"):u>0?e.slice(0,u)+"."+e.slice(u):"0."+new Array(1-u).join("0")+o(t,Math.max(0,n+u-1))[0]},h=function(t,n){var r=o(t,n);if(!r)return t+"";var e=r[0],i=r[1];return i<0?"0."+new Array(-i).join("0")+e:e.length>i+1?e.slice(0,i+1)+"."+e.slice(i+1):e+new Array(i-e.length+2).join("0")},l={"":c,"%":function(t,n){return(100*t).toFixed(n)},b:function(t){return Math.round(t).toString(2)},c:function(t){return t+""},d:function(t){return Math.round(t).toString(10)},e:function(t,n){return t.toExponential(n)},f:function(t,n){return t.toFixed(n)},g:function(t,n){return t.toPrecision(n)},o:function(t){return Math.round(t).toString(8)},p:function(t,n){return h(100*t,n)},r:h,s:f,X:function(t){return Math.round(t).toString(16).toUpperCase()},x:function(t){return Math.round(t).toString(16)}},m=/^(?:(.)?([<>=^]))?([+\-\( ])?([$#])?(0)?(\d+)?(,)?(\.\d+)?([a-z%])?$/i;n.prototype=r.prototype,r.prototype.toString=function(){return this.fill+this.align+this.sign+this.symbol+(this.zero?"0":"")+(null==this.width?"":Math.max(1,0|this.width))+(this.comma?",":"")+(null==this.precision?"":"."+Math.max(0,0|this.precision))+this.type};var p,g=function(t){return t},d=["y","z","a","f","p","n","µ","m","","k","M","G","T","P","E","Z","Y"],M=function(t){function r(t){function r(t){var n,r,s,c=y,l=b;if("c"===x)l=v(t)+l,t="";else{t=+t;var j=t<0;if(t=v(Math.abs(t),M),j&&0===+t&&(j=!1),c=(j?"("===u?u:"-":"-"===u||"("===u?"":u)+c,l=l+("s"===x?d[8+i/3]:"")+(j&&"("===u?")":""),w)for(n=-1,r=t.length;++n<r;)if(s=t.charCodeAt(n),48>s||s>57){l=(46===s?f+t.slice(n+1):t.slice(n))+l,t=t.slice(0,n);break}}g&&!m&&(t=o(t,1/0));var k=c.length+t.length+l.length,P=k<p?new Array(p-k+1).join(e):"";switch(g&&m&&(t=o(P+t,P.length?p-l.length:1/0),P=""),a){case"<":t=c+t+l+P;break;case"=":t=c+P+t+l;break;case"^":t=P.slice(0,k=P.length>>1)+c+t+l+P.slice(k);break;default:t=P+c+t+l}return h(t)}t=n(t);var e=t.fill,a=t.align,u=t.sign,s=t.symbol,m=t.zero,p=t.width,g=t.comma,M=t.precision,x=t.type,y="$"===s?c[0]:"#"===s&&/[boxX]/.test(x)?"0"+x.toLowerCase():"",b="$"===s?c[1]:/[%p]/.test(x)?"%":"",v=l[x],w=!x||/[defgprs%]/.test(x);return M=null==M?x?6:12:/[gprs]/.test(x)?Math.max(1,Math.min(21,M)):Math.max(0,Math.min(20,M)),r.toString=function(){return t+""},r}function e(t,e){var i=r((t=n(t),t.type="f",t)),o=3*Math.max(-8,Math.min(8,Math.floor(a(e)/3))),u=Math.pow(10,-o),s=d[8+o/3];return function(t){return i(u*t)+s}}var o=t.grouping&&t.thousands?u(t.grouping,t.thousands):g,c=t.currency,f=t.decimal,h=t.numerals?s(t.numerals):g;return{format:r,formatPrefix:e}};e({decimal:".",thousands:",",grouping:[3],currency:["$",""]});var x=function(t){return Math.max(0,-a(Math.abs(t)))},y=function(t,n){return Math.max(0,3*Math.max(-8,Math.min(8,Math.floor(a(n)/3)))-a(Math.abs(t)))},b=function(t,n){return t=Math.abs(t),n=Math.abs(n)-t,Math.max(0,a(n)-a(t))+1};t.formatDefaultLocale=e,t.formatLocale=M,t.formatSpecifier=n,t.precisionFixed=x,t.precisionPrefix=y,t.precisionRound=b,Object.defineProperty(t,"__esModule",{value:!0})}); |
{ | ||
"name": "d3-format", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Format numbers for human consumption.", | ||
@@ -28,9 +28,9 @@ "keywords": [ | ||
"prepublish": "npm run test && uglifyjs --preamble \"$(preamble)\" build/d3-format.js -c -m -o build/d3-format.min.js", | ||
"postpublish": "VERSION=`node -e 'console.log(require(\"./package.json\").version)'`; git push && git push --tags && cd ../d3.github.com && git pull && cp ../d3-format/build/d3-format.js d3-format.v1.js && cp ../d3-format/build/d3-format.min.js d3-format.v1.min.js && git add d3-format.v1.js d3-format.v1.min.js && git commit -m \"d3-format ${VERSION}\" && git push && cd - && zip -j build/d3-format.zip -- LICENSE README.md build/d3-format.js build/d3-format.min.js" | ||
"postpublish": "git push && git push --tags && cd ../d3.github.com && git pull && cp ../d3-format/build/d3-format.js d3-format.v1.js && cp ../d3-format/build/d3-format.min.js d3-format.v1.min.js && git add d3-format.v1.js d3-format.v1.min.js && git commit -m \"d3-format ${npm_package_version}\" && git push && cd - && zip -j build/d3-format.zip -- LICENSE README.md build/d3-format.js build/d3-format.min.js" | ||
}, | ||
"devDependencies": { | ||
"d3-queue": "3", | ||
"eslint": "2", | ||
"eslint": "3", | ||
"package-preamble": "0.0", | ||
"rollup": "0.34", | ||
"rollup": "0.41", | ||
"tape": "4", | ||
@@ -37,0 +37,0 @@ "uglify-js": "2" |
@@ -81,2 +81,16 @@ # d3-format | ||
Locale files are hosted on [unpkg](https://unpkg.com/) and can be loaded using [d3.json](https://github.com/d3/d3-request/blob/master/README.md#json). For example, to set Russian as the default locale: | ||
```js | ||
d3.json("https://unpkg.com/d3-format@1/locale/ru-RU.json", function(error, locale) { | ||
if (error) throw error; | ||
d3.formatDefaultLocale(locale); | ||
var format = d3.format("$,"); | ||
console.log(format(1234.56)); // 1 234,56 руб. | ||
}); | ||
``` | ||
[Try d3-format in your browser.](https://tonicdev.com/npm/d3-format) | ||
@@ -86,11 +100,11 @@ | ||
<a name="format" href="#format">#</a> d3.<b>format</b>(<i>specifier</i>) | ||
<a name="format" href="#format">#</a> d3.<b>format</b>(<i>specifier</i>) [<>](https://github.com/d3/d3-format/blob/master/src/defaultLocale.js#L4 "Source") | ||
An alias for [*locale*.format](#locale_format) on the [default locale](#formatDefaultLocale). | ||
<a name="formatPrefix" href="#formatPrefix">#</a> d3.<b>formatPrefix</b>(<i>specifier</i>, <i>value</i>) | ||
<a name="formatPrefix" href="#formatPrefix">#</a> d3.<b>formatPrefix</b>(<i>specifier</i>, <i>value</i>) [<>](https://github.com/d3/d3-format/blob/master/src/defaultLocale.js#L5 "Source") | ||
An alias for [*locale*.formatPrefix](#locale_formatPrefix) on the [default locale](#formatDefaultLocale). | ||
<a name="locale_format" href="#locale_format">#</a> <i>locale</i>.<b>format</b>(<i>specifier</i>) | ||
<a name="locale_format" href="#locale_format">#</a> <i>locale</i>.<b>format</b>(<i>specifier</i>) [<>](https://github.com/d3/d3-format/blob/master/src/locale.js#L18 "Source") | ||
@@ -152,3 +166,3 @@ Returns a new format function for the given string *specifier*. The returned function takes a number as the only argument, and returns a string representing the formatted number. The general form of a specifier is: | ||
<a name="locale_formatPrefix" href="#locale_formatPrefix">#</a> <i>locale</i>.<b>formatPrefix</b>(<i>specifier</i>, <i>value</i>) | ||
<a name="locale_formatPrefix" href="#locale_formatPrefix">#</a> <i>locale</i>.<b>formatPrefix</b>(<i>specifier</i>, <i>value</i>) [<>](https://github.com/d3/d3-format/blob/master/src/locale.js#L127 "Source") | ||
@@ -185,3 +199,3 @@ Equivalent to [*locale*.format](#locale_format), except the returned function will convert values to the units of the appropriate [SI prefix](https://en.wikipedia.org/wiki/Metric_prefix#List_of_SI_prefixes) for the specified numeric reference *value* before formatting in fixed point notation. The following prefixes are supported: | ||
<a name="formatSpecifier" href="#formatSpecifier">#</a> d3.<b>formatSpecifier</b>(<i>specifier</i>) | ||
<a name="formatSpecifier" href="#formatSpecifier">#</a> d3.<b>formatSpecifier</b>(<i>specifier</i>) [<>](https://github.com/d3/d3-format/blob/master/src/formatSpecifier.js "Source") | ||
@@ -213,3 +227,3 @@ Parses the specified *specifier*, returning an object with exposed fields that correspond to the [format specification mini-language](#locale_format) and a toString method that reconstructs the specifier. For example, `formatSpecifier("s")` returns: | ||
<a name="precisionFixed" href="#precisionFixed">#</a> d3.<b>precisionFixed</b>(<i>step</i>) | ||
<a name="precisionFixed" href="#precisionFixed">#</a> d3.<b>precisionFixed</b>(<i>step</i>) [<>](https://github.com/d3/d3-format/blob/master/src/precisionFixed.js "Source") | ||
@@ -246,3 +260,3 @@ Returns a suggested decimal precision for fixed point notation given the specified numeric *step* value. The *step* represents the minimum absolute difference between values that will be formatted. (This assumes that the values to be formatted are also multiples of *step*.) For example, given the numbers 1, 1.5, and 2, the *step* should be 0.5 and the suggested precision is 1: | ||
<a name="precisionPrefix" href="#precisionPrefix">#</a> d3.<b>precisionPrefix</b>(<i>step</i>, <i>value</i>) | ||
<a name="precisionPrefix" href="#precisionPrefix">#</a> d3.<b>precisionPrefix</b>(<i>step</i>, <i>value</i>) [<>](https://github.com/d3/d3-format/blob/master/src/precisionPrefix.js "Source") | ||
@@ -259,3 +273,3 @@ Returns a suggested decimal precision for use with [*locale*.formatPrefix](#locale_formatPrefix) given the specified numeric *step* and reference *value*. The *step* represents the minimum absolute difference between values that will be formatted, and *value* determines which SI prefix will be used. (This assumes that the values to be formatted are also multiples of *step*.) For example, given the numbers 1.1e6, 1.2e6, and 1.3e6, the *step* should be 1e5, the *value* could be 1.3e6, and the suggested precision is 1: | ||
<a name="precisionRound" href="#precisionRound">#</a> d3.<b>precisionRound</b>(<i>step</i>, <i>max</i>) | ||
<a name="precisionRound" href="#precisionRound">#</a> d3.<b>precisionRound</b>(<i>step</i>, <i>max</i>) [<>](https://github.com/d3/d3-format/blob/master/src/precisionRound.js "Source") | ||
@@ -293,3 +307,3 @@ Returns a suggested decimal precision for format types that round to significant digits given the specified numeric *step* and *max* values. The *step* represents the minimum absolute difference between values that will be formatted, and the *max* represents the largest absolute value that will be formatted. (This assumes that the values to be formatted are also multiples of *step*.) For example, given the numbers 0.99, 1.0, and 1.01, the *step* should be 0.01, the *max* should be 1.01, and the suggested precision is 3: | ||
<a name="formatLocale" href="#formatLocale">#</a> d3.<b>formatLocale</b>(<i>definition</i>) | ||
<a name="formatLocale" href="#formatLocale">#</a> d3.<b>formatLocale</b>(<i>definition</i>) [<>](https://github.com/d3/d3-format/blob/master/src/locale.js "Source") | ||
@@ -305,4 +319,4 @@ Returns a *locale* object for the specified *definition* with [*locale*.format](#locale_format) and [*locale*.formatPrefix](#locale_formatPrefix) methods. The *definition* must include the following properties: | ||
<a name="formatDefaultLocale" href="#formatDefaultLocale">#</a> d3.<b>formatDefaultLocale</b>(<i>definition</i>) | ||
<a name="formatDefaultLocale" href="#formatDefaultLocale">#</a> d3.<b>formatDefaultLocale</b>(<i>definition</i>) [<>](https://github.com/d3/d3-format/blob/master/src/defaultLocale.js "Source") | ||
Equivalent to [d3.formatLocale](#formatLocale), except it also redefines [d3.format](#format) and [d3.formatPrefix](#formatPrefix) to the new locale’s [*locale*.format](#locale_format) and [*locale*.formatPrefix](#locale_formatPrefix). If you do not set a default locale, it defaults to [U.S. English](https://github.com/d3/d3-format/blob/master/locale/en-US.json). |
@@ -6,6 +6,8 @@ import formatTypes from "./formatTypes"; | ||
export default function(specifier) { | ||
export default function formatSpecifier(specifier) { | ||
return new FormatSpecifier(specifier); | ||
} | ||
formatSpecifier.prototype = FormatSpecifier.prototype; // instanceof | ||
function FormatSpecifier(specifier) { | ||
@@ -12,0 +14,0 @@ if (!(match = re.exec(specifier))) throw new Error("invalid format: " + specifier); |
import exponent from "./exponent"; | ||
import formatGroup from "./formatGroup"; | ||
import formatNumerals from "./formatNumerals"; | ||
import formatSpecifier from "./formatSpecifier"; | ||
import formatTypes from "./formatTypes"; | ||
import {prefixExponent} from "./formatPrefixAuto"; | ||
import identity from "./identity"; | ||
var prefixes = ["y","z","a","f","p","n","µ","m","","k","M","G","T","P","E","Z","Y"]; | ||
function identity(x) { | ||
return x; | ||
} | ||
export default function(locale) { | ||
var group = locale.grouping && locale.thousands ? formatGroup(locale.grouping, locale.thousands) : identity, | ||
currency = locale.currency, | ||
decimal = locale.decimal; | ||
decimal = locale.decimal, | ||
numerals = locale.numerals ? formatNumerals(locale.numerals) : identity; | ||
@@ -61,23 +60,8 @@ function newFormat(specifier) { | ||
// Convert negative to positive, and compute the prefix. | ||
// Note that -0 is not less than 0, but 1 / -0 is! | ||
var valueNegative = (value < 0 || 1 / value < 0) && (value *= -1, true); | ||
// Perform the initial formatting. | ||
value = formatType(value, precision); | ||
var valueNegative = value < 0; | ||
value = formatType(Math.abs(value), precision); | ||
// If the original value was negative, it may be rounded to zero during | ||
// formatting; treat this as (positive) zero. | ||
if (valueNegative) { | ||
i = -1, n = value.length; | ||
valueNegative = false; | ||
while (++i < n) { | ||
if (c = value.charCodeAt(i), (48 < c && c < 58) | ||
|| (type === "x" && 96 < c && c < 103) | ||
|| (type === "X" && 64 < c && c < 71)) { | ||
valueNegative = true; | ||
break; | ||
} | ||
} | ||
} | ||
// If a negative value rounds to zero during formatting, treat as positive. | ||
if (valueNegative && +value === 0) valueNegative = false; | ||
@@ -114,7 +98,9 @@ // Compute the prefix and suffix. | ||
switch (align) { | ||
case "<": return valuePrefix + value + valueSuffix + padding; | ||
case "=": return valuePrefix + padding + value + valueSuffix; | ||
case "^": return padding.slice(0, length = padding.length >> 1) + valuePrefix + value + valueSuffix + padding.slice(length); | ||
case "<": value = valuePrefix + value + valueSuffix + padding; break; | ||
case "=": value = valuePrefix + padding + value + valueSuffix; break; | ||
case "^": value = padding.slice(0, length = padding.length >> 1) + valuePrefix + value + valueSuffix + padding.slice(length); break; | ||
default: value = padding + valuePrefix + value + valueSuffix; break; | ||
} | ||
return padding + valuePrefix + value + valueSuffix; | ||
return numerals(value); | ||
} | ||
@@ -121,0 +107,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
53505
77
904
314