UNPKG

1.47 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.formatMS = void 0;
4const ONE_SECOND_AS_MS = 1000;
5const ONE_MINUTE_AS_MS = 60 * ONE_SECOND_AS_MS;
6const ONE_HOUR_AS_MS = 60 * ONE_MINUTE_AS_MS;
7function formatMS(ms, d, allowMicros = false, allowNanos = true) {
8 if (ms === 0 || ms === null)
9 return "0";
10 const bounds = [
11 ONE_HOUR_AS_MS,
12 ONE_MINUTE_AS_MS,
13 ONE_SECOND_AS_MS,
14 1,
15 0.001,
16 0.000001,
17 ];
18 const units = ["hr", "min", "s", "ms", "μs", "ns"];
19 const makeSmallNumbersNice = (f) => {
20 if (f >= 100)
21 return f.toFixed(0);
22 if (f >= 10)
23 return f.toFixed(1);
24 if (f === 0)
25 return "0";
26 return f.toFixed(2);
27 };
28 const bound = bounds.find((b) => b <= ms) || bounds[bounds.length - 1];
29 const boundIndex = bounds.indexOf(bound);
30 const unit = boundIndex >= 0 ? units[boundIndex] : "";
31 if ((unit === "μs" || unit === "ns") && !allowMicros) {
32 return "< 1ms";
33 }
34 if (unit === "ns" && !allowNanos) {
35 return "< 1µs";
36 }
37 const value = typeof d !== "undefined"
38 ? (ms / bound).toFixed(d)
39 : makeSmallNumbersNice(ms / bound);
40 if ((value === "1000" || value === "1000.0") && boundIndex >= 1) {
41 return `1${units[boundIndex - 1]}`;
42 }
43 return `${value}${unit}`;
44}
45exports.formatMS = formatMS;
46//# sourceMappingURL=format.js.map
\No newline at end of file