-
Notifications
You must be signed in to change notification settings - Fork 9.5k
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
Conversation
|
||
it('formats correctly with NaN and Infinity numeric values', () => { | ||
const helloInfinityStr = str_(UIStrings.helloBytesWorld, {in: Infinity}); | ||
expect(helloInfinityStr).toBeDisplayString('Hello ∞ World'); |
There was a problem hiding this comment.
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? 🤷♀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
◊◊◊
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
∞∞∞∞∞∞∞v∞
There was a problem hiding this 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!
lighthouse-core/lib/i18n/i18n.js
Outdated
values[id] = value / 1024; | ||
formattedValues[id] = value / 1024; | ||
} else { | ||
// All other numbers passed through unchanged. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// All other numbers passed through unchanged. | |
// All other numbers (e.g. ∞) passed through unchanged. |
There was a problem hiding this comment.
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
:)
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
∞∞∞∞∞∞∞v∞
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 bothInfinity
andNaN
get converted tonull
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.