OLD | NEW |
(Empty) | |
| 1 /** |
| 2 * Copyright 2017 Google Inc. All rights reserved. |
| 3 * |
| 4 * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 * you may not use this file except in compliance with the License. |
| 6 * You may obtain a copy of the License at |
| 7 * |
| 8 * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 * |
| 10 * Unless required by applicable law or agreed to in writing, software |
| 11 * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 * See the License for the specific language governing permissions and |
| 14 * limitations under the License. |
| 15 */ |
| 16 'use strict'; |
| 17 |
| 18 /* globals self */ |
| 19 |
| 20 const RATINGS = { |
| 21 PASS: {label: 'pass', minScore: 75}, |
| 22 AVERAGE: {label: 'average', minScore: 45}, |
| 23 FAIL: {label: 'fail'} |
| 24 }; |
| 25 |
| 26 class Util { |
| 27 /** |
| 28 * Convert a score to a rating label. |
| 29 * @param {number} score |
| 30 * @return {string} |
| 31 */ |
| 32 static calculateRating(score) { |
| 33 let rating = RATINGS.FAIL.label; |
| 34 if (score >= RATINGS.PASS.minScore) { |
| 35 rating = RATINGS.PASS.label; |
| 36 } else if (score >= RATINGS.AVERAGE.minScore) { |
| 37 rating = RATINGS.AVERAGE.label; |
| 38 } |
| 39 return rating; |
| 40 } |
| 41 |
| 42 /** |
| 43 * Format number. |
| 44 * @param {number} number |
| 45 * @return {string} |
| 46 */ |
| 47 static formatNumber(number) { |
| 48 return number.toLocaleString(undefined, {maximumFractionDigits: 1}); |
| 49 } |
| 50 |
| 51 /** |
| 52 * Format time. |
| 53 * @param {string} date |
| 54 * @return {string} |
| 55 */ |
| 56 static formatDateTime(date) { |
| 57 const options = { |
| 58 month: 'short', day: 'numeric', year: 'numeric', |
| 59 hour: 'numeric', minute: 'numeric', timeZoneName: 'short' |
| 60 }; |
| 61 let formatter = new Intl.DateTimeFormat('en-US', options); |
| 62 |
| 63 // Force UTC if runtime timezone could not be detected. |
| 64 // See https://github.com/GoogleChrome/lighthouse/issues/1056 |
| 65 const tz = formatter.resolvedOptions().timeZone; |
| 66 if (!tz || tz.toLowerCase() === 'etc/unknown') { |
| 67 options.timeZone = 'UTC'; |
| 68 formatter = new Intl.DateTimeFormat('en-US', options); |
| 69 } |
| 70 return formatter.format(new Date(date)); |
| 71 } |
| 72 } |
| 73 |
| 74 if (typeof module !== 'undefined' && module.exports) { |
| 75 module.exports = Util; |
| 76 } else { |
| 77 self.Util = Util; |
| 78 } |
OLD | NEW |