MCPcopy Create free account
hub / github.com/breck7/scroll / newFormat

Function newFormat

external/.d3.js:9652–9761  ·  view source on GitHub ↗
(specifier)

Source from the content-addressed store, hash-verified

9650 nan = locale.nan === undefined ? "NaN" : locale.nan + "";
9651
9652 function newFormat(specifier) {
9653 specifier = formatSpecifier(specifier);
9654
9655 var fill = specifier.fill,
9656 align = specifier.align,
9657 sign = specifier.sign,
9658 symbol = specifier.symbol,
9659 zero = specifier.zero,
9660 width = specifier.width,
9661 comma = specifier.comma,
9662 precision = specifier.precision,
9663 trim = specifier.trim,
9664 type = specifier.type;
9665
9666 // The "n" type is an alias for ",g".
9667 if (type === "n") comma = true, type = "g";
9668
9669 // The "" type, and any invalid type, is an alias for ".12~g".
9670 else if (!formatTypes[type]) precision === undefined && (precision = 12), trim = true, type = "g";
9671
9672 // If zero fill is specified, padding goes after sign and before digits.
9673 if (zero || (fill === "0" && align === "=")) zero = true, fill = "0", align = "=";
9674
9675 // Compute the prefix and suffix.
9676 // For SI-prefix, the suffix is lazily computed.
9677 var prefix = symbol === "$" ? currencyPrefix : symbol === "#" && /[boxX]/.test(type) ? "0" + type.toLowerCase() : "",
9678 suffix = symbol === "$" ? currencySuffix : /[%p]/.test(type) ? percent : "";
9679
9680 // What format function should we use?
9681 // Is this an integer type?
9682 // Can this type generate exponential notation?
9683 var formatType = formatTypes[type],
9684 maybeSuffix = /[defgprs%]/.test(type);
9685
9686 // Set the default precision if not specified,
9687 // or clamp the specified precision to the supported range.
9688 // For significant precision, it must be in [1, 21].
9689 // For fixed precision, it must be in [0, 20].
9690 precision = precision === undefined ? 6
9691 : /[gprs]/.test(type) ? Math.max(1, Math.min(21, precision))
9692 : Math.max(0, Math.min(20, precision));
9693
9694 function format(value) {
9695 var valuePrefix = prefix,
9696 valueSuffix = suffix,
9697 i, n, c;
9698
9699 if (type === "c") {
9700 valueSuffix = formatType(value) + valueSuffix;
9701 value = "";
9702 } else {
9703 value = +value;
9704
9705 // Determine the sign. -0 is not less than 0, but 1 / -0 is!
9706 var valueNegative = value < 0 || 1 / value < 0;
9707
9708 // Perform the initial formatting.
9709 value = isNaN(value) ? nan : formatType(Math.abs(value), precision);

Callers 2

formatPrefixFunction · 0.85
formatLocaleFunction · 0.85

Calls 6

formatSpecifierFunction · 0.85
formatFunction · 0.85
maxMethod · 0.80
minMethod · 0.80
sliceMethod · 0.80
joinMethod · 0.45

Tested by

no test coverage detected