MCPcopy
hub / github.com/microsoft/SandDance / newFormat

Function newFormat

docs/app/js/sanddance-app.js:15319–15395  ·  view source on GitHub ↗
(specifier)

Source from the content-addressed store, hash-verified

15317exports.default = function(locale) {
15318 var group = locale.grouping === undefined || locale.thousands === undefined ? (0, _identityJsDefault.default) : (0, _formatGroupJsDefault.default)(map.call(locale.grouping, Number), locale.thousands + ""), currencyPrefix = locale.currency === undefined ? "" : locale.currency[0] + "", currencySuffix = locale.currency === undefined ? "" : locale.currency[1] + "", decimal = locale.decimal === undefined ? "." : locale.decimal + "", numerals = locale.numerals === undefined ? (0, _identityJsDefault.default) : (0, _formatNumeralsJsDefault.default)(map.call(locale.numerals, String)), percent = locale.percent === undefined ? "%" : locale.percent + "", minus = locale.minus === undefined ? "-" : locale.minus + "", nan = locale.nan === undefined ? "NaN" : locale.nan + "";
15319 function newFormat(specifier) {
15320 specifier = (0, _formatSpecifierJsDefault.default)(specifier);
15321 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, trim = specifier.trim, type = specifier.type;
15322 // The "n" type is an alias for ",g".
15323 if (type === "n") comma = true, type = "g";
15324 else if (!(0, _formatTypesJsDefault.default)[type]) precision === undefined && (precision = 12), trim = true, type = "g";
15325 // If zero fill is specified, padding goes after sign and before digits.
15326 if (zero || fill === "0" && align === "=") zero = true, fill = "0", align = "=";
15327 // Compute the prefix and suffix.
15328 // For SI-prefix, the suffix is lazily computed.
15329 var prefix = symbol === "$" ? currencyPrefix : symbol === "#" && /[boxX]/.test(type) ? "0" + type.toLowerCase() : "", suffix = symbol === "$" ? currencySuffix : /[%p]/.test(type) ? percent : "";
15330 // What format function should we use?
15331 // Is this an integer type?
15332 // Can this type generate exponential notation?
15333 var formatType = (0, _formatTypesJsDefault.default)[type], maybeSuffix = /[defgprs%]/.test(type);
15334 // Set the default precision if not specified,
15335 // or clamp the specified precision to the supported range.
15336 // For significant precision, it must be in [1, 21].
15337 // For fixed precision, it must be in [0, 20].
15338 precision = precision === undefined ? 6 : /[gprs]/.test(type) ? Math.max(1, Math.min(21, precision)) : Math.max(0, Math.min(20, precision));
15339 function format(value) {
15340 var valuePrefix = prefix, valueSuffix = suffix, i, n, c;
15341 if (type === "c") {
15342 valueSuffix = formatType(value) + valueSuffix;
15343 value = "";
15344 } else {
15345 value = +value;
15346 // Determine the sign. -0 is not less than 0, but 1 / -0 is!
15347 var valueNegative = value < 0 || 1 / value < 0;
15348 // Perform the initial formatting.
15349 value = isNaN(value) ? nan : formatType(Math.abs(value), precision);
15350 // Trim insignificant zeros.
15351 if (trim) value = (0, _formatTrimJsDefault.default)(value);
15352 // If a negative value rounds to zero after formatting, and no explicit positive sign is requested, hide the sign.
15353 if (valueNegative && +value === 0 && sign !== "+") valueNegative = false;
15354 // Compute the prefix and suffix.
15355 valuePrefix = (valueNegative ? sign === "(" ? sign : minus : sign === "-" || sign === "(" ? "" : sign) + valuePrefix;
15356 valueSuffix = (type === "s" ? prefixes[8 + (0, _formatPrefixAutoJs.prefixExponent) / 3] : "") + valueSuffix + (valueNegative && sign === "(" ? ")" : "");
15357 // Break the formatted value into the integer “value” part that can be
15358 // grouped, and fractional or exponential “suffix” part that is not.
15359 if (maybeSuffix) {
15360 i = -1, n = value.length;
15361 while(++i < n)if (c = value.charCodeAt(i), 48 > c || c > 57) {
15362 valueSuffix = (c === 46 ? decimal + value.slice(i + 1) : value.slice(i)) + valueSuffix;
15363 value = value.slice(0, i);
15364 break;
15365 }
15366 }
15367 }
15368 // If the fill character is not "0", grouping is applied before padding.
15369 if (comma && !zero) value = group(value, Infinity);
15370 // Compute the padding.
15371 var length = valuePrefix.length + value.length + valueSuffix.length, padding = length < width ? new Array(width - length + 1).join(fill) : "";
15372 // If the fill character is "0", grouping is applied after padding.
15373 if (comma && zero) value = group(padding + value, padding.length ? width - valueSuffix.length : Infinity), padding = "";
15374 // Reconstruct the final output based on the desired alignment.
15375 switch(align){
15376 case "<":

Callers 2

formatPrefixFunction · 0.70
formatLocaleFunction · 0.70

Calls 3

formatFunction · 0.70
maxMethod · 0.45
minMethod · 0.45

Tested by

no test coverage detected