hl-digits
Version:
## NPM [https://www.npmjs.com/package/hl-digits](https://www.npmjs.com/package/hl-digits)
56 lines • 2.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var constants_1 = require("./constants");
var utils_1 = require("./utils");
var ByteUnit = {
kb: function (n) { return n * constants_1.KILOBYTE_UNIT_SIZE; },
kilobytes: function (n) { return n * constants_1.KILOBYTE_UNIT_SIZE; },
mb: function (n) { return n * ByteUnit.kb(constants_1.KILOBYTE_UNIT_SIZE); },
megabytes: function (n) { return n * ByteUnit.kb(constants_1.KILOBYTE_UNIT_SIZE); },
gb: function (n) { return n * ByteUnit.mb(constants_1.KILOBYTE_UNIT_SIZE); },
gigabytes: function (n) { return n * ByteUnit.mb(constants_1.KILOBYTE_UNIT_SIZE); },
tb: function (n) { return n * ByteUnit.gb(constants_1.KILOBYTE_UNIT_SIZE); },
terabytes: function (n) { return n * ByteUnit.gb(constants_1.KILOBYTE_UNIT_SIZE); },
pb: function (n) { return n * ByteUnit.tb(constants_1.KILOBYTE_UNIT_SIZE); },
petabytes: function (n) { return n * ByteUnit.tb(constants_1.KILOBYTE_UNIT_SIZE); },
b: function (n) { return n; },
bytes: function (n) { return n; },
parse: function (str) { return (0, utils_1.parse)(ByteUnit, str); },
toString: function (value, as, representation) {
if (as === void 0) { as = 'auto'; }
if (representation === void 0) { representation = 'binary'; }
var bytes = ByteUnit.parse(value);
if (bytes === 0) {
return "0.0b";
}
var MULTILPIER = representation === 'decimal' ? 1000 : constants_1.KILOBYTE_UNIT_SIZE;
var suffixes = {
0: 'b',
1: 'kb',
2: 'mb',
3: 'gb',
4: 'tb',
5: 'pb'
};
switch (as) {
case 'b':
return "".concat((bytes).toFixed(1)).concat(suffixes[0]);
case 'kb':
return "".concat((bytes / MULTILPIER).toFixed(1)).concat(suffixes[1]);
case 'mb':
return "".concat((bytes / (Math.pow(MULTILPIER, 2))).toFixed(1)).concat(suffixes[2]);
case 'gb':
return "".concat((bytes / (Math.pow(MULTILPIER, 3))).toFixed(1)).concat(suffixes[3]);
case 'tb':
return "".concat((bytes / (Math.pow(MULTILPIER, 4))).toFixed(1)).concat(suffixes[4]);
case 'pb':
return "".concat((bytes / (Math.pow(MULTILPIER, 5))).toFixed(1)).concat(suffixes[5]);
case 'auto':
default:
var suffix = bytes.toString().split('').reverse().join('').match(/.{1,3}/g).reverse().slice(1).length;
return "".concat((bytes / (Math.pow(MULTILPIER, suffix))).toFixed(1)).concat(suffixes[suffix]);
}
}
};
exports.default = ByteUnit;
//# sourceMappingURL=ByteUnit.js.map