@formatjs/intl-pluralrules
- Version 6.3.15
- Published
- 972 kB
- 2 dependencies
- MIT license
Install
npm i @formatjs/intl-pluralrulesyarn add @formatjs/intl-pluralrulespnpm add @formatjs/intl-pluralrulesOverview
Polyfill for Intl.PluralRules
Index
Classes
Interfaces
Type Aliases
Namespaces
Classes
class PluralRules
class PluralRules {}constructor
constructor(locales?: string | string[], options?: Intl.PluralRulesOptions);property availableLocales
static availableLocales: Set<string>;property localeData
static localeData: Record<string, PluralRulesData>;property polyfilled
static polyfilled: boolean;property relevantExtensionKeys
static relevantExtensionKeys: never[];method getDefaultLocale
static getDefaultLocale: () => string;method resolvedOptions
resolvedOptions: () => Intl.ResolvedPluralRulesOptions;method select
select: (val: number | bigint) => LDMLPluralRule;method selectRange
selectRange: (start: number | bigint, end: number | bigint) => LDMLPluralRule;Intl.PluralRules.prototype.selectRange ( start, end )
Returns a string indicating which plural rule applies to a range of numbers. This is useful for formatting ranges like "1-2 items" vs "2-3 items" where different languages have different plural rules for ranges.
Specification: https://tc39.es/ecma402/#sec-intl.pluralrules.prototype.selectrange
Parameter start
The start value of the range (number or bigint)
Parameter end
The end value of the range (number or bigint)
Returns
The plural category for the range (zero, one, two, few, many, or other)
Throws
{TypeError} If start or end is undefined
Throws
{RangeError} If start or end converts to NaN
Chrome's native implementation (as of early 2025) has a bug where it throws "Cannot convert a BigInt value to a number" when using BigInt arguments. This is a browser bug - the spec requires BigInt support. This polyfill handles BigInt correctly.
Example 1
const pr = new Intl.PluralRules('en'); pr.selectRange(1, 2); // "other" (English: "1-2 items") pr.selectRange(1, 1); // "one" (same value: "1 item")
Example 2
const prFr = new Intl.PluralRules('fr'); prFr.selectRange(0, 1); // "one" (French: "0-1 vue") prFr.selectRange(1, 2); // "other" (French: "1-2 vues")
Example 3
// BigInt support (spec-compliant, but Chrome has a bug as of early 2025) pr.selectRange(BigInt(1), BigInt(2)); // "other"
method supportedLocalesOf
static supportedLocalesOf: ( locales?: string | string[], options?: Pick<Intl.PluralRulesOptions, 'localeMatcher'>) => string[];method toString
toString: () => string;Interfaces
interface LocaleData
interface LocaleData<T> {}interface NumberFormatDigitInternalSlots
interface NumberFormatDigitInternalSlots {}property maximumFractionDigits
maximumFractionDigits: number;property maximumSignificantDigits
maximumSignificantDigits: number;property minimumFractionDigits
minimumFractionDigits: number;property minimumIntegerDigits
minimumIntegerDigits: number;property minimumSignificantDigits
minimumSignificantDigits: number;property notation
notation: NumberFormatNotation;property roundingIncrement
roundingIncrement: number;property roundingMode
roundingMode: RoundingModeType;property roundingPriority
roundingPriority: RoundingPriorityType;property roundingType
roundingType: NumberFormatRoundingType;property trailingZeroDisplay
trailingZeroDisplay: TrailingZeroDisplay;interface PluralRangesData
interface PluralRangesData {}interface PluralRulesData
interface PluralRulesData {}property categories
categories: { cardinal: string[]; ordinal: string[];};property compactExponents
compactExponents?: CompactExponentData;property fn
fn: (val: number | string, ord?: boolean, exponent?: number) => LDMLPluralRule;property pluralRanges
pluralRanges?: PluralRangesData;interface PluralRulesInternal
interface PluralRulesInternal extends NumberFormatDigitInternalSlots {}property compactDisplay
compactDisplay?: 'short' | 'long';property compactExponents
compactExponents?: CompactExponentData;property initializedPluralRules
initializedPluralRules: boolean;property locale
locale: string;property notation
notation: 'standard' | 'scientific' | 'engineering' | 'compact';property type
type: 'cardinal' | 'ordinal';Type Aliases
type CompactExponentData
type CompactExponentData = Record<'short' | 'long', Record<number, number>>;type LDMLPluralRule
type LDMLPluralRule = 'zero' | 'one' | 'two' | 'few' | 'many' | 'other';type Locale
type Locale = string;type NumberFormatNotation
type NumberFormatNotation = 'standard' | 'scientific' | 'engineering' | 'compact';type NumberFormatRoundingType
type NumberFormatRoundingType = | 'morePrecision' | 'lessPrecision' | 'significantDigits' | 'fractionDigits';type PluralRulesLocaleData
type PluralRulesLocaleData = LocaleData<PluralRulesData>;type RoundingModeType
type RoundingModeType = | 'ceil' | 'floor' | 'expand' | 'trunc' | 'halfCeil' | 'halfFloor' | 'halfExpand' | 'halfTrunc' | 'halfEven';type RoundingPriorityType
type RoundingPriorityType = 'auto' | 'morePrecision' | 'lessPrecision';type TrailingZeroDisplay
type TrailingZeroDisplay = 'auto' | 'stripIfInteger';Namespaces
namespace global
namespace global {}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
namespace global.Intl
namespace global.Intl {}interface PluralRules
interface PluralRules {}method selectRange
selectRange: (start: number | bigint, end: number | bigint) => LDMLPluralRule;interface PluralRulesOptions
interface PluralRulesOptions {}property compactDisplay
compactDisplay?: 'short' | 'long';property notation
notation?: 'standard' | 'scientific' | 'engineering' | 'compact';Package Files (1)
Dependencies (2)
Dev Dependencies (0)
No dev dependencies.
Peer Dependencies (0)
No peer dependencies.
Badge
To add a badge like this oneto your package's README, use the codes available below.
You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/@formatjs/intl-pluralrules.
- Markdown[](https://www.jsdocs.io/package/@formatjs/intl-pluralrules)
- HTML<a href="https://www.jsdocs.io/package/@formatjs/intl-pluralrules"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>
- Updated .
Package analyzed in 2373 ms. - Missing or incorrect documentation? Open an issue for this package.
