text: /**
   * @license Copyright 2017 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 */
  
  const ELLIPSIS = '\u2026';
  const NBSP = '\xa0';
  
  const RATINGS = {
    PASS: {label: 'pass', minScore: 75},
    AVERAGE: {label: 'average', minScore: 45},
    FAIL: {label: 'fail'}
  };
  
  class Util {
    /**
     * Convert a score to a rating label.
     * @param {number} score
     * @return {string}
     */
    static calculateRating(score) {
      let rating = RATINGS.FAIL.label;
      if (score >= RATINGS.PASS.minScore) {
        rating = RATINGS.PASS.label;
      } else if (score >= RATINGS.AVERAGE.minScore) {
        rating = RATINGS.AVERAGE.label;
      }
      return rating;
    }
  
    /**
     * Format number.
     * @param {number} number
     * @return {string}
     */
    static formatNumber(number) {
      return number.toLocaleString(undefined, {maximumFractionDigits: 1});
    }
  
    /**
     * @param {number} size
     * @param {number=} decimalPlaces Number of decimal places to include. Defaults to 2.
     * @return {string}
     */
    static formateBytesToKB(size, decimalPlaces = 2) {
      return (size / 1024).toLocaleString(undefined, {maximumFractionDigits: decimalPlaces});
    }
  
    /**
     * @param {number} ms
     * @param {number=} granularity Controls how coarse the displayed value is, defaults to 10
     * @return {string}
     */
    static formatMilliseconds(ms, granularity = 10) {
      const coarseTime = Math.round(ms / granularity) * granularity;
      return `${coarseTime.toLocaleString()}${NBSP}ms`;
    }
  
    /**
     * Format time.
     * @param {string} date
     * @return {string}
     */
    static formatDateTime(date) {
      const options = {
        month: 'short', day: 'numeric', year: 'numeric',
        hour: 'numeric', minute: 'numeric', timeZoneName: 'short'
      };
      let formatter = new Intl.DateTimeFormat('en-US', 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('en-US', options);
      }
      return formatter.format(new Date(date));
    }
  
    /**
     * @param {!URL} parsedUrl
     * @param {{numPathParts: (number|undefined), preserveQuery: (boolean|undefined), preserveHost: (boolean|undefined)}=} options
     * @return {string}
     */
    static getURLDisplayName(parsedUrl, options = {}) {
      const numPathParts = options.numPathParts !== undefined ? options.numPathParts : 2;
      const preserveQuery = options.preserveQuery !== undefined ? options.preserveQuery : true;
      const preserveHost = options.preserveHost || false;
  
      let name;
  
      if (parsedUrl.protocol === 'about:' || parsedUrl.protocol === 'data:') {
        // Handle 'about:*' and 'data:*' URLs specially since they have no path.
        name = parsedUrl.href;
      } else {
        name = parsedUrl.pathname;
        const parts = name.split('/').filter(part => part.length);
        if (numPathParts && parts.length > numPathParts) {
          name = ELLIPSIS + parts.slice(-1 * numPathParts).join('/');
        }
  
        if (preserveHost) {
          name = `${parsedUrl.host}/${name.replace(/^\//, '')}`;
        }
        if (preserveQuery) {
          name = `${name}${parsedUrl.search}`;
        }
      }
  
      const MAX_LENGTH = 64;
      // Always elide hash
      name = name.replace(/([a-f0-9]{7})[a-f0-9]{13}[a-f0-9]*/g, `$1${ELLIPSIS}`);
  
      // Elide query params first
      if (name.length > MAX_LENGTH && name.includes('?')) {
        // Try to leave the first query parameter intact
        name = name.replace(/\?([^=]*)(=)?.*/, `?$1$2${ELLIPSIS}`);
  
        // Remove it all if it's still too long
        if (name.length > MAX_LENGTH) {
          name = name.replace(/\?.*/, `?${ELLIPSIS}`);
        }
      }
  
      // Elide too long names next
      if (name.length > MAX_LENGTH) {
        const dotIndex = name.lastIndexOf('.');
        if (dotIndex >= 0) {
          name = name.slice(0, MAX_LENGTH - 1 - (name.length - dotIndex)) +
              // Show file extension
              `${ELLIPSIS}${name.slice(dotIndex)}`;
        } else {
          name = name.slice(0, MAX_LENGTH - 1) + ELLIPSIS;
        }
      }
  
      return name;
    }
  
    /**
     * Split a URL into a file and hostname for easy display.
     * @param {string} url
     * @return {{file: string, hostname: string}}
     */
    static parseURL(url) {
      const parsedUrl = new URL(url);
      return {file: Util.getURLDisplayName(parsedUrl), hostname: parsedUrl.hostname};
    }
  
    /**
     * @param {number} startTime
     * @param {number} endTime
     * @return {string}
     */
    static chainDuration(startTime, endTime) {
      return Util.formatNumber((endTime - startTime) * 1000);
    }
  }
  
  if (typeof module !== 'undefined' && module.exports) {
    module.exports = Util;
  } else {
    self.Util = Util;
  }
  