@naturalcycles/js-lib
Version:
Standard library for universal (browser + Node.js) javascript
51 lines (50 loc) • 1.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports._gb = _gb;
exports._mb = _mb;
exports._kb = _kb;
exports._hb = _hb;
exports._hc = _hc;
function _gb(b) {
return Math.round(b / 1024 ** 3);
}
function _mb(b) {
return Math.round(b / 1024 ** 2);
}
function _kb(b) {
return Math.round(b / 1024);
}
/**
* Byte size to Human byte size string
*/
function _hb(b = 0) {
if (b < 1024)
return `${Math.round(b)} byte(s)`;
if (b < 1024 ** 2)
return `${(b / 1024).toPrecision(3)} Kb`;
if (b < 1024 ** 3)
return `${(b / 1024 ** 2).toPrecision(3)} Mb`;
if (b < 1024 ** 4)
return `${(b / 1024 ** 3).toPrecision(3)} Gb`;
if (b < 1024 ** 5)
return `${(b / 1024 ** 4).toPrecision(3)} Tb`;
return `${Math.round(b / 1024 ** 4)} Tb`;
}
/**
* hc stands for "human count", similar to "human bytes" `_hb` function.
* Helpful to print big numbers, as it adds `K` (kilo), `M` (mega), etc to make
* them more readable.
*/
function _hc(c = 0) {
if (c < 10 ** 4)
return String(c);
if (c < 10 ** 6)
return (c / 10 ** 3).toPrecision(3) + ' K';
if (c < 10 ** 9)
return (c / 10 ** 6).toPrecision(3) + ' M'; // million
if (c < 10 ** 12)
return (c / 10 ** 9).toPrecision(3) + ' B'; // billion
if (c < 10 ** 15)
return (c / 10 ** 12).toPrecision(3) + ' T'; // trillion
return Math.round(c / 10 ** 12) + ' T';
}