UNPKG

nhb-toolbox

Version:

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

57 lines (56 loc) 1.48 kB
var _a; import { $BaseConverter } from './base.js'; import { UNITS } from './constants.js'; export class $Data extends $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 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); } } _a = $Data;