UNPKG

nhb-toolbox

Version:

A versatile collection of smart, efficient, and reusable utility functions, classes and types for everyday development needs.

61 lines (60 loc) 1.61 kB
"use strict"; var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.$Data = void 0; const base_1 = require("./base"); const constants_1 = require("./constants"); class $Data extends base_1.$BaseConverter { static #factors = { bit: 1 / 8, byte: 1, kilobit: 128, kilobyte: 1024, megabit: 131072, megabyte: 1048576, gigabit: 134217728, gigabyte: 1073741824, terabit: 137438953472, terabyte: 1099511627776, petabit: 140737488355328, petabyte: 1125899906842624, }; constructor(value, unit) { super(value, unit); } #toBytes() { return this.value * _a.#factors[this.unit]; } to(target) { const inBytes = this.#toBytes(); return inBytes / _a.#factors[target]; } toAll() { const inBytes = this.#toBytes(); const result = {}; for (const unit of constants_1.UNITS.data) { result[unit] = inBytes / _a.#factors[unit]; } return result; } formatTo(target, options) { const value = this.to(target); const shortLabels = { bit: 'b', byte: 'B', kilobit: 'Kb', kilobyte: 'KB', megabit: 'Mb', megabyte: 'MB', gigabit: 'Gb', gigabyte: 'GB', terabit: 'Tb', terabyte: 'TB', petabit: 'Pb', petabyte: 'PB', }; return this.$formatTo(value, target, shortLabels, options); } } exports.$Data = $Data; _a = $Data;