@formatjs/intl-pluralrules
Version:
Polyfill for Intl.PluralRules
44 lines (43 loc) • 2.19 kB
TypeScript
import { type LDMLPluralRule, type PluralRulesInternal } from "@formatjs/ecma402-abstract";
import Decimal from "decimal.js";
import { type OperandsRecord } from "./GetOperands.js";
/**
* Result of ResolvePluralInternal containing both the formatted string and plural category.
* This corresponds to a Record with [[FormattedString]] and [[PluralCategory]] fields
* as described in the ECMA-402 spec for ResolvePluralRange.
*/
export interface ResolvePluralResult {
/** The formatted representation of the number */
formattedString: string;
/** The LDML plural category (zero, one, two, few, many, or other) */
pluralCategory: LDMLPluralRule;
}
/**
* ResolvePluralInternal ( pluralRules, n )
*
* Internal version of ResolvePlural that returns both the formatted string and plural category.
* This is needed for selectRange, which must compare formatted strings to determine if the
* start and end values are identical.
*
* The formatted string is obtained by applying the number formatting options (digit options)
* from the PluralRules object to the input number. This ensures that formatting-sensitive
* plural rules work correctly (e.g., rules that depend on visible fraction digits).
*
* @param pl - An initialized PluralRules object
* @param n - Mathematical value to resolve
* @returns Record containing the formatted string and plural category
*/
export declare function ResolvePluralInternal(pl: Intl.PluralRules, n: Decimal, { getInternalSlots, PluralRuleSelect }: {
getInternalSlots(pl: Intl.PluralRules): PluralRulesInternal;
PluralRuleSelect: (locale: string, type: "cardinal" | "ordinal", n: Decimal, operands: OperandsRecord) => LDMLPluralRule;
}): ResolvePluralResult;
/**
* http://ecma-international.org/ecma-402/7.0/index.html#sec-resolveplural
* @param pl
* @param n
* @param PluralRuleSelect Has to pass in bc it's implementation-specific
*/
export declare function ResolvePlural(pl: Intl.PluralRules, n: Decimal, { getInternalSlots, PluralRuleSelect }: {
getInternalSlots(pl: Intl.PluralRules): PluralRulesInternal;
PluralRuleSelect: (locale: string, type: "cardinal" | "ordinal", n: Decimal, operands: OperandsRecord) => LDMLPluralRule;
}): LDMLPluralRule;