Repository navigation
feat(@formatjs/intl-getcanonicallocales): update to latest spec, handle arraylike objs - #5903
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. |
|
|
||
| // Step 5-6: Convert to object and get length for array-like objects | ||
| const O = Object(locales) | ||
| const len = typeof O.length === 'number' ? O.length : 0 |
There was a problem hiding this comment.
The length property is not properly converted according to ECMAScript ToLength specification. If O.length is Infinity, NaN, negative, or non-integer, this will cause issues:
length: Infinitywill cause an infinite looplength: NaNwill skip iteration (k < NaN is always false)length: -5will skip iteration but should be treated as 0length: 3.7will iterate 3 times but should be converted to integer first
Fix by properly converting to a valid length:
const len = Math.max(0, Math.min(Math.floor(Number(O.length) || 0), Number.MAX_SAFE_INTEGER))Or use the spec-compliant ToLength algorithm which clamps between 0 and 2^53-1.
| const len = typeof O.length === 'number' ? O.length : 0 | |
| const len = Math.max(0, Math.min(Math.floor(Number(O.length) || 0), Number.MAX_SAFE_INTEGER)) |
Spotted by Graphite Agent
Is this helpful? React 👍 or 👎 to let us know.
…le arraylike objs (#5903) ### TL;DR Added support for Intl.Locale objects and array-like objects in getCanonicalLocales. ### What changed? - Enhanced `getCanonicalLocales` to accept Intl.Locale objects as input - Added support for array-like objects as input parameters - Implemented proper type checking for all input types - Improved error handling with more specific error messages - Updated the function signature to reflect the new supported parameter types - Added comprehensive test cases for all new functionality ### How to test? Run the test suite which includes new tests for: - Single Intl.Locale object input - Arrays of Intl.Locale objects - Mixed arrays of strings and Intl.Locale objects - Array-like objects with various content types - Error cases for invalid inputs Examples: ```js // Test with Intl.Locale objects getCanonicalLocales(new Intl.Locale('en-US')) getCanonicalLocales([new Intl.Locale('en-US'), new Intl.Locale('fr-FR')]) // Test with array-like objects getCanonicalLocales({0: 'en-US', 1: 'fr-FR', length: 2}) ``` ### Why make this change? This change brings the implementation closer to the ECMAScript specification, which allows for Intl.Locale objects as input to getCanonicalLocales. It also improves compatibility with array-like objects, making the function more flexible and robust in different usage scenarios.
Merge activity
|
Merge activity
|
…le arraylike objs
1fec62b to
3ffe1bd
Compare
28ed005 to
3ff1185
Compare

TL;DR
Added support for Intl.Locale objects and array-like objects in getCanonicalLocales.
What changed?
getCanonicalLocalesto accept Intl.Locale objects as inputHow to test?
Run the test suite which includes new tests for:
Examples:
Why make this change?
This change brings the implementation closer to the ECMAScript specification, which allows for Intl.Locale objects as input to getCanonicalLocales. It also improves compatibility with array-like objects, making the function more flexible and robust in different usage scenarios.