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

i18n: treat Infinity and NaN as numeric values #9687

Merged
merged 3 commits into from
Sep 18, 2019
Merged

Conversation

brendankenny
Copy link
Member

fixes the crasher part of #9684

The issue was that we were JSON.parse(JSON.stringify(values))ing to make sure we weren't modifying the original ICU replacement values, but both Infinity and NaN get converted to null when stringified, which then fails the check that we're only replacing numeric placeholders with numbers.

Instead, this copies over the values one by one to a new object, formatting if needed. Also smushes _preformatValues and _processParsedElements together because the first was mostly just calling the second, but the first had a way better name.


it('formats correctly with NaN and Infinity numeric values', () => {
const helloInfinityStr = str_(UIStrings.helloBytesWorld, {in: Infinity});
expect(helloInfinityStr).toBeDisplayString('Hello ∞ World');
Copy link
Member Author

Choose a reason for hiding this comment

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

expect(helloInfinityStr).toBeDisplayString('Hello ∞ World');

I guess this is how it's supposed to get formatted? 🤷‍♀

Copy link
Member

Choose a reason for hiding this comment

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

◊◊◊

Copy link
Member

Choose a reason for hiding this comment

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

∞∞∞∞∞∞∞v∞

Copy link
Member

@exterkamp exterkamp left a comment

Choose a reason for hiding this comment

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

LGTM, seems like a good fix and makes i18n a little simpler!

values[id] = value / 1024;
formattedValues[id] = value / 1024;
} else {
// All other numbers passed through unchanged.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// All other numbers passed through unchanged.
// All other numbers (e.g. ∞) passed through unchanged.

Copy link
Member Author

Choose a reason for hiding this comment

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

Infinity could be passed to the other styles as well (the issue in #9684 is passing it through bytes, it's just that Infinity / 1024 === Infinity :)

Copy link
Member Author

Choose a reason for hiding this comment

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

tried to make this comment clearer as well

}
}

// Throw an error if a value is provided but has no placeholder in the message.
for (const valueId of Object.keys(values)) {
// errorCode is a special case always allowed to help ease of LHError use.
if (valueId === 'errorCode') continue;
if (valueId in formattedValues) continue;
Copy link
Member

Choose a reason for hiding this comment

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

Nice elimination of a find


it('formats correctly with NaN and Infinity numeric values', () => {
const helloInfinityStr = str_(UIStrings.helloBytesWorld, {in: Infinity});
expect(helloInfinityStr).toBeDisplayString('Hello ∞ World');
Copy link
Member

Choose a reason for hiding this comment

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

∞∞∞∞∞∞∞v∞

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants