Last change
on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
583 bytes
|
Rev | Line | |
---|
[6a3a178] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Sean Larkin @thelarkinn
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | /**
|
---|
| 9 | * @param {number} size the size in bytes
|
---|
| 10 | * @returns {string} the formatted size
|
---|
| 11 | */
|
---|
| 12 | exports.formatSize = size => {
|
---|
| 13 | if (typeof size !== "number" || Number.isNaN(size) === true) {
|
---|
| 14 | return "unknown size";
|
---|
| 15 | }
|
---|
| 16 |
|
---|
| 17 | if (size <= 0) {
|
---|
| 18 | return "0 bytes";
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | const abbreviations = ["bytes", "KiB", "MiB", "GiB"];
|
---|
| 22 | const index = Math.floor(Math.log(size) / Math.log(1024));
|
---|
| 23 |
|
---|
| 24 | return `${+(size / Math.pow(1024, index)).toPrecision(3)} ${
|
---|
| 25 | abbreviations[index]
|
---|
| 26 | }`;
|
---|
| 27 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.