Skip to content

feat(@formatjs/intl-getcanonicallocales): update to latest spec, handle arraylike objs - #5903

Closed
longlho wants to merge 1 commit into
mainfrom
01-14-feat_formatjs_intl-getcanonicallocales_update_to_latest_spec_handle_arraylike_objs
Closed

longlho wants to merge 1 commit into
mainfrom
01-14-feat_formatjs_intl-getcanonicallocales_update_to_latest_spec_handle_arraylike_objs

Conversation

@longlho

@longlho longlho commented Jan 15, 2026 •

Copy link
Copy Markdown
Member

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:

// 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.

@longlho
longlho marked this pull request as ready for review January 15, 2026 02:10

longlho commented Jan 15, 2026 •

Copy link
Copy Markdown
Member Author

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.
Learn more


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • main - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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: Infinity will cause an infinite loop
  • length: NaN will skip iteration (k < NaN is always false)
  • length: -5 will skip iteration but should be treated as 0
  • length: 3.7 will 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.

Suggested change
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

Fix in Graphite


Is this helpful? React 👍 or 👎 to let us know.

graphite-app Bot pushed a commit that referenced this pull request Jan 15, 2026
…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.
@graphite-app

graphite-app Bot commented Jan 15, 2026

Copy link
Copy Markdown

Merge activity

@graphite-app

graphite-app Bot commented Jan 15, 2026 •

Copy link
Copy Markdown

Merge activity

  • Jan 15, 2:24 AM UTC: CI is running for this pull request on a draft pull request (#5906) due to your merge queue CI optimization settings.
  • Jan 15, 2:40 AM UTC: Merged by the Graphite merge queue via draft PR: #5906.

@graphite-app
graphite-app Bot changed the base branch from 01-14-feat_upgrade_cldr_to_48.1.0 to graphite-base/5903 January 15, 2026 02:25
@longlho
longlho force-pushed the graphite-base/5903 branch from 1fec62b to 3ffe1bd Compare January 15, 2026 02:33
@longlho
longlho force-pushed the 01-14-feat_formatjs_intl-getcanonicallocales_update_to_latest_spec_handle_arraylike_objs branch from 28ed005 to 3ff1185 Compare January 15, 2026 02:33
@longlho
longlho changed the base branch from graphite-base/5903 to main January 15, 2026 02:33
@graphite-app graphite-app Bot closed this Jan 15, 2026
@graphite-app
graphite-app Bot deleted the 01-14-feat_formatjs_intl-getcanonicallocales_update_to_latest_spec_handle_arraylike_objs branch January 15, 2026 02:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant