-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
report: remove Util.UIStrings mutation, add I18n renderer class (#10153)
- Loading branch information
1 parent
5dcb666
commit 81c6e92
Showing
32 changed files
with
327 additions
and
275 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/** | ||
* @license Copyright 2020 Google Inc. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
'use strict'; | ||
|
||
/* globals self, URL */ | ||
|
||
// Not named `NBSP` because that creates a duplicate identifier (util.js). | ||
const NBSP2 = '\xa0'; | ||
|
||
class I18n { | ||
/** | ||
* @param {LH.Locale} locale | ||
* @param {LH.I18NRendererStrings=} strings | ||
*/ | ||
constructor(locale, strings) { | ||
// When testing, use a locale with more exciting numeric formatting. | ||
if (locale === 'en-XA') locale = 'de'; | ||
|
||
this._numberDateLocale = locale; | ||
this._numberFormatter = new Intl.NumberFormat(locale); | ||
this._strings = /** @type {LH.I18NRendererStrings} */ (strings || {}); | ||
} | ||
|
||
get strings() { | ||
return this._strings; | ||
} | ||
|
||
/** | ||
* Format number. | ||
* @param {number} number | ||
* @param {number=} granularity Number of decimal places to include. Defaults to 0.1. | ||
* @return {string} | ||
*/ | ||
formatNumber(number, granularity = 0.1) { | ||
const coarseValue = Math.round(number / granularity) * granularity; | ||
return this._numberFormatter.format(coarseValue); | ||
} | ||
|
||
/** | ||
* @param {number} size | ||
* @param {number=} granularity Controls how coarse the displayed value is, defaults to 0.1 | ||
* @return {string} | ||
*/ | ||
formatBytesToKB(size, granularity = 0.1) { | ||
const kbs = this._numberFormatter.format(Math.round(size / 1024 / granularity) * granularity); | ||
return `${kbs}${NBSP2}KB`; | ||
} | ||
|
||
/** | ||
* @param {number} ms | ||
* @param {number=} granularity Controls how coarse the displayed value is, defaults to 10 | ||
* @return {string} | ||
*/ | ||
formatMilliseconds(ms, granularity = 10) { | ||
const coarseTime = Math.round(ms / granularity) * granularity; | ||
return `${this._numberFormatter.format(coarseTime)}${NBSP2}ms`; | ||
} | ||
|
||
/** | ||
* @param {number} ms | ||
* @param {number=} granularity Controls how coarse the displayed value is, defaults to 0.1 | ||
* @return {string} | ||
*/ | ||
formatSeconds(ms, granularity = 0.1) { | ||
const coarseTime = Math.round(ms / 1000 / granularity) * granularity; | ||
return `${this._numberFormatter.format(coarseTime)}${NBSP2}s`; | ||
} | ||
|
||
/** | ||
* Format time. | ||
* @param {string} date | ||
* @return {string} | ||
*/ | ||
formatDateTime(date) { | ||
/** @type {Intl.DateTimeFormatOptions} */ | ||
const options = { | ||
month: 'short', day: 'numeric', year: 'numeric', | ||
hour: 'numeric', minute: 'numeric', timeZoneName: 'short', | ||
}; | ||
let formatter = new Intl.DateTimeFormat(this._numberDateLocale, options); | ||
|
||
// Force UTC if runtime timezone could not be detected. | ||
// See https://github.com/GoogleChrome/lighthouse/issues/1056 | ||
const tz = formatter.resolvedOptions().timeZone; | ||
if (!tz || tz.toLowerCase() === 'etc/unknown') { | ||
options.timeZone = 'UTC'; | ||
formatter = new Intl.DateTimeFormat(this._numberDateLocale, options); | ||
} | ||
return formatter.format(new Date(date)); | ||
} | ||
/** | ||
* Converts a time in milliseconds into a duration string, i.e. `1d 2h 13m 52s` | ||
* @param {number} timeInMilliseconds | ||
* @return {string} | ||
*/ | ||
formatDuration(timeInMilliseconds) { | ||
let timeInSeconds = timeInMilliseconds / 1000; | ||
if (Math.round(timeInSeconds) === 0) { | ||
return 'None'; | ||
} | ||
|
||
/** @type {Array<string>} */ | ||
const parts = []; | ||
const unitLabels = /** @type {Object<string, number>} */ ({ | ||
d: 60 * 60 * 24, | ||
h: 60 * 60, | ||
m: 60, | ||
s: 1, | ||
}); | ||
|
||
Object.keys(unitLabels).forEach(label => { | ||
const unit = unitLabels[label]; | ||
const numberOfUnits = Math.floor(timeInSeconds / unit); | ||
if (numberOfUnits > 0) { | ||
timeInSeconds -= numberOfUnits * unit; | ||
parts.push(`${numberOfUnits}\xa0${label}`); | ||
} | ||
}); | ||
|
||
return parts.join(' '); | ||
} | ||
} | ||
|
||
if (typeof module !== 'undefined' && module.exports) { | ||
module.exports = I18n; | ||
} else { | ||
self.I18n = I18n; | ||
} |
Oops, something went wrong.