Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: locale aware sorting #2906

Draft
wants to merge 5 commits into
base: next
Choose a base branch
from

Conversation

matthewmayer
Copy link
Contributor

@matthewmayer matthewmayer commented May 17, 2024

Draft implementation for #2905

Uses the locale key to customize the sort order.

Requires Intl

The actual changes are in scripts/generate-locales.ts

Copy link

netlify bot commented May 17, 2024

Deploy Preview for fakerjs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 2c5c169
🔍 Latest deploy log https://app.netlify.com/sites/fakerjs/deploys/664739b31d31790008943285
😎 Deploy Preview https://deploy-preview-2906.fakerjs.dev
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

codecov bot commented May 17, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.95%. Comparing base (7fb9bb5) to head (2c5c169).

Additional details and impacted files
@@            Coverage Diff             @@
##             next    #2906      +/-   ##
==========================================
- Coverage   99.96%   99.95%   -0.01%     
==========================================
  Files        2977     2977              
  Lines      215466   215466              
  Branches      950      950              
==========================================
- Hits       215394   215375      -19     
- Misses         72       91      +19     
Files Coverage Δ
src/locales/ar/commerce/department.ts 100.00% <100.00%> (ø)
src/locales/ar/vehicle/manufacturer.ts 100.00% <100.00%> (ø)
src/locales/ar/vehicle/model.ts 100.00% <100.00%> (ø)
src/locales/az/color/human.ts 100.00% <100.00%> (ø)
src/locales/az/commerce/department.ts 100.00% <100.00%> (ø)
src/locales/az/commerce/product_name.ts 100.00% <100.00%> (ø)
src/locales/az/company/prefix.ts 100.00% <100.00%> (ø)
src/locales/base/color/space.ts 100.00% <100.00%> (ø)
src/locales/da/company/buzz_adjective.ts 100.00% <100.00%> (ø)
src/locales/da/company/buzz_noun.ts 100.00% <100.00%> (ø)
... and 177 more

... and 2 files with indirect coverage changes

scripts/generate-locales.ts Outdated Show resolved Hide resolved
scripts/generate-locales.ts Outdated Show resolved Hide resolved
@@ -270,7 +270,7 @@ async function updateLocaleFileHook(
console.log(`${filePath} <-> ${locale} @ ${definitionKey} -> ${entryName}`);
}

return normalizeLocaleFile(filePath, definitionKey);
return normalizeLocaleFile(filePath, definitionKey, locale);
Copy link
Member

Choose a reason for hiding this comment

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

IMO we should retain the same parameter order as updateLocaleFileHook for consistency.

let collator = null;
try {
// eslint-disable-next-line no-restricted-globals
collator = new Intl.Collator(locale.replaceAll('_', '-'));
Copy link
Member

Choose a reason for hiding this comment

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

Optional: IMO you could store the tranformed locale name in a separate value, then you only have to access Intl once.

const localeName = locale === base ? en : locale.replaceAll

Then you theoretically dont need the catch either.

// eslint-disable-next-line no-restricted-globals
collator = new Intl.Collator('en');
} else {
throw(`Failed to create collator for locale ${locale}. Using default collator.`);
Copy link
Member

Choose a reason for hiding this comment

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

Please throw a proper error, adjust the message and include the cause.

@ST-DDT ST-DDT added p: 1-normal Nothing urgent c: locale Permutes locale definitions c: infra Changes to our infrastructure or project setup labels May 17, 2024
@ST-DDT ST-DDT added this to the v9.0 milestone May 17, 2024
@ST-DDT ST-DDT linked an issue May 17, 2024 that may be closed by this pull request
@matthewmayer
Copy link
Contributor Author

i want to see if the team think this approach is desirable before getting too nitpicky with this PR?

@ST-DDT
Copy link
Member

ST-DDT commented May 17, 2024

I dont see any drawbacks.

@matthewmayer
Copy link
Contributor Author

Possible drawbacks

  • Most other tools are not locale aware eg if I select a bunch of lines in VS code and sort them I'll get a different order
  • won't work in environments with no Intl support
  • changes in ICU between different versions of node might cause different sort orders
  • case insensitive sorting makes it harder to spot items with inconsistent casing to other entries.

@ST-DDT
Copy link
Member

ST-DDT commented May 18, 2024

Thanks for listing all the potential drawbacks.

  • Most other tools are not locale aware eg if I select a bunch of lines in VS code and sort them I'll get a different order

True, but that is even the case without locale aware sorting as they treat upper and lowercase differently sometimes, words with suffixes are even worse due to the ' behind it that messes their order up.

I consider this a low barrier or entry as we require node for building anyway and node should always come with Intl included AFAIK.
What do the others think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: infra Changes to our infrastructure or project setup c: locale Permutes locale definitions p: 1-normal Nothing urgent
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Check whether the locale data should use locale aware sorting
2 participants