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
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
formatNumber gran default
  • Loading branch information
connorjclark committed Apr 8, 2022
commit 8b0fa70837d0a3d5a33f2172f5a73e21165d4929
16 changes: 16 additions & 0 deletions docs/new-audits.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ An audit can return a number of different [detail types](https://github.com/Goog
| `'link'` | - | arbitrary link / url combination |
| `'text'\|'ms'\|'numeric'` | - | |

### Granularity

The following detail types accept a `granularity` field:

- `bytes`
- `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?


- 0.1
- 0.5
- 0.01
- 1

The value will be rounded to that nearest number.

<!--- https://docs.google.com/document/d/1KS6PGPYDfE_TWrRdw55Rd67P-g_MU4KdMetT3cTPHjI/edit#heading=h.32w9jjm4c70w -->
![Detail type examples](../assets/detail-type-examples.png)
Expand Down
11 changes: 8 additions & 3 deletions report/renderer/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,16 @@ export class I18n {
/**
* Format number.
* @param {number} number
* @param {number=} granularity Number of decimal places to include. Defaults to 0.1.
* @param {number=} granularity Number of decimal places to include.
* If undefined, the number will be displayed in full.
* @return {string}
*/
formatNumber(number, granularity = 0.1) {
return this._formatNumberWithGranularity(number, granularity);
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

if (granularity === undefined) {
return new Intl.NumberFormat(this._locale).format(number).replace(' ', NBSP2);
} else {
return this._formatNumberWithGranularity(number, granularity);
}
}

/**
Expand Down
8 changes: 2 additions & 6 deletions report/renderer/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,10 +430,8 @@ class Util {
break;
case 'devtools': {
const {cpuSlowdownMultiplier, requestLatencyMs} = throttling;
// TODO: better api in i18n formatter such that this isn't needed.
const cpuGranularity = Number.isInteger(cpuSlowdownMultiplier) ? 1 : 0.1;
// eslint-disable-next-line max-len
cpuThrottling = `${Util.i18n.formatNumber(cpuSlowdownMultiplier, cpuGranularity)}x slowdown (DevTools)`;
cpuThrottling = `${Util.i18n.formatNumber(cpuSlowdownMultiplier)}x slowdown (DevTools)`;
networkThrottling = `${Util.i18n.formatMilliseconds(requestLatencyMs, 1)} HTTP RTT, ` +
`${Util.i18n.formatKbps(throttling.downloadThroughputKbps)} down, ` +
`${Util.i18n.formatKbps(throttling.uploadThroughputKbps)} up (DevTools)`;
Expand All @@ -448,10 +446,8 @@ class Util {
}
case 'simulate': {
const {cpuSlowdownMultiplier, rttMs, throughputKbps} = throttling;
// TODO: better api in i18n formatter such that this isn't needed.
const cpuGranularity = Number.isInteger(cpuSlowdownMultiplier) ? 1 : 0.1;
// eslint-disable-next-line max-len
cpuThrottling = `${Util.i18n.formatNumber(cpuSlowdownMultiplier, cpuGranularity)}x slowdown (Simulated)`;
cpuThrottling = `${Util.i18n.formatNumber(cpuSlowdownMultiplier)}x slowdown (Simulated)`;
networkThrottling = `${Util.i18n.formatMilliseconds(rttMs)} TCP RTT, ` +
`${Util.i18n.formatKbps(throughputKbps)} throughput (Simulated)`;

Expand Down
18 changes: 13 additions & 5 deletions report/test/renderer/i18n-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ const NBSP = '\xa0';
describe('util helpers', () => {
it('formats a number', () => {
const i18n = new I18n('en', {...Util.UIStrings});
assert.strictEqual(i18n.formatNumber(10), '10.0');
assert.strictEqual(i18n.formatNumber(100.01), '100.0');
assert.strictEqual(i18n.formatNumber(13000.456), '13,000.5');
assert.strictEqual(i18n.formatNumber(10), '10');
assert.strictEqual(i18n.formatNumber(100.01), '100.01');
assert.strictEqual(i18n.formatNumber(13000.456), '13,000.456');
assert.strictEqual(i18n.formatNumber(13000.456444), '13,000.456');

assert.strictEqual(i18n.formatNumber(10, 0.1), '10.0');
assert.strictEqual(i18n.formatNumber(100.01, 0.1), '100.0');
assert.strictEqual(i18n.formatNumber(13000.456, 0.1), '13,000.5');

assert.strictEqual(i18n.formatInteger(10), '10');
assert.strictEqual(i18n.formatInteger(100.01), '100');
assert.strictEqual(i18n.formatInteger(13000.6), '13,001');
Expand Down Expand Up @@ -130,7 +136,8 @@ describe('util helpers', () => {
const number = 12346.858558;

const i18n = new I18n('de', {...Util.UIStrings});
assert.strictEqual(i18n.formatNumber(number), '12.346,9');
assert.strictEqual(i18n.formatNumber(number), '12.346,859');
assert.strictEqual(i18n.formatNumber(number, 0.1), '12.346,9');
assert.strictEqual(i18n.formatBytesToKiB(number), `12,1${NBSP}KiB`);
assert.strictEqual(i18n.formatMilliseconds(number), `12.350${NBSP}ms`);
assert.strictEqual(i18n.formatSeconds(number), `12,3${NBSP}Sek.`);
Expand All @@ -141,7 +148,8 @@ describe('util helpers', () => {
const number = 12346.858558;

const i18n = new I18n('en-XA', {...Util.UIStrings});
assert.strictEqual(i18n.formatNumber(number), '12.346,9');
assert.strictEqual(i18n.formatNumber(number), '12.346,859');
assert.strictEqual(i18n.formatNumber(number, 0.1), '12.346,9');
assert.strictEqual(i18n.formatBytesToKiB(number), `12,1${NBSP}KiB`);
assert.strictEqual(i18n.formatMilliseconds(number), `12.350${NBSP}ms`);
assert.strictEqual(i18n.formatSeconds(number), `12,3${NBSP}Sek.`);
Expand Down