@formatjs/intl-numberformat
Version:
Ponyfill for ES2020 Intl.NumberFormat
207 lines (206 loc) • 7.98 kB
TypeScript
import Decimal, { Decimal as Decimal$1 } from "@formatjs/bigdecimal";
//#region packages/ecma402-abstract/types/core.d.ts
type Locale = string;
interface LocaleData<T> {
data: T;
locale: Locale;
}
//#endregion
//#region packages/ecma402-abstract/types/plural-rules.d.ts
type LDMLPluralRule = "zero" | "one" | "two" | "few" | "many" | "other";
//#endregion
//#region packages/ecma402-abstract/types/number.d.ts
type NumberFormatNotation = "standard" | "scientific" | "engineering" | "compact";
type RoundingPriorityType = "auto" | "morePrecision" | "lessPrecision";
type NumberFormatRoundingType = "morePrecision" | "lessPrecision" | "significantDigits" | "fractionDigits";
type RoundingModeType = "ceil" | "floor" | "expand" | "trunc" | "halfCeil" | "halfFloor" | "halfExpand" | "halfTrunc" | "halfEven";
type UseGroupingType = "min2" | "auto" | "always" | boolean;
interface NumberFormatDigitOptions {
minimumIntegerDigits?: number;
minimumSignificantDigits?: number;
maximumSignificantDigits?: number;
minimumFractionDigits?: number;
maximumFractionDigits?: number;
roundingPriority?: RoundingPriorityType;
roundingIncrement?: number;
roundingMode?: RoundingModeType;
trailingZeroDisplay?: TrailingZeroDisplay;
}
interface NumberFormatDigitInternalSlots {
minimumIntegerDigits: number;
minimumSignificantDigits: number;
maximumSignificantDigits: number;
roundingType: NumberFormatRoundingType;
minimumFractionDigits: number;
maximumFractionDigits: number;
notation: NumberFormatNotation;
roundingIncrement: number;
roundingMode: RoundingModeType;
trailingZeroDisplay: TrailingZeroDisplay;
roundingPriority: RoundingPriorityType;
}
type RawNumberLocaleData = LocaleData<NumberFormatLocaleInternalData>;
interface NumberFormatLocaleInternalData {
units: UnitDataTable;
currencies: Record<string, CurrencyData>;
numbers: RawNumberData;
nu: string[];
}
interface UnitDataTable {
simple: Record<string, UnitData>;
compound: Record<string, CompoundUnitData>;
}
interface UnitData {
long: LDMLPluralRuleMap<string>;
short: LDMLPluralRuleMap<string>;
narrow: LDMLPluralRuleMap<string>;
perUnit: Record<"narrow" | "short" | "long", string | undefined>;
}
interface CompoundUnitData {
long: string;
short: string;
narrow: string;
}
interface CurrencyData {
displayName: LDMLPluralRuleMap<string>;
symbol: string;
narrow: string;
}
type DecimalFormatNum = "1000" | "10000" | "100000" | "1000000" | "10000000" | "100000000" | "1000000000" | "10000000000" | "100000000000" | "1000000000000" | "10000000000000" | "100000000000000";
type NumberingSystem = string;
/**
* We only care about insertBetween bc we assume
* `currencyMatch` & `surroundingMatch` are all the same
*
* @export
* @interface CurrencySpacingData
*/
interface CurrencySpacingData {
beforeInsertBetween: string;
afterInsertBetween: string;
}
interface RawCurrencyData {
currencySpacing: CurrencySpacingData;
standard: string;
accounting: string;
short?: Record<DecimalFormatNum, LDMLPluralRuleMap<string>>;
unitPattern: string;
}
interface SymbolsData {
decimal: string;
group: string;
list: string;
percentSign: string;
plusSign: string;
minusSign: string;
exponential: string;
superscriptingExponent: string;
perMille: string;
infinity: string;
nan: string;
timeSeparator: string;
approximatelySign: string;
rangeSign: string;
currencyGroup?: string;
currencyDecimal?: string;
}
interface RawNumberData {
nu: string[];
symbols: Record<NumberingSystem, SymbolsData>;
decimal: Record<NumberingSystem, {
standard: string;
long: Record<DecimalFormatNum, LDMLPluralRuleMap<string>>;
short: Record<DecimalFormatNum, LDMLPluralRuleMap<string>>;
}>;
percent: Record<NumberingSystem, string>;
currency: Record<NumberingSystem, RawCurrencyData>;
}
type LDMLPluralRuleMap<T> = Omit<Partial<Record<LDMLPluralRule, T>>, "other"> & {
other: T;
};
type NumberFormatOptionsLocaleMatcher = "lookup" | "best fit";
type NumberFormatOptionsStyle = "decimal" | "percent" | "currency" | "unit";
type NumberFormatOptionsCompactDisplay = "short" | "long";
type NumberFormatOptionsCurrencyDisplay = "symbol" | "code" | "name" | "narrowSymbol";
type NumberFormatOptionsCurrencySign = "standard" | "accounting";
type NumberFormatOptionsNotation = NumberFormatNotation;
type NumberFormatOptionsSignDisplay = "auto" | "always" | "never" | "exceptZero" | "negative";
type NumberFormatOptionsUnitDisplay = "long" | "short" | "narrow";
type TrailingZeroDisplay = "auto" | "stripIfInteger";
interface NumberFormatInternal extends NumberFormatDigitInternalSlots {
locale: string;
dataLocale: string;
style: NumberFormatOptionsStyle;
currency?: string;
currencyDisplay: NumberFormatOptionsCurrencyDisplay;
unit?: string;
unitDisplay: NumberFormatOptionsUnitDisplay;
currencySign: NumberFormatOptionsCurrencySign;
notation: NumberFormatOptionsNotation;
compactDisplay: NumberFormatOptionsCompactDisplay;
signDisplay: NumberFormatOptionsSignDisplay;
useGrouping?: UseGroupingType;
pl: Intl.PluralRules;
boundFormat?: Intl.NumberFormat["format"];
numberingSystem: string;
dataLocaleData: NumberFormatLocaleInternalData;
roundingMode: RoundingModeType;
}
type NumberFormatOptions = Omit<Intl.NumberFormatOptions, "signDisplay" | "useGrouping"> & NumberFormatDigitOptions & {
localeMatcher?: NumberFormatOptionsLocaleMatcher;
style?: NumberFormatOptionsStyle;
compactDisplay?: NumberFormatOptionsCompactDisplay;
currencyDisplay?: NumberFormatOptionsCurrencyDisplay;
currencySign?: NumberFormatOptionsCurrencySign;
notation?: NumberFormatOptionsNotation;
signDisplay?: NumberFormatOptionsSignDisplay;
unit?: string;
unitDisplay?: NumberFormatOptionsUnitDisplay;
numberingSystem?: string;
trailingZeroDisplay?: TrailingZeroDisplay;
roundingPriority?: RoundingPriorityType;
roundingIncrement?: number;
roundingMode?: RoundingModeType;
useGrouping?: UseGroupingType;
};
type ResolvedNumberFormatOptions = Intl.ResolvedNumberFormatOptions & Pick<NumberFormatInternal, "currencySign" | "unit" | "unitDisplay" | "notation" | "compactDisplay" | "signDisplay">;
type NumberFormatPartTypes = Intl.NumberFormatPartTypes | "exponentSeparator" | "exponentMinusSign" | "exponentInteger" | "compact" | "unit" | "literal" | "approximatelySign";
interface NumberFormatPart {
type: NumberFormatPartTypes;
value: string;
source?: string;
}
interface NumberRangeToParts extends NumberFormatPart {
result: string;
}
//#endregion
//#region packages/intl-numberformat/types.d.ts
interface NumberFormat$1 {
resolvedOptions(): ResolvedNumberFormatOptions;
formatToParts(x: number | bigint | Decimal | string): NumberFormatPart[];
format(x: number | bigint | Decimal | string): string;
formatRange(start: number | bigint | Decimal | string, end: number | bigint | Decimal | string): string;
formatRangeToParts(start: number | bigint | Decimal | string, end: number | bigint | Decimal | string): NumberRangeToParts[];
}
interface NumberFormatConstructor {
new (locales?: string | string[], options?: NumberFormatOptions): NumberFormat$1;
(locales?: string | string[], options?: NumberFormatOptions): NumberFormat$1;
__addLocaleData(...data: RawNumberLocaleData[]): void;
__addUnitData(locale: string, unitsData: RawNumberLocaleData["data"]["units"]): void;
supportedLocalesOf(locales: string | string[], options?: Pick<NumberFormatOptions, "localeMatcher">): string[];
getDefaultLocale(): string;
__defaultLocale: string;
localeData: Record<string, NumberFormatLocaleInternalData | undefined>;
availableLocales: Set<string>;
polyfilled: boolean;
}
//#endregion
//#region packages/intl-numberformat/core.d.ts
type NumberFormat = NumberFormat$1;
/**
* https://tc39.es/ecma402/#sec-intl-numberformat-constructor
*/
declare const NumberFormat: NumberFormatConstructor;
//#endregion
export { NumberFormat };
//# sourceMappingURL=index.d.ts.map