-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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: remove default granularity values from formatter #13839
Conversation
Co-authored-by: Adam Raine <[email protected]>
report/renderer/i18n.js
Outdated
if (Object.is(number, -0)) number = 0; | ||
// Avoid displaying a negative value that rounds to zero as "0". | ||
if (Object.is(number, -0)) number = 0; | ||
} else if (Math.abs(number) <= 0.0001) { |
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.
Can you add a comment explaining where 0.0001 comes from.
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.
I think it also needs to be Math.abs(number) < 0.0005
(less than half the smallest expressible value).
e.g. with this _formatNumberWithGranularity(-0.0004)
currently gives '-0'
docs/new-audits.md
Outdated
- `ms` | ||
- `numeric` | ||
|
||
`granularity` must either be a positive integer or power of 10. Some examples of valid values for `granularity`: |
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.
When you say "power of 10" you mean "integer power of 10" right? If so I don't think 0.5 qualifies.
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.
updated but... maybe we could just reduce the restriction on granularity to any positive number?
report/renderer/i18n.js
Outdated
if (Object.is(number, -0)) number = 0; | ||
// Avoid displaying a negative value that rounds to zero as "0". | ||
if (Object.is(number, -0)) number = 0; | ||
} else if (Math.abs(number) <= 0.0001) { |
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.
I think it also needs to be Math.abs(number) < 0.0005
(less than half the smallest expressible value).
e.g. with this _formatNumberWithGranularity(-0.0004)
currently gives '-0'
if (!Number.isFinite(log10) || (granularity > 1 && Math.floor(log10) !== log10)) { | ||
throw new Error(`granularity of ${granularity} is invalid`); | ||
} | ||
if (granularity !== undefined) { |
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.
it does feel like we're working too hard to maintain granularity
which was kind of a haphazard choice long ago, but it's unfortunately in the LHR, so...:(
report/renderer/i18n.js
Outdated
@@ -32,36 +32,42 @@ export class I18n { | |||
|
|||
/** | |||
* @param {number} number | |||
* @param {number} granularity | |||
* @param {number|undefined} granularity | |||
* @param {Intl.NumberFormatOptions} opts |
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.
nit: seems like we can make granularity
and opts
optional. opts
even has a default value.
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.
Bump on this if it's not too much work.
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.
just now: I added =
to the opts type to better reflect how this method is used.
granularity should remain undefined
because all the use cases here are passing a value explicitly, even if it may be undefined.
all this stuff is pretty subjective i think. if you feel strongly I'll make the change.
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.
Yeah that's fine with me
report/renderer/i18n.js
Outdated
* @return {string} | ||
*/ | ||
formatNumber(number, granularity = 0.1) { | ||
formatNumber(number, granularity = undefined) { |
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.
nit: optional params are already undefined by default
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.
Even so, I think having a second way to represent "this is optional" is nice.
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.
I'm definitely for not including = undefined
for optional params
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.
Yeah I don't think we do this anywhere else. I still prefer without = undefined
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.
I have a couple outstanding nits but otherwise LGTM
report/renderer/i18n.js
Outdated
@@ -32,36 +32,42 @@ export class I18n { | |||
|
|||
/** | |||
* @param {number} number | |||
* @param {number} granularity | |||
* @param {number|undefined} granularity | |||
* @param {Intl.NumberFormatOptions} opts |
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.
Bump on this if it's not too much work.
We had various default values for granularity in the i18n formatter module. For formatNumber particularly, this made that method less usable outside details rendering, where these default granularities values were intended, and the assumption that "round to the nearest tenth" didn't hold for the one use case outside details rendering.
this pr changes the default behavior of these formatting method to do no granularity/rounding (asterisk, except what is defined in the
Intl
api), and moves the default granularity values to the details renderer.Passing no granularity is equivalent to using
0
forminimumFractionDigits
and3
formaximumFractionDigits
, which is behavior that cannot be replicated with ourgranularity
abstraction. And makes for a more usable default behavior.There should be zero changes to report rendering, besides
getEmulationDescriptions
.