1 |
|
2 |
|
3 |
|
4 |
|
5 | "use strict";
|
6 |
|
7 | const SizeFormatHelpers = exports;
|
8 |
|
9 | SizeFormatHelpers.formatSize = size => {
|
10 | if (typeof size !== "number" || Number.isNaN(size) === true) {
|
11 | return "unknown size";
|
12 | }
|
13 |
|
14 | if (size <= 0) {
|
15 | return "0 bytes";
|
16 | }
|
17 |
|
18 | const abbreviations = ["bytes", "KiB", "MiB", "GiB"];
|
19 | const index = Math.floor(Math.log(size) / Math.log(1024));
|
20 |
|
21 | return `${+(size / Math.pow(1024, index)).toPrecision(3)} ${
|
22 | abbreviations[index]
|
23 | }`;
|
24 | };
|
25 |
|
\ | No newline at end of file |