UNPKG

538 BJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Sean Larkin @thelarkinn
4*/
5"use strict";
6
7const SizeFormatHelpers = exports;
8
9SizeFormatHelpers.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