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: remove default granularity values from formatter #13839

Merged
merged 24 commits into from
Apr 27, 2022

Conversation

connorjclark
Copy link
Collaborator

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 for minimumFractionDigits and 3 for maximumFractionDigits, which is behavior that cannot be replicated with our granularity abstraction. And makes for a more usable default behavior.

There should be zero changes to report rendering, besides getEmulationDescriptions.

@connorjclark connorjclark requested a review from a team as a code owner April 8, 2022 20:46
@connorjclark connorjclark requested review from adamraine and removed request for a team April 8, 2022 20:46
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) {
Copy link
Member

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.

Copy link
Member

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'

report/test/renderer/i18n-test.js Show resolved Hide resolved
- `ms`
- `numeric`

`granularity` must either be a positive integer or power of 10. Some examples of valid values for `granularity`:
Copy link
Member

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.

Copy link
Collaborator Author

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/details-renderer.js Show resolved Hide resolved
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) {
Copy link
Member

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) {
Copy link
Member

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...:(

@@ -32,36 +32,42 @@ export class I18n {

/**
* @param {number} number
* @param {number} granularity
* @param {number|undefined} granularity
* @param {Intl.NumberFormatOptions} opts
Copy link
Member

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.

Copy link
Member

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.

Copy link
Collaborator Author

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.

Copy link
Member

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

* @return {string}
*/
formatNumber(number, granularity = 0.1) {
formatNumber(number, granularity = undefined) {
Copy link
Member

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

Copy link
Collaborator Author

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.

Copy link
Member

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

Copy link
Member

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

report/renderer/i18n.js Outdated Show resolved Hide resolved
Base automatically changed from i18n-formatter-units to master April 19, 2022 20:58
Copy link
Member

@adamraine adamraine left a 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

@@ -32,36 +32,42 @@ export class I18n {

/**
* @param {number} number
* @param {number} granularity
* @param {number|undefined} granularity
* @param {Intl.NumberFormatOptions} opts
Copy link
Member

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.

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

Successfully merging this pull request may close these issues.

None yet

4 participants