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

fix: format should follow locale order #1619

Merged
merged 3 commits into from
Apr 27, 2023

Conversation

vonovak
Copy link
Collaborator

@vonovak vonovak commented Apr 25, 2023

Description

fixes a bug in formatting: cacheKey would sort locales to obtain cache key; however, the order of passed locales matters.

The tests that I added would previously fail

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update

Fixes # (issue)

Checklist

  • I have read the CONTRIBUTING and CODE_OF_CONDUCT docs
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

@vercel
Copy link

vercel bot commented Apr 25, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
js-lingui ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 25, 2023 9:16pm

@github-actions
Copy link

github-actions bot commented Apr 25, 2023

size-limit report 📦

Path Size
./packages/core/dist/index.mjs 1.44 KB (0%)
./packages/detect-locale/dist/index.mjs 721 B (0%)
./packages/react/dist/index.mjs 1.6 KB (0%)
./packages/remote-loader/dist/index.mjs 7.24 KB (0%)

@codecov
Copy link

codecov bot commented Apr 25, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: +0.01 🎉

Comparison is base (73081c2) 75.53% compared to head (a21850a) 75.55%.

Additional details and impacted files
@@            Coverage Diff             @@
##             next    #1619      +/-   ##
==========================================
+ Coverage   75.53%   75.55%   +0.01%     
==========================================
  Files          77       77              
  Lines        1991     1988       -3     
  Branches      520      517       -3     
==========================================
- Hits         1504     1502       -2     
+ Misses        374      373       -1     
  Partials      113      113              
Impacted Files Coverage Δ
packages/core/src/formats.ts 100.00% <100.00%> (+3.12%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

expect(date(["en"], new Date(2023, 2, 5))).toMatchInlineSnapshot(
`"3/5/2023"`
)
expect(date(["en"], new Date(2023, 2, 5))).toBe("3/5/2023")
Copy link
Contributor

Choose a reason for hiding this comment

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

it might be intended to use toMatchInlineSnapshot to make sure string format is generated. But I will leave it up to @thekip

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

if you compare x with "3/5/2023" using toBe, then it's immediately clear that x is a string, so I don't see that as an issue 🙂

Copy link
Collaborator

Choose a reason for hiding this comment

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

it's just easier to write tests from scratch with toMatchInlineSnapshot. Here when @vonovak refactor tests you already have an expected result. When i wrote them, expected result had to be figured out, so with toMatchInlineSnaphsot is just more convenient, no more difference here

locales?: readonly string[],
options: unknown = {}
locales: readonly string[],
options?: Intl.DateTimeFormatOptions | Intl.NumberFormatOptions
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe default to {} as before, the differences are:

JSON.stringify(undefined)
undefined
JSON.stringify({})
'{}'

so it affects the generated cache key, unless you intend to make cacheKey(type, locales) to generate a different key compared to cacheKey(type, locales, {}) ?

Copy link
Collaborator Author

@vonovak vonovak Apr 26, 2023

Choose a reason for hiding this comment

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

yes, I can revert that part

unless you intend to make cacheKey(type, locales) to generate a different key compared to cacheKey(type, locales, {})

no, that is not an intent but a side effect. I thought, "how are people going to call this"? And I think it's unlikely they would call i18n. number(123, {}). However, if they do call i18n. number(123, {}) and i18n. number(123}) then it's true that the cache would have 2 entries for the same number formatter which is not great but I also thought about the default parameter options = {} which means that every time i18n. number(123}) is called there is an empty object allocated and disposed. It's not important though, I can revert this part...

@thekip
Copy link
Collaborator

thekip commented Apr 26, 2023

Kinda very corner case, but it's good to be caught and fixed.

Just out of curiosity, have you really encountered this issue on the real project or just spotted the issue looking in the codebase?

@vonovak
Copy link
Collaborator Author

vonovak commented Apr 26, 2023

Kinda very corner case, but it's good to be caught and fixed.

Just out of curiosity, have you really encountered this issue on the real project or just spotted the issue looking in the codebase?

just randomly spotted it :)

@andrii-bodnar andrii-bodnar changed the base branch from next to main April 26, 2023 10:03
@andrii-bodnar
Copy link
Contributor

@vonovak @thekip can we merge this?

@thekip
Copy link
Collaborator

thekip commented Apr 27, 2023

yes

@andrii-bodnar andrii-bodnar merged commit 4d53b7b into lingui:main Apr 27, 2023
16 checks passed
@vonovak vonovak deleted the fix/format-locale-order branch April 27, 2023 12:59
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.

None yet

4 participants