@formatjs/intl-pluralrules
Version:
Polyfill for Intl.PluralRules
100 lines • 4.08 kB
TypeScript
import "@formatjs/bigdecimal";
declare const nullClassBase: abstract new () => object;
type Locale = string;
interface LocaleData<T> {
data: T;
locale: Locale;
}
type LDMLPluralRule = "zero" | "one" | "two" | "few" | "many" | "other";
interface PluralRangesData {
cardinal?: Record<string, LDMLPluralRule>;
ordinal?: Record<string, LDMLPluralRule>;
}
type CompactExponentData = Record<"short" | "long", Record<number, number>>;
interface PluralRulesData {
compactExponents?: CompactExponentData;
categories: {
cardinal: string[];
ordinal: string[];
};
fn: (val: number | string, ord?: boolean, exponent?: number) => LDMLPluralRule;
pluralRanges?: PluralRangesData;
}
type PluralRulesLocaleData = LocaleData<PluralRulesData>;
interface PluralRulesInternal extends NumberFormatDigitInternalSlots {
initializedPluralRules: boolean;
locale: string;
localeData?: PluralRulesData;
type: "cardinal" | "ordinal";
notation: "standard" | "scientific" | "engineering" | "compact";
compactDisplay?: "short" | "long";
compactExponents?: CompactExponentData;
}
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";
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 TrailingZeroDisplay = "auto" | "stripIfInteger" | "stripToMinimum";
/**
* Type augmentation for Intl.PluralRules
*
* ECMA-402 Spec: selectRange method (Intl.PluralRules.prototype.selectRange)
* https://tc39.es/ecma402/#sec-intl.pluralrules.prototype.selectrange
*
* ECMA-402 notation and compactDisplay options
* Mirrors Intl.NumberFormat notation option for proper plural selection with compact numbers
*/
declare global {
namespace Intl {
interface PluralRules {
select(value?: number | bigint | string): LDMLPluralRule;
selectRange(start: number | bigint | string, end: number | bigint | string): LDMLPluralRule;
}
interface ResolvedPluralRulesOptions {
trailingZeroDisplay: NumberFormatDigitInternalSlots["trailingZeroDisplay"];
}
interface PluralRulesOptions extends NumberFormatDigitOptions {
notation?: "standard" | "scientific" | "engineering" | "compact";
compactDisplay?: "short" | "long";
}
}
}
export declare class PluralRules extends nullClassBase {
constructor(locales?: string | string[], options?: Intl.PluralRulesOptions);
resolvedOptions(): Intl.ResolvedPluralRulesOptions;
select(val?: number | bigint | string): LDMLPluralRule;
selectRange(start: number | bigint | string, end: number | bigint | string): LDMLPluralRule;
static supportedLocalesOf(locales?: string | string[], options?: Pick<Intl.PluralRulesOptions, "localeMatcher">): string[];
static __addLocaleData(...data: PluralRulesLocaleData[]): void;
static localeData: Record<string, PluralRulesData>;
static availableLocales: Set<string>;
static __defaultLocale: string;
static getDefaultLocale(): string;
static relevantExtensionKeys: never[];
static polyfilled: boolean;
}
export type { PluralRulesInternal };