node_modules ignore

This commit is contained in:
2025-05-08 23:43:47 +02:00
parent e19d52f172
commit 4574544c9f
65041 changed files with 10593536 additions and 0 deletions

View File

@@ -0,0 +1,2 @@
import { UnsignedRoundingModeType } from '../types/number';
export declare function ApplyUnsignedRoundingMode(x: number, r1: number, r2: number, unsignedRoundingMode: UnsignedRoundingModeType): number;

View File

@@ -0,0 +1,38 @@
export function ApplyUnsignedRoundingMode(x, r1, r2, unsignedRoundingMode) {
if (x === r1)
return r1;
if (unsignedRoundingMode === undefined) {
throw new Error('unsignedRoundingMode is mandatory');
}
if (unsignedRoundingMode === 'zero') {
return r1;
}
if (unsignedRoundingMode === 'infinity') {
return r2;
}
var d1 = x - r1;
var d2 = r2 - x;
if (d1 < d2) {
return r1;
}
if (d2 < d1) {
return r2;
}
if (d1 !== d2) {
throw new Error('Unexpected error');
}
if (unsignedRoundingMode === 'half-zero') {
return r1;
}
if (unsignedRoundingMode === 'half-infinity') {
return r2;
}
if (unsignedRoundingMode !== 'half-even') {
throw new Error("Unexpected value for unsignedRoundingMode: ".concat(unsignedRoundingMode));
}
var cardinality = (r1 / (r2 - r1)) % 2;
if (cardinality === 0) {
return r1;
}
return r2;
}

View File

@@ -0,0 +1,4 @@
/**
* https://tc39.es/ecma402/#sec-collapsenumberrange
*/
export declare function CollapseNumberRange<T>(result: T): T;

View File

@@ -0,0 +1,6 @@
/**
* https://tc39.es/ecma402/#sec-collapsenumberrange
*/
export function CollapseNumberRange(result) {
return result;
}

View File

@@ -0,0 +1,11 @@
import { NumberFormatInternal } from '../types/number';
/**
* The abstract operation ComputeExponent computes an exponent (power of ten) by which to scale x
* according to the number formatting settings. It handles cases such as 999 rounding up to 1000,
* requiring a different exponent.
*
* NOT IN SPEC: it returns [exponent, magnitude].
*/
export declare function ComputeExponent(numberFormat: Intl.NumberFormat, x: number, { getInternalSlots, }: {
getInternalSlots(nf: Intl.NumberFormat): NumberFormatInternal;
}): [number, number];

View File

@@ -0,0 +1,39 @@
import { getMagnitude } from '../utils';
import { ComputeExponentForMagnitude } from './ComputeExponentForMagnitude';
import { FormatNumericToString } from './FormatNumericToString';
/**
* The abstract operation ComputeExponent computes an exponent (power of ten) by which to scale x
* according to the number formatting settings. It handles cases such as 999 rounding up to 1000,
* requiring a different exponent.
*
* NOT IN SPEC: it returns [exponent, magnitude].
*/
export function ComputeExponent(numberFormat, x, _a) {
var getInternalSlots = _a.getInternalSlots;
if (x === 0) {
return [0, 0];
}
if (x < 0) {
x = -x;
}
var magnitude = getMagnitude(x);
var exponent = ComputeExponentForMagnitude(numberFormat, magnitude, {
getInternalSlots: getInternalSlots,
});
// Preserve more precision by doing multiplication when exponent is negative.
x = exponent < 0 ? x * Math.pow(10, -exponent) : x / Math.pow(10, exponent);
var formatNumberResult = FormatNumericToString(getInternalSlots(numberFormat), x);
if (formatNumberResult.roundedNumber === 0) {
return [exponent, magnitude];
}
var newMagnitude = getMagnitude(formatNumberResult.roundedNumber);
if (newMagnitude === magnitude - exponent) {
return [exponent, magnitude];
}
return [
ComputeExponentForMagnitude(numberFormat, magnitude + 1, {
getInternalSlots: getInternalSlots,
}),
magnitude + 1,
];
}

View File

@@ -0,0 +1,9 @@
import { NumberFormatInternal } from '../types/number';
/**
* The abstract operation ComputeExponentForMagnitude computes an exponent by which to scale a
* number of the given magnitude (power of ten of the most significant digit) according to the
* locale and the desired notation (scientific, engineering, or compact).
*/
export declare function ComputeExponentForMagnitude(numberFormat: Intl.NumberFormat, magnitude: number, { getInternalSlots, }: {
getInternalSlots(nf: Intl.NumberFormat): NumberFormatInternal;
}): number;

View File

@@ -0,0 +1,60 @@
/**
* The abstract operation ComputeExponentForMagnitude computes an exponent by which to scale a
* number of the given magnitude (power of ten of the most significant digit) according to the
* locale and the desired notation (scientific, engineering, or compact).
*/
export function ComputeExponentForMagnitude(numberFormat, magnitude, _a) {
var getInternalSlots = _a.getInternalSlots;
var internalSlots = getInternalSlots(numberFormat);
var notation = internalSlots.notation, dataLocaleData = internalSlots.dataLocaleData, numberingSystem = internalSlots.numberingSystem;
switch (notation) {
case 'standard':
return 0;
case 'scientific':
return magnitude;
case 'engineering':
return Math.floor(magnitude / 3) * 3;
default: {
// Let exponent be an implementation- and locale-dependent (ILD) integer by which to scale a
// number of the given magnitude in compact notation for the current locale.
var compactDisplay = internalSlots.compactDisplay, style = internalSlots.style, currencyDisplay = internalSlots.currencyDisplay;
var thresholdMap = void 0;
if (style === 'currency' && currencyDisplay !== 'name') {
var currency = dataLocaleData.numbers.currency[numberingSystem] ||
dataLocaleData.numbers.currency[dataLocaleData.numbers.nu[0]];
thresholdMap = currency.short;
}
else {
var decimal = dataLocaleData.numbers.decimal[numberingSystem] ||
dataLocaleData.numbers.decimal[dataLocaleData.numbers.nu[0]];
thresholdMap = compactDisplay === 'long' ? decimal.long : decimal.short;
}
if (!thresholdMap) {
return 0;
}
var num = String(Math.pow(10, magnitude));
var thresholds = Object.keys(thresholdMap); // TODO: this can be pre-processed
if (num < thresholds[0]) {
return 0;
}
if (num > thresholds[thresholds.length - 1]) {
return thresholds[thresholds.length - 1].length - 1;
}
var i = thresholds.indexOf(num);
if (i === -1) {
return 0;
}
// See https://unicode.org/reports/tr35/tr35-numbers.html#Compact_Number_Formats
// Special handling if the pattern is precisely `0`.
var magnitudeKey = thresholds[i];
// TODO: do we need to handle plural here?
var compactPattern = thresholdMap[magnitudeKey].other;
if (compactPattern === '0') {
return 0;
}
// Example: in zh-TW, `10000000` maps to `0000萬`. So we need to return 8 - 4 = 4 here.
return (magnitudeKey.length -
thresholdMap[magnitudeKey].other.match(/0+/)[0].length);
}
}
}

View File

@@ -0,0 +1,6 @@
/**
* https://tc39.es/ecma402/#sec-currencydigits
*/
export declare function CurrencyDigits(c: string, { currencyDigitsData }: {
currencyDigitsData: Record<string, number>;
}): number;

View File

@@ -0,0 +1,10 @@
import { HasOwnProperty } from '../262';
/**
* https://tc39.es/ecma402/#sec-currencydigits
*/
export function CurrencyDigits(c, _a) {
var currencyDigitsData = _a.currencyDigitsData;
return HasOwnProperty(currencyDigitsData, c)
? currencyDigitsData[c]
: 2;
}

View File

@@ -0,0 +1,7 @@
import { NumberFormatInternal, NumberFormatPart } from '../types/number';
/**
* https://tc39.es/ecma402/#sec-formatapproximately
*/
export declare function FormatApproximately(numberFormat: Intl.NumberFormat, result: NumberFormatPart[], { getInternalSlots, }: {
getInternalSlots(nf: Intl.NumberFormat): NumberFormatInternal;
}): NumberFormatPart[];

View File

@@ -0,0 +1,11 @@
/**
* https://tc39.es/ecma402/#sec-formatapproximately
*/
export function FormatApproximately(numberFormat, result, _a) {
var getInternalSlots = _a.getInternalSlots;
var internalSlots = getInternalSlots(numberFormat);
var symbols = internalSlots.dataLocaleData.numbers.symbols[internalSlots.numberingSystem];
var approximatelySign = symbols.approximatelySign;
result.push({ type: 'approximatelySign', value: approximatelySign });
return result;
}

View File

@@ -0,0 +1,7 @@
import { NumberFormatInternal } from '../types/number';
/**
* https://tc39.es/ecma402/#sec-formatnumericrange
*/
export declare function FormatNumericRange(numberFormat: Intl.NumberFormat, x: number, y: number, { getInternalSlots, }: {
getInternalSlots(nf: Intl.NumberFormat): NumberFormatInternal;
}): string;

View File

@@ -0,0 +1,11 @@
import { PartitionNumberRangePattern } from './PartitionNumberRangePattern';
/**
* https://tc39.es/ecma402/#sec-formatnumericrange
*/
export function FormatNumericRange(numberFormat, x, y, _a) {
var getInternalSlots = _a.getInternalSlots;
var parts = PartitionNumberRangePattern(numberFormat, x, y, {
getInternalSlots: getInternalSlots,
});
return parts.map(function (part) { return part.value; }).join('');
}

View File

@@ -0,0 +1,7 @@
import { NumberFormatInternal, NumberRangeToParts } from '../types/number';
/**
* https://tc39.es/ecma402/#sec-formatnumericrangetoparts
*/
export declare function FormatNumericRangeToParts(numberFormat: Intl.NumberFormat, x: number, y: number, { getInternalSlots, }: {
getInternalSlots(nf: Intl.NumberFormat): NumberFormatInternal;
}): NumberRangeToParts[];

View File

@@ -0,0 +1,16 @@
import { PartitionNumberRangePattern } from './PartitionNumberRangePattern';
/**
* https://tc39.es/ecma402/#sec-formatnumericrangetoparts
*/
export function FormatNumericRangeToParts(numberFormat, x, y, _a) {
var getInternalSlots = _a.getInternalSlots;
var parts = PartitionNumberRangePattern(numberFormat, x, y, {
getInternalSlots: getInternalSlots,
});
return parts.map(function (part, index) { return ({
type: part.type,
value: part.value,
source: part.source,
result: index.toString(),
}); });
}

View File

@@ -0,0 +1,4 @@
import { NumberFormatInternal, NumberFormatPart } from '../types/number';
export declare function FormatNumericToParts(nf: Intl.NumberFormat, x: number, implDetails: {
getInternalSlots(nf: Intl.NumberFormat): NumberFormatInternal;
}): NumberFormatPart[];

View File

@@ -0,0 +1,14 @@
import { PartitionNumberPattern } from './PartitionNumberPattern';
import { ArrayCreate } from '../262';
export function FormatNumericToParts(nf, x, implDetails) {
var parts = PartitionNumberPattern(nf, x, implDetails);
var result = ArrayCreate(0);
for (var _i = 0, parts_1 = parts; _i < parts_1.length; _i++) {
var part = parts_1[_i];
result.push({
type: part.type,
value: part.value,
});
}
return result;
}

View File

@@ -0,0 +1,8 @@
import { NumberFormatDigitInternalSlots } from '../types/number';
/**
* https://tc39.es/ecma402/#sec-formatnumberstring
*/
export declare function FormatNumericToString(intlObject: Pick<NumberFormatDigitInternalSlots, 'roundingType' | 'minimumSignificantDigits' | 'maximumSignificantDigits' | 'minimumIntegerDigits' | 'minimumFractionDigits' | 'maximumFractionDigits'>, x: number): {
roundedNumber: number;
formattedString: string;
};

View File

@@ -0,0 +1,41 @@
import { SameValue } from '../262';
import { ToRawPrecision } from './ToRawPrecision';
import { repeat } from '../utils';
import { ToRawFixed } from './ToRawFixed';
/**
* https://tc39.es/ecma402/#sec-formatnumberstring
*/
export function FormatNumericToString(intlObject, x) {
var isNegative = x < 0 || SameValue(x, -0);
if (isNegative) {
x = -x;
}
var result;
var rourndingType = intlObject.roundingType;
switch (rourndingType) {
case 'significantDigits':
result = ToRawPrecision(x, intlObject.minimumSignificantDigits, intlObject.maximumSignificantDigits);
break;
case 'fractionDigits':
result = ToRawFixed(x, intlObject.minimumFractionDigits, intlObject.maximumFractionDigits);
break;
default:
result = ToRawPrecision(x, 1, 2);
if (result.integerDigitsCount > 1) {
result = ToRawFixed(x, 0, 0);
}
break;
}
x = result.roundedNumber;
var string = result.formattedString;
var int = result.integerDigitsCount;
var minInteger = intlObject.minimumIntegerDigits;
if (int < minInteger) {
var forwardZeros = repeat('0', minInteger - int);
string = forwardZeros + string;
}
if (isNegative) {
x = -x;
}
return { roundedNumber: x, formattedString: string };
}

View File

@@ -0,0 +1,2 @@
import { RoundingModeType, UnsignedRoundingModeType } from '../types/number';
export declare function GetUnsignedRoundingMode(roundingMode: RoundingModeType, isNegative: boolean): UnsignedRoundingModeType;

View File

@@ -0,0 +1,28 @@
var negativeMapping = {
ceil: 'zero',
floor: 'infinity',
expand: 'infinity',
trunc: 'zero',
halfCeil: 'half-zero',
halfFloor: 'half-infinity',
halfExpand: 'half-infinity',
halfTrunc: 'half-zero',
halfEven: 'half-even',
};
var positiveMapping = {
ceil: 'infinity',
floor: 'zero',
expand: 'infinity',
trunc: 'zero',
halfCeil: 'half-infinity',
halfFloor: 'half-zero',
halfExpand: 'half-infinity',
halfTrunc: 'half-zero',
halfEven: 'half-even',
};
export function GetUnsignedRoundingMode(roundingMode, isNegative) {
if (isNegative) {
return negativeMapping[roundingMode];
}
return positiveMapping[roundingMode];
}

View File

@@ -0,0 +1,12 @@
import { NumberFormatInternal, NumberFormatLocaleInternalData, NumberFormatOptions } from '../types/number';
/**
* https://tc39.es/ecma402/#sec-initializenumberformat
*/
export declare function InitializeNumberFormat(nf: Intl.NumberFormat, locales: string | ReadonlyArray<string> | undefined, opts: NumberFormatOptions | undefined, { getInternalSlots, localeData, availableLocales, numberingSystemNames, getDefaultLocale, currencyDigitsData, }: {
getInternalSlots(nf: Intl.NumberFormat): NumberFormatInternal;
localeData: Record<string, NumberFormatLocaleInternalData | undefined>;
availableLocales: Set<string>;
numberingSystemNames: ReadonlyArray<string>;
getDefaultLocale(): string;
currencyDigitsData: Record<string, number>;
}): Intl.NumberFormat;

View File

@@ -0,0 +1,95 @@
import { ResolveLocale } from '@formatjs/intl-localematcher';
import { CanonicalizeLocaleList } from '../CanonicalizeLocaleList';
import { CoerceOptionsToObject } from '../CoerceOptionsToObject';
import { GetNumberOption } from '../GetNumberOption';
import { GetOption } from '../GetOption';
import { GetStringOrBooleanOption } from '../GetStringOrBooleanOption';
import { invariant } from '../utils';
import { CurrencyDigits } from './CurrencyDigits';
import { SetNumberFormatDigitOptions } from './SetNumberFormatDigitOptions';
import { SetNumberFormatUnitOptions } from './SetNumberFormatUnitOptions';
var VALID_ROUND_INCREMENT_VALUES = [
1, 2, 5, 10, 20, 25, 50, 100, 200, 250, 500, 1000, 2000,
];
/**
* https://tc39.es/ecma402/#sec-initializenumberformat
*/
export function InitializeNumberFormat(nf, locales, opts, _a) {
var getInternalSlots = _a.getInternalSlots, localeData = _a.localeData, availableLocales = _a.availableLocales, numberingSystemNames = _a.numberingSystemNames, getDefaultLocale = _a.getDefaultLocale, currencyDigitsData = _a.currencyDigitsData;
// @ts-ignore
var requestedLocales = CanonicalizeLocaleList(locales);
var options = CoerceOptionsToObject(opts);
var opt = Object.create(null);
var matcher = GetOption(options, 'localeMatcher', 'string', ['lookup', 'best fit'], 'best fit');
opt.localeMatcher = matcher;
var numberingSystem = GetOption(options, 'numberingSystem', 'string', undefined, undefined);
if (numberingSystem !== undefined &&
numberingSystemNames.indexOf(numberingSystem) < 0) {
// 8.a. If numberingSystem does not match the Unicode Locale Identifier type nonterminal,
// throw a RangeError exception.
throw RangeError("Invalid numberingSystems: ".concat(numberingSystem));
}
opt.nu = numberingSystem;
var r = ResolveLocale(Array.from(availableLocales), requestedLocales, opt,
// [[RelevantExtensionKeys]] slot, which is a constant
['nu'], localeData, getDefaultLocale);
var dataLocaleData = localeData[r.dataLocale];
invariant(!!dataLocaleData, "Missing locale data for ".concat(r.dataLocale));
var internalSlots = getInternalSlots(nf);
internalSlots.locale = r.locale;
internalSlots.dataLocale = r.dataLocale;
internalSlots.numberingSystem = r.nu;
internalSlots.dataLocaleData = dataLocaleData;
SetNumberFormatUnitOptions(nf, options, { getInternalSlots: getInternalSlots });
var style = internalSlots.style;
var mnfdDefault;
var mxfdDefault;
if (style === 'currency') {
var currency = internalSlots.currency;
var cDigits = CurrencyDigits(currency, { currencyDigitsData: currencyDigitsData });
mnfdDefault = cDigits;
mxfdDefault = cDigits;
}
else {
mnfdDefault = 0;
mxfdDefault = style === 'percent' ? 0 : 3;
}
var notation = GetOption(options, 'notation', 'string', ['standard', 'scientific', 'engineering', 'compact'], 'standard');
internalSlots.notation = notation;
SetNumberFormatDigitOptions(internalSlots, options, mnfdDefault, mxfdDefault, notation);
var roundingIncrement = GetNumberOption(options, 'roundingIncrement', 1, 5000, 1);
if (VALID_ROUND_INCREMENT_VALUES.indexOf(roundingIncrement) === -1) {
throw new RangeError("Invalid rounding increment value: ".concat(roundingIncrement, ".\nValid values are ").concat(VALID_ROUND_INCREMENT_VALUES, "."));
}
if (roundingIncrement !== 1 &&
internalSlots.roundingType !== 'fractionDigits') {
throw new TypeError("For roundingIncrement > 1 only fractionDigits is a valid roundingType");
}
if (roundingIncrement !== 1 &&
internalSlots.maximumFractionDigits !== internalSlots.minimumFractionDigits) {
throw new RangeError('With roundingIncrement > 1, maximumFractionDigits and minimumFractionDigits must be equal.');
}
internalSlots.roundingIncrement = roundingIncrement;
var trailingZeroDisplay = GetOption(options, 'trailingZeroDisplay', 'string', ['auto', 'stripIfInteger'], 'auto');
internalSlots.trailingZeroDisplay = trailingZeroDisplay;
var compactDisplay = GetOption(options, 'compactDisplay', 'string', ['short', 'long'], 'short');
var defaultUseGrouping = 'auto';
if (notation === 'compact') {
internalSlots.compactDisplay = compactDisplay;
defaultUseGrouping = 'min2';
}
internalSlots.useGrouping = GetStringOrBooleanOption(options, 'useGrouping', ['min2', 'auto', 'always'], 'always', false, defaultUseGrouping);
internalSlots.signDisplay = GetOption(options, 'signDisplay', 'string', ['auto', 'never', 'always', 'exceptZero', 'negative'], 'auto');
internalSlots.roundingMode = GetOption(options, 'roundingMode', 'string', [
'ceil',
'floor',
'expand',
'trunc',
'halfCeil',
'halfFloor',
'halfExpand',
'halfTrunc',
'halfEven',
], 'halfExpand');
return nf;
}

View File

@@ -0,0 +1,7 @@
import { NumberFormatInternal } from '../types/number';
/**
* https://tc39.es/ecma402/#sec-formatnumberstring
*/
export declare function PartitionNumberPattern(numberFormat: Intl.NumberFormat, x: number, { getInternalSlots, }: {
getInternalSlots(nf: Intl.NumberFormat): NumberFormatInternal;
}): import("../types/number").NumberFormatPart[];

View File

@@ -0,0 +1,80 @@
import { FormatNumericToString } from './FormatNumericToString';
import { SameValue } from '../262';
import { ComputeExponent } from './ComputeExponent';
import formatToParts from './format_to_parts';
/**
* https://tc39.es/ecma402/#sec-formatnumberstring
*/
export function PartitionNumberPattern(numberFormat, x, _a) {
var _b;
var getInternalSlots = _a.getInternalSlots;
var internalSlots = getInternalSlots(numberFormat);
var pl = internalSlots.pl, dataLocaleData = internalSlots.dataLocaleData, numberingSystem = internalSlots.numberingSystem;
var symbols = dataLocaleData.numbers.symbols[numberingSystem] ||
dataLocaleData.numbers.symbols[dataLocaleData.numbers.nu[0]];
var magnitude = 0;
var exponent = 0;
var n;
if (isNaN(x)) {
n = symbols.nan;
}
else if (x == Number.POSITIVE_INFINITY || x == Number.NEGATIVE_INFINITY) {
n = symbols.infinity;
}
else {
if (!SameValue(x, -0)) {
if (!isFinite(x)) {
throw new Error('Input must be a mathematical value');
}
if (internalSlots.style == 'percent') {
x *= 100;
}
;
_b = ComputeExponent(numberFormat, x, {
getInternalSlots: getInternalSlots,
}), exponent = _b[0], magnitude = _b[1];
// Preserve more precision by doing multiplication when exponent is negative.
x = exponent < 0 ? x * Math.pow(10, -exponent) : x / Math.pow(10, exponent);
}
var formatNumberResult = FormatNumericToString(internalSlots, x);
n = formatNumberResult.formattedString;
x = formatNumberResult.roundedNumber;
}
// Based on https://tc39.es/ecma402/#sec-getnumberformatpattern
// We need to do this before `x` is rounded.
var sign;
var signDisplay = internalSlots.signDisplay;
switch (signDisplay) {
case 'never':
sign = 0;
break;
case 'auto':
if (SameValue(x, 0) || x > 0 || isNaN(x)) {
sign = 0;
}
else {
sign = -1;
}
break;
case 'always':
if (SameValue(x, 0) || x > 0 || isNaN(x)) {
sign = 1;
}
else {
sign = -1;
}
break;
default:
// x === 0 -> x is 0 or x is -0
if (x === 0 || isNaN(x)) {
sign = 0;
}
else if (x > 0) {
sign = 1;
}
else {
sign = -1;
}
}
return formatToParts({ roundedNumber: x, formattedString: n, exponent: exponent, magnitude: magnitude, sign: sign }, internalSlots.dataLocaleData, pl, internalSlots);
}

View File

@@ -0,0 +1,7 @@
import { NumberFormatInternal, NumberFormatPart } from '../types/number';
/**
* https://tc39.es/ecma402/#sec-partitionnumberrangepattern
*/
export declare function PartitionNumberRangePattern(numberFormat: Intl.NumberFormat, x: number, y: number, { getInternalSlots, }: {
getInternalSlots(nf: Intl.NumberFormat): NumberFormatInternal;
}): NumberFormatPart[];

View File

@@ -0,0 +1,32 @@
import { PartitionNumberPattern } from './PartitionNumberPattern';
import { CollapseNumberRange } from './CollapseNumberRange';
import { FormatApproximately } from './FormatApproximately';
/**
* https://tc39.es/ecma402/#sec-partitionnumberrangepattern
*/
export function PartitionNumberRangePattern(numberFormat, x, y, _a) {
var getInternalSlots = _a.getInternalSlots;
if (isNaN(x) || isNaN(y)) {
throw new RangeError('Input must be a number');
}
var result = [];
var xResult = PartitionNumberPattern(numberFormat, x, { getInternalSlots: getInternalSlots });
var yResult = PartitionNumberPattern(numberFormat, y, { getInternalSlots: getInternalSlots });
if (xResult === yResult) {
return FormatApproximately(numberFormat, xResult, { getInternalSlots: getInternalSlots });
}
for (var _i = 0, xResult_1 = xResult; _i < xResult_1.length; _i++) {
var r = xResult_1[_i];
r.source = 'startRange';
}
result = result.concat(xResult);
var internalSlots = getInternalSlots(numberFormat);
var symbols = internalSlots.dataLocaleData.numbers.symbols[internalSlots.numberingSystem];
result.push({ type: 'literal', value: symbols.rangeSign, source: 'shared' });
for (var _b = 0, yResult_1 = yResult; _b < yResult_1.length; _b++) {
var r = yResult_1[_b];
r.source = 'endRange';
}
result = result.concat(yResult);
return CollapseNumberRange(result);
}

View File

@@ -0,0 +1,5 @@
import { NumberFormatDigitInternalSlots, NumberFormatDigitOptions, NumberFormatNotation } from '../types/number';
/**
* https://tc39.es/ecma402/#sec-setnfdigitoptions
*/
export declare function SetNumberFormatDigitOptions(internalSlots: NumberFormatDigitInternalSlots, opts: NumberFormatDigitOptions, mnfdDefault: number, mxfdDefault: number, notation: NumberFormatNotation): void;

View File

@@ -0,0 +1,80 @@
import { DefaultNumberOption } from '../DefaultNumberOption';
import { GetNumberOption } from '../GetNumberOption';
import { GetOption } from '../GetOption';
/**
* https://tc39.es/ecma402/#sec-setnfdigitoptions
*/
export function SetNumberFormatDigitOptions(internalSlots, opts, mnfdDefault, mxfdDefault, notation) {
var mnid = GetNumberOption(opts, 'minimumIntegerDigits', 1, 21, 1);
var mnfd = opts.minimumFractionDigits;
var mxfd = opts.maximumFractionDigits;
var mnsd = opts.minimumSignificantDigits;
var mxsd = opts.maximumSignificantDigits;
internalSlots.minimumIntegerDigits = mnid;
var roundingPriority = GetOption(opts, 'roundingPriority', 'string', ['auto', 'morePrecision', 'lessPrecision'], 'auto');
var hasSd = mnsd !== undefined || mxsd !== undefined;
var hasFd = mnfd !== undefined || mxfd !== undefined;
var needSd = true;
var needFd = true;
if (roundingPriority === 'auto') {
needSd = hasSd;
if (hasSd || (!hasFd && notation === 'compact')) {
needFd = false;
}
}
if (needSd) {
if (hasSd) {
mnsd = DefaultNumberOption(mnsd, 1, 21, 1);
mxsd = DefaultNumberOption(mxsd, mnsd, 21, 21);
internalSlots.minimumSignificantDigits = mnsd;
internalSlots.maximumSignificantDigits = mxsd;
}
else {
internalSlots.minimumSignificantDigits = 1;
internalSlots.maximumSignificantDigits = 21;
}
}
if (needFd) {
if (hasFd) {
mnfd = DefaultNumberOption(mnfd, 0, 20, undefined);
mxfd = DefaultNumberOption(mxfd, 0, 20, undefined);
if (mnfd === undefined) {
// @ts-expect-error
mnfd = Math.min(mnfdDefault, mxfd);
}
else if (mxfd === undefined) {
mxfd = Math.max(mxfdDefault, mnfd);
}
else if (mnfd > mxfd) {
throw new RangeError("Invalid range, ".concat(mnfd, " > ").concat(mxfd));
}
internalSlots.minimumFractionDigits = mnfd;
internalSlots.maximumFractionDigits = mxfd;
}
else {
internalSlots.minimumFractionDigits = mnfdDefault;
internalSlots.maximumFractionDigits = mxfdDefault;
}
}
if (needSd || needFd) {
if (roundingPriority === 'morePrecision') {
internalSlots.roundingType = 'morePrecision';
}
else if (roundingPriority === 'lessPrecision') {
internalSlots.roundingType = 'lessPrecision';
}
else if (hasSd) {
internalSlots.roundingType = 'significantDigits';
}
else {
internalSlots.roundingType = 'fractionDigits';
}
}
else {
internalSlots.roundingType = 'morePrecision';
internalSlots.minimumFractionDigits = 0;
internalSlots.maximumFractionDigits = 0;
internalSlots.minimumSignificantDigits = 1;
internalSlots.maximumSignificantDigits = 2;
}
}

View File

@@ -0,0 +1,7 @@
import { NumberFormatInternal, NumberFormatOptions } from '../types/number';
/**
* https://tc39.es/ecma402/#sec-setnumberformatunitoptions
*/
export declare function SetNumberFormatUnitOptions(nf: Intl.NumberFormat, options: NumberFormatOptions | undefined, { getInternalSlots, }: {
getInternalSlots(nf: Intl.NumberFormat): NumberFormatInternal;
}): void;

View File

@@ -0,0 +1,39 @@
import { GetOption } from '../GetOption';
import { IsWellFormedCurrencyCode } from '../IsWellFormedCurrencyCode';
import { IsWellFormedUnitIdentifier } from '../IsWellFormedUnitIdentifier';
/**
* https://tc39.es/ecma402/#sec-setnumberformatunitoptions
*/
export function SetNumberFormatUnitOptions(nf, options, _a) {
if (options === void 0) { options = Object.create(null); }
var getInternalSlots = _a.getInternalSlots;
var internalSlots = getInternalSlots(nf);
var style = GetOption(options, 'style', 'string', ['decimal', 'percent', 'currency', 'unit'], 'decimal');
internalSlots.style = style;
var currency = GetOption(options, 'currency', 'string', undefined, undefined);
if (currency !== undefined && !IsWellFormedCurrencyCode(currency)) {
throw RangeError('Malformed currency code');
}
if (style === 'currency' && currency === undefined) {
throw TypeError('currency cannot be undefined');
}
var currencyDisplay = GetOption(options, 'currencyDisplay', 'string', ['code', 'symbol', 'narrowSymbol', 'name'], 'symbol');
var currencySign = GetOption(options, 'currencySign', 'string', ['standard', 'accounting'], 'standard');
var unit = GetOption(options, 'unit', 'string', undefined, undefined);
if (unit !== undefined && !IsWellFormedUnitIdentifier(unit)) {
throw RangeError('Invalid unit argument for Intl.NumberFormat()');
}
if (style === 'unit' && unit === undefined) {
throw TypeError('unit cannot be undefined');
}
var unitDisplay = GetOption(options, 'unitDisplay', 'string', ['short', 'narrow', 'long'], 'short');
if (style === 'currency') {
internalSlots.currency = currency.toUpperCase();
internalSlots.currencyDisplay = currencyDisplay;
internalSlots.currencySign = currencySign;
}
if (style === 'unit') {
internalSlots.unit = unit;
internalSlots.unitDisplay = unitDisplay;
}
}

View File

@@ -0,0 +1,9 @@
import { RawNumberFormatResult } from '../types/number';
/**
* TODO: dedup with intl-pluralrules and support BigInt
* https://tc39.es/ecma402/#sec-torawfixed
* @param x a finite non-negative Number or BigInt
* @param minFraction and integer between 0 and 20
* @param maxFraction and integer between 0 and 20
*/
export declare function ToRawFixed(x: number, minFraction: number, maxFraction: number): RawNumberFormatResult;

View File

@@ -0,0 +1,51 @@
import { repeat } from '../utils';
/**
* TODO: dedup with intl-pluralrules and support BigInt
* https://tc39.es/ecma402/#sec-torawfixed
* @param x a finite non-negative Number or BigInt
* @param minFraction and integer between 0 and 20
* @param maxFraction and integer between 0 and 20
*/
export function ToRawFixed(x, minFraction, maxFraction) {
var f = maxFraction;
var n = Math.round(x * Math.pow(10, f));
var xFinal = n / Math.pow(10, f);
// n is a positive integer, but it is possible to be greater than 1e21.
// In such case we will go the slow path.
// See also: https://tc39.es/ecma262/#sec-numeric-types-number-tostring
var m;
if (n < 1e21) {
m = n.toString();
}
else {
m = n.toString();
var _a = m.split('e'), mantissa = _a[0], exponent = _a[1];
m = mantissa.replace('.', '');
m = m + repeat('0', Math.max(+exponent - m.length + 1, 0));
}
var int;
if (f !== 0) {
var k = m.length;
if (k <= f) {
var z = repeat('0', f + 1 - k);
m = z + m;
k = f + 1;
}
var a = m.slice(0, k - f);
var b = m.slice(k - f);
m = "".concat(a, ".").concat(b);
int = a.length;
}
else {
int = m.length;
}
var cut = maxFraction - minFraction;
while (cut > 0 && m[m.length - 1] === '0') {
m = m.slice(0, -1);
cut--;
}
if (m[m.length - 1] === '.') {
m = m.slice(0, -1);
}
return { formattedString: m, roundedNumber: xFinal, integerDigitsCount: int };
}

View File

@@ -0,0 +1,2 @@
import { RawNumberFormatResult } from '../types/number';
export declare function ToRawPrecision(x: number, minPrecision: number, maxPrecision: number): RawNumberFormatResult;

View File

@@ -0,0 +1,74 @@
import { repeat, getMagnitude } from '../utils';
export function ToRawPrecision(x, minPrecision, maxPrecision) {
var p = maxPrecision;
var m;
var e;
var xFinal;
if (x === 0) {
m = repeat('0', p);
e = 0;
xFinal = 0;
}
else {
var xToString = x.toString();
// If xToString is formatted as scientific notation, the number is either very small or very
// large. If the precision of the formatted string is lower that requested max precision, we
// should still infer them from the formatted string, otherwise the formatted result might have
// precision loss (e.g. 1e41 will not have 0 in every trailing digits).
var xToStringExponentIndex = xToString.indexOf('e');
var _a = xToString.split('e'), xToStringMantissa = _a[0], xToStringExponent = _a[1];
var xToStringMantissaWithoutDecimalPoint = xToStringMantissa.replace('.', '');
if (xToStringExponentIndex >= 0 &&
xToStringMantissaWithoutDecimalPoint.length <= p) {
e = +xToStringExponent;
m =
xToStringMantissaWithoutDecimalPoint +
repeat('0', p - xToStringMantissaWithoutDecimalPoint.length);
xFinal = x;
}
else {
e = getMagnitude(x);
var decimalPlaceOffset = e - p + 1;
// n is the integer containing the required precision digits. To derive the formatted string,
// we will adjust its decimal place in the logic below.
var n = Math.round(adjustDecimalPlace(x, decimalPlaceOffset));
// The rounding caused the change of magnitude, so we should increment `e` by 1.
if (adjustDecimalPlace(n, p - 1) >= 10) {
e = e + 1;
// Divide n by 10 to swallow one precision.
n = Math.floor(n / 10);
}
m = n.toString();
// Equivalent of n * 10 ** (e - p + 1)
xFinal = adjustDecimalPlace(n, p - 1 - e);
}
}
var int;
if (e >= p - 1) {
m = m + repeat('0', e - p + 1);
int = e + 1;
}
else if (e >= 0) {
m = "".concat(m.slice(0, e + 1), ".").concat(m.slice(e + 1));
int = e + 1;
}
else {
m = "0.".concat(repeat('0', -e - 1)).concat(m);
int = 1;
}
if (m.indexOf('.') >= 0 && maxPrecision > minPrecision) {
var cut = maxPrecision - minPrecision;
while (cut > 0 && m[m.length - 1] === '0') {
m = m.slice(0, -1);
cut--;
}
if (m[m.length - 1] === '.') {
m = m.slice(0, -1);
}
}
return { formattedString: m, roundedNumber: xFinal, integerDigitsCount: int };
// x / (10 ** magnitude), but try to preserve as much floating point precision as possible.
function adjustDecimalPlace(x, magnitude) {
return magnitude < 0 ? x * Math.pow(10, -magnitude) : x / Math.pow(10, magnitude);
}
}

View File

@@ -0,0 +1 @@
export declare const digitMapping: Record<string, ReadonlyArray<string>>;

View File

@@ -0,0 +1,782 @@
export var digitMapping = {
"adlm": [
"𞥐",
"𞥑",
"𞥒",
"𞥓",
"𞥔",
"𞥕",
"𞥖",
"𞥗",
"𞥘",
"𞥙"
],
"ahom": [
"𑜰",
"𑜱",
"𑜲",
"𑜳",
"𑜴",
"𑜵",
"𑜶",
"𑜷",
"𑜸",
"𑜹"
],
"arab": [
"٠",
"١",
"٢",
"٣",
"٤",
"٥",
"٦",
"٧",
"٨",
"٩"
],
"arabext": [
"۰",
"۱",
"۲",
"۳",
"۴",
"۵",
"۶",
"۷",
"۸",
"۹"
],
"bali": [
"᭐",
"᭑",
"᭒",
"᭓",
"᭔",
"᭕",
"᭖",
"᭗",
"᭘",
"᭙"
],
"beng": [
"",
"১",
"২",
"৩",
"",
"৫",
"৬",
"",
"৮",
"৯"
],
"bhks": [
"𑱐",
"𑱑",
"𑱒",
"𑱓",
"𑱔",
"𑱕",
"𑱖",
"𑱗",
"𑱘",
"𑱙"
],
"brah": [
"𑁦",
"𑁧",
"𑁨",
"𑁩",
"𑁪",
"𑁫",
"𑁬",
"𑁭",
"𑁮",
"𑁯"
],
"cakm": [
"𑄶",
"𑄷",
"𑄸",
"𑄹",
"𑄺",
"𑄻",
"𑄼",
"𑄽",
"𑄾",
"𑄿"
],
"cham": [
"꩐",
"꩑",
"꩒",
"꩓",
"꩔",
"꩕",
"꩖",
"꩗",
"꩘",
"꩙"
],
"deva": [
"",
"१",
"२",
"३",
"४",
"५",
"६",
"७",
"८",
"९"
],
"diak": [
"𑥐",
"𑥑",
"𑥒",
"𑥓",
"𑥔",
"𑥕",
"𑥖",
"𑥗",
"𑥘",
"𑥙"
],
"fullwide": [
"",
"",
"",
"",
"",
"",
"",
"",
"",
""
],
"gong": [
"𑶠",
"𑶡",
"𑶢",
"𑶣",
"𑶤",
"𑶥",
"𑶦",
"𑶧",
"𑶨",
"𑶩"
],
"gonm": [
"𑵐",
"𑵑",
"𑵒",
"𑵓",
"𑵔",
"𑵕",
"𑵖",
"𑵗",
"𑵘",
"𑵙"
],
"gujr": [
"",
"૧",
"૨",
"૩",
"૪",
"૫",
"૬",
"૭",
"૮",
"૯"
],
"guru": [
"",
"",
"੨",
"੩",
"",
"੫",
"੬",
"੭",
"੮",
"੯"
],
"hanidec": [
"",
"一",
"二",
"三",
"四",
"五",
"六",
"七",
"八",
"九"
],
"hmng": [
"𖭐",
"𖭑",
"𖭒",
"𖭓",
"𖭔",
"𖭕",
"𖭖",
"𖭗",
"𖭘",
"𖭙"
],
"hmnp": [
"𞅀",
"𞅁",
"𞅂",
"𞅃",
"𞅄",
"𞅅",
"𞅆",
"𞅇",
"𞅈",
"𞅉"
],
"java": [
"꧐",
"꧑",
"꧒",
"꧓",
"꧔",
"꧕",
"꧖",
"꧗",
"꧘",
"꧙"
],
"kali": [
"꤀",
"꤁",
"꤂",
"꤃",
"꤄",
"꤅",
"꤆",
"꤇",
"꤈",
"꤉"
],
"khmr": [
"០",
"១",
"២",
"៣",
"៤",
"៥",
"៦",
"៧",
"៨",
"៩"
],
"knda": [
"",
"೧",
"೨",
"೩",
"೪",
"೫",
"೬",
"೭",
"೮",
"೯"
],
"lana": [
"᪀",
"᪁",
"᪂",
"᪃",
"᪄",
"᪅",
"᪆",
"᪇",
"᪈",
"᪉"
],
"lanatham": [
"᪐",
"᪑",
"᪒",
"᪓",
"᪔",
"᪕",
"᪖",
"᪗",
"᪘",
"᪙"
],
"laoo": [
"",
"໑",
"໒",
"໓",
"໔",
"໕",
"໖",
"໗",
"໘",
"໙"
],
"lepc": [
"᪐",
"᪑",
"᪒",
"᪓",
"᪔",
"᪕",
"᪖",
"᪗",
"᪘",
"᪙"
],
"limb": [
"᥆",
"᥇",
"᥈",
"᥉",
"᥊",
"᥋",
"᥌",
"᥍",
"᥎",
"᥏"
],
"mathbold": [
"𝟎",
"𝟏",
"𝟐",
"𝟑",
"𝟒",
"𝟓",
"𝟔",
"𝟕",
"𝟖",
"𝟗"
],
"mathdbl": [
"𝟘",
"𝟙",
"𝟚",
"𝟛",
"𝟜",
"𝟝",
"𝟞",
"𝟟",
"𝟠",
"𝟡"
],
"mathmono": [
"𝟶",
"𝟷",
"𝟸",
"𝟹",
"𝟺",
"𝟻",
"𝟼",
"𝟽",
"𝟾",
"𝟿"
],
"mathsanb": [
"𝟬",
"𝟭",
"𝟮",
"𝟯",
"𝟰",
"𝟱",
"𝟲",
"𝟳",
"𝟴",
"𝟵"
],
"mathsans": [
"𝟢",
"𝟣",
"𝟤",
"𝟥",
"𝟦",
"𝟧",
"𝟨",
"𝟩",
"𝟪",
"𝟫"
],
"mlym": [
"",
"൧",
"൨",
"൩",
"൪",
"൫",
"൬",
"",
"൮",
"൯"
],
"modi": [
"𑙐",
"𑙑",
"𑙒",
"𑙓",
"𑙔",
"𑙕",
"𑙖",
"𑙗",
"𑙘",
"𑙙"
],
"mong": [
"᠐",
"᠑",
"᠒",
"᠓",
"᠔",
"᠕",
"᠖",
"᠗",
"᠘",
"᠙"
],
"mroo": [
"𖩠",
"𖩡",
"𖩢",
"𖩣",
"𖩤",
"𖩥",
"𖩦",
"𖩧",
"𖩨",
"𖩩"
],
"mtei": [
"꯰",
"꯱",
"꯲",
"꯳",
"꯴",
"꯵",
"꯶",
"꯷",
"꯸",
"꯹"
],
"mymr": [
"",
"၁",
"၂",
"၃",
"၄",
"၅",
"၆",
"၇",
"၈",
"၉"
],
"mymrshan": [
"႐",
"႑",
"႒",
"႓",
"႔",
"႕",
"႖",
"႗",
"႘",
"႙"
],
"mymrtlng": [
"꧰",
"꧱",
"꧲",
"꧳",
"꧴",
"꧵",
"꧶",
"꧷",
"꧸",
"꧹"
],
"newa": [
"𑑐",
"𑑑",
"𑑒",
"𑑓",
"𑑔",
"𑑕",
"𑑖",
"𑑗",
"𑑘",
"𑑙"
],
"nkoo": [
"߀",
"߁",
"߂",
"߃",
"߄",
"߅",
"߆",
"߇",
"߈",
"߉"
],
"olck": [
"᱐",
"᱑",
"᱒",
"᱓",
"᱔",
"᱕",
"᱖",
"᱗",
"᱘",
"᱙"
],
"orya": [
"",
"୧",
"",
"୩",
"୪",
"୫",
"୬",
"୭",
"୮",
"୯"
],
"osma": [
"𐒠",
"𐒡",
"𐒢",
"𐒣",
"𐒤",
"𐒥",
"𐒦",
"𐒧",
"𐒨",
"𐒩"
],
"rohg": [
"𐴰",
"𐴱",
"𐴲",
"𐴳",
"𐴴",
"𐴵",
"𐴶",
"𐴷",
"𐴸",
"𐴹"
],
"saur": [
"꣐",
"꣑",
"꣒",
"꣓",
"꣔",
"꣕",
"꣖",
"꣗",
"꣘",
"꣙"
],
"segment": [
"🯰",
"🯱",
"🯲",
"🯳",
"🯴",
"🯵",
"🯶",
"🯷",
"🯸",
"🯹"
],
"shrd": [
"𑇐",
"𑇑",
"𑇒",
"𑇓",
"𑇔",
"𑇕",
"𑇖",
"𑇗",
"𑇘",
"𑇙"
],
"sind": [
"𑋰",
"𑋱",
"𑋲",
"𑋳",
"𑋴",
"𑋵",
"𑋶",
"𑋷",
"𑋸",
"𑋹"
],
"sinh": [
"෦",
"෧",
"෨",
"෩",
"෪",
"෫",
"෬",
"෭",
"෮",
"෯"
],
"sora": [
"𑃰",
"𑃱",
"𑃲",
"𑃳",
"𑃴",
"𑃵",
"𑃶",
"𑃷",
"𑃸",
"𑃹"
],
"sund": [
"᮰",
"᮱",
"᮲",
"᮳",
"᮴",
"᮵",
"᮶",
"᮷",
"᮸",
"᮹"
],
"takr": [
"𑛀",
"𑛁",
"𑛂",
"𑛃",
"𑛄",
"𑛅",
"𑛆",
"𑛇",
"𑛈",
"𑛉"
],
"talu": [
"᧐",
"᧑",
"᧒",
"᧓",
"᧔",
"᧕",
"᧖",
"᧗",
"᧘",
"᧙"
],
"tamldec": [
"",
"௧",
"௨",
"௩",
"௪",
"௫",
"௬",
"௭",
"௮",
"௯"
],
"telu": [
"",
"౧",
"౨",
"౩",
"౪",
"౫",
"౬",
"౭",
"౮",
"౯"
],
"thai": [
"",
"๑",
"๒",
"๓",
"๔",
"๕",
"๖",
"๗",
"๘",
"๙"
],
"tibt": [
"༠",
"༡",
"༢",
"༣",
"༤",
"༥",
"༦",
"༧",
"༨",
"༩"
],
"tirh": [
"𑓐",
"𑓑",
"𑓒",
"𑓓",
"𑓔",
"𑓕",
"𑓖",
"𑓗",
"𑓘",
"𑓙"
],
"vaii": [
"ᘠ",
"ᘡ",
"ᘢ",
"ᘣ",
"ᘤ",
"ᘥ",
"ᘦ",
"ᘧ",
"ᘨ",
"ᘩ"
],
"wara": [
"𑣠",
"𑣡",
"𑣢",
"𑣣",
"𑣤",
"𑣥",
"𑣦",
"𑣧",
"𑣨",
"𑣩"
],
"wcho": [
"𞋰",
"𞋱",
"𞋲",
"𞋳",
"𞋴",
"𞋵",
"𞋶",
"𞋷",
"𞋸",
"𞋹"
]
};

View File

@@ -0,0 +1,21 @@
import { NumberFormatOptionsStyle, NumberFormatOptionsNotation, NumberFormatOptionsCompactDisplay, NumberFormatOptionsCurrencyDisplay, NumberFormatOptionsCurrencySign, NumberFormatOptionsUnitDisplay, NumberFormatLocaleInternalData, NumberFormatPart, UseGroupingType } from '../types/number';
interface NumberResult {
formattedString: string;
roundedNumber: number;
sign: -1 | 0 | 1;
exponent: number;
magnitude: number;
}
export default function formatToParts(numberResult: NumberResult, data: NumberFormatLocaleInternalData, pl: Intl.PluralRules, options: {
numberingSystem: string;
useGrouping?: UseGroupingType;
style: NumberFormatOptionsStyle;
notation: NumberFormatOptionsNotation;
compactDisplay?: NumberFormatOptionsCompactDisplay;
currency?: string;
currencyDisplay?: NumberFormatOptionsCurrencyDisplay;
currencySign?: NumberFormatOptionsCurrencySign;
unit?: string;
unitDisplay?: NumberFormatOptionsUnitDisplay;
}): NumberFormatPart[];
export {};

View File

@@ -0,0 +1,421 @@
import { ToRawFixed } from './ToRawFixed';
import { digitMapping } from './digit-mapping.generated';
import { S_UNICODE_REGEX } from '../regex.generated';
// This is from: unicode-12.1.0/General_Category/Symbol/regex.js
// IE11 does not support unicode flag, otherwise this is just /\p{S}/u.
// /^\p{S}/u
var CARET_S_UNICODE_REGEX = new RegExp("^".concat(S_UNICODE_REGEX.source));
// /\p{S}$/u
var S_DOLLAR_UNICODE_REGEX = new RegExp("".concat(S_UNICODE_REGEX.source, "$"));
var CLDR_NUMBER_PATTERN = /[#0](?:[\.,][#0]+)*/g;
export default function formatToParts(numberResult, data, pl, options) {
var sign = numberResult.sign, exponent = numberResult.exponent, magnitude = numberResult.magnitude;
var notation = options.notation, style = options.style, numberingSystem = options.numberingSystem;
var defaultNumberingSystem = data.numbers.nu[0];
// #region Part 1: partition and interpolate the CLDR number pattern.
// ----------------------------------------------------------
var compactNumberPattern = null;
if (notation === 'compact' && magnitude) {
compactNumberPattern = getCompactDisplayPattern(numberResult, pl, data, style, options.compactDisplay, options.currencyDisplay, numberingSystem);
}
// This is used multiple times
var nonNameCurrencyPart;
if (style === 'currency' && options.currencyDisplay !== 'name') {
var byCurrencyDisplay = data.currencies[options.currency];
if (byCurrencyDisplay) {
switch (options.currencyDisplay) {
case 'code':
nonNameCurrencyPart = options.currency;
break;
case 'symbol':
nonNameCurrencyPart = byCurrencyDisplay.symbol;
break;
default:
nonNameCurrencyPart = byCurrencyDisplay.narrow;
break;
}
}
else {
// Fallback for unknown currency
nonNameCurrencyPart = options.currency;
}
}
var numberPattern;
if (!compactNumberPattern) {
// Note: if the style is unit, or is currency and the currency display is name,
// its unit parts will be interpolated in part 2. So here we can fallback to decimal.
if (style === 'decimal' ||
style === 'unit' ||
(style === 'currency' && options.currencyDisplay === 'name')) {
// Shortcut for decimal
var decimalData = data.numbers.decimal[numberingSystem] ||
data.numbers.decimal[defaultNumberingSystem];
numberPattern = getPatternForSign(decimalData.standard, sign);
}
else if (style === 'currency') {
var currencyData = data.numbers.currency[numberingSystem] ||
data.numbers.currency[defaultNumberingSystem];
// We replace number pattern part with `0` for easier postprocessing.
numberPattern = getPatternForSign(currencyData[options.currencySign], sign);
}
else {
// percent
var percentPattern = data.numbers.percent[numberingSystem] ||
data.numbers.percent[defaultNumberingSystem];
numberPattern = getPatternForSign(percentPattern, sign);
}
}
else {
numberPattern = compactNumberPattern;
}
// Extract the decimal number pattern string. It looks like "#,##0,00", which will later be
// used to infer decimal group sizes.
var decimalNumberPattern = CLDR_NUMBER_PATTERN.exec(numberPattern)[0];
// Now we start to substitute patterns
// 1. replace strings like `0` and `#,##0.00` with `{0}`
// 2. unquote characters (invariant: the quoted characters does not contain the special tokens)
numberPattern = numberPattern
.replace(CLDR_NUMBER_PATTERN, '{0}')
.replace(/'(.)'/g, '$1');
// Handle currency spacing (both compact and non-compact).
if (style === 'currency' && options.currencyDisplay !== 'name') {
var currencyData = data.numbers.currency[numberingSystem] ||
data.numbers.currency[defaultNumberingSystem];
// See `currencySpacing` substitution rule in TR-35.
// Here we always assume the currencyMatch is "[:^S:]" and surroundingMatch is "[:digit:]".
//
// Example 1: for pattern "#,##0.00¤" with symbol "US$", we replace "¤" with the symbol,
// but insert an extra non-break space before the symbol, because "[:^S:]" matches "U" in
// "US$" and "[:digit:]" matches the latn numbering system digits.
//
// Example 2: for pattern "¤#,##0.00" with symbol "US$", there is no spacing between symbol
// and number, because `$` does not match "[:^S:]".
//
// Implementation note: here we do the best effort to infer the insertion.
// We also assume that `beforeInsertBetween` and `afterInsertBetween` will never be `;`.
var afterCurrency = currencyData.currencySpacing.afterInsertBetween;
if (afterCurrency && !S_DOLLAR_UNICODE_REGEX.test(nonNameCurrencyPart)) {
numberPattern = numberPattern.replace('¤{0}', "\u00A4".concat(afterCurrency, "{0}"));
}
var beforeCurrency = currencyData.currencySpacing.beforeInsertBetween;
if (beforeCurrency && !CARET_S_UNICODE_REGEX.test(nonNameCurrencyPart)) {
numberPattern = numberPattern.replace('{0}¤', "{0}".concat(beforeCurrency, "\u00A4"));
}
}
// The following tokens are special: `{0}`, `¤`, `%`, `-`, `+`, `{c:...}.
var numberPatternParts = numberPattern.split(/({c:[^}]+}|\{0\}|[¤%\-\+])/g);
var numberParts = [];
var symbols = data.numbers.symbols[numberingSystem] ||
data.numbers.symbols[defaultNumberingSystem];
for (var _i = 0, numberPatternParts_1 = numberPatternParts; _i < numberPatternParts_1.length; _i++) {
var part = numberPatternParts_1[_i];
if (!part) {
continue;
}
switch (part) {
case '{0}': {
// We only need to handle scientific and engineering notation here.
numberParts.push.apply(numberParts, paritionNumberIntoParts(symbols, numberResult, notation, exponent, numberingSystem,
// If compact number pattern exists, do not insert group separators.
!compactNumberPattern && Boolean(options.useGrouping), decimalNumberPattern));
break;
}
case '-':
numberParts.push({ type: 'minusSign', value: symbols.minusSign });
break;
case '+':
numberParts.push({ type: 'plusSign', value: symbols.plusSign });
break;
case '%':
numberParts.push({ type: 'percentSign', value: symbols.percentSign });
break;
case '¤':
// Computed above when handling currency spacing.
numberParts.push({ type: 'currency', value: nonNameCurrencyPart });
break;
default:
if (/^\{c:/.test(part)) {
numberParts.push({
type: 'compact',
value: part.substring(3, part.length - 1),
});
}
else {
// literal
numberParts.push({ type: 'literal', value: part });
}
break;
}
}
// #endregion
// #region Part 2: interpolate unit pattern if necessary.
// ----------------------------------------------
switch (style) {
case 'currency': {
// `currencyDisplay: 'name'` has similar pattern handling as units.
if (options.currencyDisplay === 'name') {
var unitPattern = (data.numbers.currency[numberingSystem] ||
data.numbers.currency[defaultNumberingSystem]).unitPattern;
// Select plural
var unitName = void 0;
var currencyNameData = data.currencies[options.currency];
if (currencyNameData) {
unitName = selectPlural(pl, numberResult.roundedNumber * Math.pow(10, exponent), currencyNameData.displayName);
}
else {
// Fallback for unknown currency
unitName = options.currency;
}
// Do {0} and {1} substitution
var unitPatternParts = unitPattern.split(/(\{[01]\})/g);
var result = [];
for (var _a = 0, unitPatternParts_1 = unitPatternParts; _a < unitPatternParts_1.length; _a++) {
var part = unitPatternParts_1[_a];
switch (part) {
case '{0}':
result.push.apply(result, numberParts);
break;
case '{1}':
result.push({ type: 'currency', value: unitName });
break;
default:
if (part) {
result.push({ type: 'literal', value: part });
}
break;
}
}
return result;
}
else {
return numberParts;
}
}
case 'unit': {
var unit = options.unit, unitDisplay = options.unitDisplay;
var unitData = data.units.simple[unit];
var unitPattern = void 0;
if (unitData) {
// Simple unit pattern
unitPattern = selectPlural(pl, numberResult.roundedNumber * Math.pow(10, exponent), data.units.simple[unit][unitDisplay]);
}
else {
// See: http://unicode.org/reports/tr35/tr35-general.html#perUnitPatterns
// If cannot find unit in the simple pattern, it must be "per" compound pattern.
// Implementation note: we are not following TR-35 here because we need to format to parts!
var _b = unit.split('-per-'), numeratorUnit = _b[0], denominatorUnit = _b[1];
unitData = data.units.simple[numeratorUnit];
var numeratorUnitPattern = selectPlural(pl, numberResult.roundedNumber * Math.pow(10, exponent), data.units.simple[numeratorUnit][unitDisplay]);
var perUnitPattern = data.units.simple[denominatorUnit].perUnit[unitDisplay];
if (perUnitPattern) {
// perUnitPattern exists, combine it with numeratorUnitPattern
unitPattern = perUnitPattern.replace('{0}', numeratorUnitPattern);
}
else {
// get compoundUnit pattern (e.g. "{0} per {1}"), repalce {0} with numerator pattern and {1} with
// the denominator pattern in singular form.
var perPattern = data.units.compound.per[unitDisplay];
var denominatorPattern = selectPlural(pl, 1, data.units.simple[denominatorUnit][unitDisplay]);
unitPattern = unitPattern = perPattern
.replace('{0}', numeratorUnitPattern)
.replace('{1}', denominatorPattern.replace('{0}', ''));
}
}
var result = [];
// We need spacing around "{0}" because they are not treated as "unit" parts, but "literal".
for (var _c = 0, _d = unitPattern.split(/(\s*\{0\}\s*)/); _c < _d.length; _c++) {
var part = _d[_c];
var interpolateMatch = /^(\s*)\{0\}(\s*)$/.exec(part);
if (interpolateMatch) {
// Space before "{0}"
if (interpolateMatch[1]) {
result.push({ type: 'literal', value: interpolateMatch[1] });
}
// "{0}" itself
result.push.apply(result, numberParts);
// Space after "{0}"
if (interpolateMatch[2]) {
result.push({ type: 'literal', value: interpolateMatch[2] });
}
}
else if (part) {
result.push({ type: 'unit', value: part });
}
}
return result;
}
default:
return numberParts;
}
// #endregion
}
// A subset of https://tc39.es/ecma402/#sec-partitionnotationsubpattern
// Plus the exponent parts handling.
function paritionNumberIntoParts(symbols, numberResult, notation, exponent, numberingSystem, useGrouping,
/**
* This is the decimal number pattern without signs or symbols.
* It is used to infer the group size when `useGrouping` is true.
*
* A typical value looks like "#,##0.00" (primary group size is 3).
* Some locales like Hindi has secondary group size of 2 (e.g. "#,##,##0.00").
*/
decimalNumberPattern) {
var result = [];
// eslint-disable-next-line prefer-const
var n = numberResult.formattedString, x = numberResult.roundedNumber;
if (isNaN(x)) {
return [{ type: 'nan', value: n }];
}
else if (!isFinite(x)) {
return [{ type: 'infinity', value: n }];
}
var digitReplacementTable = digitMapping[numberingSystem];
if (digitReplacementTable) {
n = n.replace(/\d/g, function (digit) { return digitReplacementTable[+digit] || digit; });
}
// TODO: Else use an implementation dependent algorithm to map n to the appropriate
// representation of n in the given numbering system.
var decimalSepIndex = n.indexOf('.');
var integer;
var fraction;
if (decimalSepIndex > 0) {
integer = n.slice(0, decimalSepIndex);
fraction = n.slice(decimalSepIndex + 1);
}
else {
integer = n;
}
// #region Grouping integer digits
// The weird compact and x >= 10000 check is to ensure consistency with Node.js and Chrome.
// Note that `de` does not have compact form for thousands, but Node.js does not insert grouping separator
// unless the rounded number is greater than 10000:
// NumberFormat('de', {notation: 'compact', compactDisplay: 'short'}).format(1234) //=> "1234"
// NumberFormat('de').format(1234) //=> "1.234"
if (useGrouping && (notation !== 'compact' || x >= 10000)) {
var groupSepSymbol = symbols.group;
var groups = [];
// > There may be two different grouping sizes: The primary grouping size used for the least
// > significant integer group, and the secondary grouping size used for more significant groups.
// > If a pattern contains multiple grouping separators, the interval between the last one and the
// > end of the integer defines the primary grouping size, and the interval between the last two
// > defines the secondary grouping size. All others are ignored.
var integerNumberPattern = decimalNumberPattern.split('.')[0];
var patternGroups = integerNumberPattern.split(',');
var primaryGroupingSize = 3;
var secondaryGroupingSize = 3;
if (patternGroups.length > 1) {
primaryGroupingSize = patternGroups[patternGroups.length - 1].length;
}
if (patternGroups.length > 2) {
secondaryGroupingSize = patternGroups[patternGroups.length - 2].length;
}
var i = integer.length - primaryGroupingSize;
if (i > 0) {
// Slice the least significant integer group
groups.push(integer.slice(i, i + primaryGroupingSize));
// Then iteratively push the more signicant groups
// TODO: handle surrogate pairs in some numbering system digits
for (i -= secondaryGroupingSize; i > 0; i -= secondaryGroupingSize) {
groups.push(integer.slice(i, i + secondaryGroupingSize));
}
groups.push(integer.slice(0, i + secondaryGroupingSize));
}
else {
groups.push(integer);
}
while (groups.length > 0) {
var integerGroup = groups.pop();
result.push({ type: 'integer', value: integerGroup });
if (groups.length > 0) {
result.push({ type: 'group', value: groupSepSymbol });
}
}
}
else {
result.push({ type: 'integer', value: integer });
}
// #endregion
if (fraction !== undefined) {
result.push({ type: 'decimal', value: symbols.decimal }, { type: 'fraction', value: fraction });
}
if ((notation === 'scientific' || notation === 'engineering') &&
isFinite(x)) {
result.push({ type: 'exponentSeparator', value: symbols.exponential });
if (exponent < 0) {
result.push({ type: 'exponentMinusSign', value: symbols.minusSign });
exponent = -exponent;
}
var exponentResult = ToRawFixed(exponent, 0, 0);
result.push({
type: 'exponentInteger',
value: exponentResult.formattedString,
});
}
return result;
}
function getPatternForSign(pattern, sign) {
if (pattern.indexOf(';') < 0) {
pattern = "".concat(pattern, ";-").concat(pattern);
}
var _a = pattern.split(';'), zeroPattern = _a[0], negativePattern = _a[1];
switch (sign) {
case 0:
return zeroPattern;
case -1:
return negativePattern;
default:
return negativePattern.indexOf('-') >= 0
? negativePattern.replace(/-/g, '+')
: "+".concat(zeroPattern);
}
}
// Find the CLDR pattern for compact notation based on the magnitude of data and style.
//
// Example return value: "¤ {c:laki}000;¤{c:laki} -0" (`sw` locale):
// - Notice the `{c:...}` token that wraps the compact literal.
// - The consecutive zeros are normalized to single zero to match CLDR_NUMBER_PATTERN.
//
// Returning null means the compact display pattern cannot be found.
function getCompactDisplayPattern(numberResult, pl, data, style, compactDisplay, currencyDisplay, numberingSystem) {
var _a;
var roundedNumber = numberResult.roundedNumber, sign = numberResult.sign, magnitude = numberResult.magnitude;
var magnitudeKey = String(Math.pow(10, magnitude));
var defaultNumberingSystem = data.numbers.nu[0];
var pattern;
if (style === 'currency' && currencyDisplay !== 'name') {
var byNumberingSystem = data.numbers.currency;
var currencyData = byNumberingSystem[numberingSystem] ||
byNumberingSystem[defaultNumberingSystem];
// NOTE: compact notation ignores currencySign!
var compactPluralRules = (_a = currencyData.short) === null || _a === void 0 ? void 0 : _a[magnitudeKey];
if (!compactPluralRules) {
return null;
}
pattern = selectPlural(pl, roundedNumber, compactPluralRules);
}
else {
var byNumberingSystem = data.numbers.decimal;
var byCompactDisplay = byNumberingSystem[numberingSystem] ||
byNumberingSystem[defaultNumberingSystem];
var compactPlaralRule = byCompactDisplay[compactDisplay][magnitudeKey];
if (!compactPlaralRule) {
return null;
}
pattern = selectPlural(pl, roundedNumber, compactPlaralRule);
}
// See https://unicode.org/reports/tr35/tr35-numbers.html#Compact_Number_Formats
// > If the value is precisely “0”, either explicit or defaulted, then the normal number format
// > pattern for that sort of object is supplied.
if (pattern === '0') {
return null;
}
pattern = getPatternForSign(pattern, sign)
// Extract compact literal from the pattern
.replace(/([^\s;\-\+\d¤]+)/g, '{c:$1}')
// We replace one or more zeros with a single zero so it matches `CLDR_NUMBER_PATTERN`.
.replace(/0+/, '0');
return pattern;
}
function selectPlural(pl, x, rules) {
return rules[pl.select(x)] || rules.other;
}