-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathutil.js
171 lines (150 loc) · 5.37 KB
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* @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
* @param {number=} decimalPlaces Number of decimal places to include. Defaults to 1.
* @return {string}
*/
static formatNumber(number, decimalPlaces = 1) {
return number.toLocaleString(undefined, {maximumFractionDigits: decimalPlaces});
}
/**
* @param {number} size
* @param {number=} decimalPlaces Number of decimal places to include. Defaults to 2.
* @return {string}
*/
static formatBytesToKB(size, decimalPlaces = 2) {
const kbs = (size / 1024).toLocaleString(undefined, {maximumFractionDigits: decimalPlaces});
return `${kbs}${NBSP}KB`;
}
/**
* @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;
}