UNPKG

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