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

Add formatter cache #1843

Merged
merged 2 commits into from
Oct 6, 2022
Merged

Add formatter cache #1843

merged 2 commits into from
Oct 6, 2022

Conversation

pwolaq
Copy link
Contributor

@pwolaq pwolaq commented Oct 5, 2022

This PR resolves #1841.

Few considerations:

  • maybe we should export createCachedFormatter as a utility function so that others may reuse it when creating custom formatters?
  • I had no idea how to test that functionality apart from somehow stubbing Intl constructors and verifying that they are not called multiple times - I can try to add this test if you would like to have it
  • as cache keys are always strings I decided to use object as caching structure instead of Map (which might cause compatibility issues in older browsers)

Checklist

  • only relevant code is changed (make a diff before you submit the PR)
  • run tests npm run test
  • tests are included
  • commit message and code follows the Developer's Certification of Origin

Checklist (for documentation change)

  • only relevant documentation part is changed (make a diff before you submit the PR)
  • motivation/reason is provided
  • commit message and code follows the Developer's Certification of Origin

@coveralls
Copy link

coveralls commented Oct 5, 2022

Coverage Status

Coverage increased (+0.1%) to 91.151% when pulling 48fcdef on pwolaq:formatter-cache into 69313fc on i18next:master.

src/Formatter.js Show resolved Hide resolved
@adrai
Copy link
Member

adrai commented Oct 5, 2022

As alternative to the exposure of the createCachedFormatter function, we could extend the add function... something like add(name, fc, getFormatterFc) and cache that automatically... just an idea before going to bed 😉

@adrai
Copy link
Member

adrai commented Oct 6, 2022

As alternative to the exposure of the createCachedFormatter function, we could extend the add function... something like add(name, fc, getFormatterFc) and cache that automatically... just an idea before going to bed 😉

what do you think about something like this?

add(name, fc) {
  this.formats[name.toLowerCase().trim()] = fc;
},

addCached(name, fc) {
  this.formats[name.toLowerCase().trim()] = createCachedFormatter(fc)
}


i18next.services.formatter.addCached('my-format', (lng, options) => {
  const formatter = new Intl.NumberFormat(lng, options);
  return (val) => formatter.format(val);
})

@adrai adrai merged commit e12564d into i18next:master Oct 6, 2022
@adrai
Copy link
Member

adrai commented Oct 6, 2022

landed in v21.10.0

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.

Caching Intl formatters
3 participants