Repository navigation
feat(@formatjs/intl-pluralrules): replace make-plural-compiler with native implementation and add BigInt support - #5910
Conversation
|
Warning This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
How to use the Graphite Merge QueueAdd either label to this PR to merge it via the merge queue:
You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has required the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. This stack of pull requests is managed by Graphite. Learn more about stacking. |
| ['n', 'Math.abs(parseFloat(numStr))'], | ||
| ['i', 'Math.floor(Math.abs(parseFloat(integerPart)))'], |
There was a problem hiding this comment.
Critical precision loss for huge numbers in modulo operations
The operand i uses parseFloat(integerPart) which will lose precision for numbers exceeding Number.MAX_SAFE_INTEGER (9007199254740991). This breaks modulo operations like i % 10 or i % 100 for truly huge numbers.
For example, the string "900719925474099991" would be parsed to 900719925474099970 (due to floating-point rounding), causing i % 10 to incorrectly return 7 instead of 1. This breaks plural/ordinal rules that depend on the last digits.
Fix: For huge numbers, implement string-based modulo operations:
// For numbers exceeding MAX_SAFE_INTEGER, use string-based modulo
function safeMod(numStr: string, divisor: number): number {
if (numStr.length > 15) { // Rough check for potentially unsafe numbers
// String-based modulo for last digits
if (divisor === 10) return parseInt(numStr.slice(-1), 10) || 0
if (divisor === 100) return parseInt(numStr.slice(-2), 10) || 0
if (divisor === 1000) return parseInt(numStr.slice(-3), 10) || 0
}
return Math.floor(Math.abs(parseFloat(numStr))) % divisor
}Or document that the implementation only supports numbers up to MAX_SAFE_INTEGER despite the PR title claiming "huge numbers" support.
Spotted by Graphite Agent
Is this helpful? React 👍 or 👎 to let us know.
519ab1f to
e554504
Compare
Merge activity
|
…ative implementation and add BigInt support (#5910) ### TL;DR Removed `make-plural-compiler` dependency and implemented a custom plural rules compiler with improved BigInt support. ### What changed? - Removed the `make-plural-compiler` dependency and its associated patch file - Created a new custom `plural-rules-compiler.ts` implementation that handles CLDR plural rules - Enhanced `ToIntlMathematicalValue` and `ToNumber` to properly support BigInt values - Fixed edge cases in `ApplyUnsignedRoundingMode` for handling boundary values - Updated `GetOperands` to handle very large integer values that exceed JavaScript's number limits - Added comprehensive tests for BigInt and large number handling - Updated Bazel dependencies to newer versions ### How to test? 1. Run the test suite with `pnpm test` to verify the new implementation works correctly 2. Test with BigInt values: ```js const pr = new Intl.PluralRules('en'); console.log(pr.select(1n)); // "one" console.log(pr.select(2n)); // "other" const ordinal = new Intl.PluralRules('en', {type: 'ordinal'}); console.log(ordinal.select(1n)); // "one" console.log(ordinal.select(2n)); // "two" console.log(ordinal.select(3n)); // "few" ``` 3. Test with very large numbers: ```js console.log(pr.select(10000000000000n)); // "other" console.log(ordinal.select(10000000000001n)); // "one" ``` ### Why make this change? The `make-plural-compiler` dependency had limitations in handling BigInt values and very large numbers. This change improves compatibility with the ECMAScript specification by properly supporting BigInt values in `Intl.PluralRules.prototype.select()` as required by ECMA-402. The custom implementation also provides better handling of edge cases and maintains precision for very large numbers that exceed JavaScript's safe integer limits.
…pport for huge numbers
e554504 to
e933aa8
Compare
835e2c9 to
6f83aad
Compare

TL;DR
Removed
make-plural-compilerdependency and implemented a custom plural rules compiler with improved BigInt support.What changed?
make-plural-compilerdependency and its associated patch fileplural-rules-compiler.tsimplementation that handles CLDR plural rulesToIntlMathematicalValueandToNumberto properly support BigInt valuesApplyUnsignedRoundingModefor handling boundary valuesGetOperandsto handle very large integer values that exceed JavaScript's number limitsHow to test?
pnpm testto verify the new implementation works correctlyWhy make this change?
The
make-plural-compilerdependency had limitations in handling BigInt values and very large numbers. This change improves compatibility with the ECMAScript specification by properly supporting BigInt values inIntl.PluralRules.prototype.select()as required by ECMA-402. The custom implementation also provides better handling of edge cases and maintains precision for very large numbers that exceed JavaScript's safe integer limits.