Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

guard against locale-specific sorting #280

Merged
merged 4 commits into from
May 10, 2021
Merged

Conversation

isaacs
Copy link
Contributor

@isaacs isaacs commented May 6, 2021

Node will use the current environment's locale sorting for
String.localeCompare. Thankfully, String.localeCompare can take a
second argument to specify the locale. Up until Node 13, any valid
argument provided to the function may be respected if Node was
compiled with Intl support, or would always be treated as 'en'
otherwise.

The solution is to always set the locale to 'en'. An alternative
solution would be to compare strings based on byte order, but this does
not provide ideal ergonomis.

To verify this, the locale is set in the test environment to 'sk',
which has a significantly different alphabet, including sorting words
starting with 'ch' after words starting with 'd'.

Re: npm/cli#2829
Fix: #276

References

@isaacs isaacs force-pushed the isaacs/fix-locale-compare branch 2 times, most recently from dc3590b to bf38e1b Compare May 7, 2021 00:29
@isaacs
Copy link
Contributor Author

isaacs commented May 7, 2021

This took a few rounds of updating all the deps in node-tap. Setting the LC_ALL=sk in the test environment shook out a ton of bugs!

@wraithgar
Copy link
Member

node-tap is updated in main so this can be fixed up.

@isaacs isaacs force-pushed the isaacs/fix-locale-compare branch from bf38e1b to 8fbe794 Compare May 10, 2021 16:11
Node will use the current environment's locale sorting for
String.localeCompare.  Thankfully, String.localeCompare can take a
second argument to specify the locale.  Up until Node 13, any valid
argument provided to the function _may_ be respected if Node was
compiled with Intl support, or would always be treated as `'en'`
otherwise.

The solution is to always set the locale to `'en'`.  An alternative
solution would be to compare strings based on byte order, but this does
not provide ideal ergonomis.

To verify this, the locale is set in the test environment to `'sk'`,
which has a significantly different alphabet, including sorting words
starting with `'ch'` _after_ words starting with `'d'`.

Re: npm/cli#2829
Fix: #276
@isaacs isaacs force-pushed the isaacs/fix-locale-compare branch from 4b06448 to a843eea Compare May 10, 2021 16:14
@wraithgar wraithgar merged commit 571e9e4 into main May 10, 2021
@wraithgar wraithgar deleted the isaacs/fix-locale-compare branch May 10, 2021 16:27
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants