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 unformat number in react-i18n #1215

Merged
merged 5 commits into from
Jan 13, 2020
Merged

🐛Fix unformat number in react-i18n #1215

merged 5 commits into from
Jan 13, 2020

Conversation

j-rooks
Copy link
Contributor

@j-rooks j-rooks commented Dec 9, 2019

Description

Issue: https://github.com/Shopify/intl-new-commerce-patterns/issues/916

Fixes an issue with unformatNumber when the thousand separator symbol is a period.
Ideally, we would use the formatToParts method to fix the issue but that is still experimental.

Before:

out

After:

out

Type of change

  • react-i18n Patch: Bug/ Documentation fix (non-breaking change which fixes an issue or adds documentation)

Checklist

  • I have added a changelog entry, prefixed by the type of change noted above

@j-rooks j-rooks changed the title [WIP] Fix unformat number in react-i18n Fix unformat number in react-i18n Dec 10, 2019
@j-rooks j-rooks changed the title Fix unformat number in react-i18n 🐛Fix unformat number in react-i18n Dec 10, 2019

const formatted = '123.456,7891';

expect(i18n.unformatNumber(formatted)).toBe(input.toString());
});

it('handles number with space as the thousand symbol', () => {
formatNumber.mockImplementationOnce(() => '123 456,7');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could not find any examples of languages using a space for the thousand separator and a period for the decimal.
See: https://docs.oracle.com/cd/E19455-01/806-0169/overview-9/index.html

Copy link
Contributor

@benoitzohar benoitzohar left a comment

Choose a reason for hiding this comment

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

LGTM, but I would add a test (or tests) for getSeparators()

@j-rooks
Copy link
Contributor Author

j-rooks commented Dec 10, 2019

LGTM, but I would add a test (or tests) for getSeparators()

@benoitzohar Actually, I'm not seeing any tests for helper functions such as normalizedNumber in i18n.ts. That makes sense to me since testing unformatNumber already tests for normalizedNumber and getSeparators.

@benoitzohar
Copy link
Contributor

@adjnor It's true.
However, it feels weird to mock formatNumber but not getSeparators and normalizedNumber.
I would be consistent in the tests, either mocking all the helpers to only test the function unformatNumber (and have tests for each helper), or not mock formatNumber.
But since formatNumber() is tested already, my favorite solution would be the first one (mock all helpers and add tests).

It might be nitpicking, let me know what you think about this.

@j-rooks
Copy link
Contributor Author

j-rooks commented Dec 10, 2019

As we discussed IRL, I'll update the PR to move the logic from getSeparators into numberSymbols since both functions are used for the same thing.

@j-rooks j-rooks force-pushed the fix_unformat_number branch 2 times, most recently from 59baccf to faccd42 Compare December 10, 2019 21:54
@j-rooks j-rooks requested review from a team and removed request for a team December 17, 2019 15:35
Copy link
Contributor

@lukashlavacka lukashlavacka left a comment

Choose a reason for hiding this comment

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

Good catch and a nice fix. My only concern is the computational cost of numberSymbols. I am not sure if we could memoize something or just define some one-time evaluated const, I assume the i18n formatting cannot change without reloading the page (and therefore the javascript).

@j-rooks
Copy link
Contributor Author

j-rooks commented Jan 6, 2020

@lukashlavacka Good point, I added the @memoize() decorator to the numberSymbols method.
The failing CI doesn't seem to be related to my changes, should be fixed once this PR ships

Copy link
Contributor

@lukashlavacka lukashlavacka left a comment

Choose a reason for hiding this comment

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

Changes, tests and 🎩 LGTM!

@j-rooks j-rooks merged commit 2f137b5 into master Jan 13, 2020
@j-rooks j-rooks deleted the fix_unformat_number branch January 13, 2020 14:47
@lemonmade lemonmade temporarily deployed to production January 14, 2020 20:54 Inactive
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

7 participants