UNPKG

typescript-util

Version:

JS/TS 的简单工具

192 lines 5.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StorageUnit = void 0; // noinspection JSUnusedGlobalSymbols /** * StorageUnit * @author LL * @date 2022/1/25 14:54 */ class StorageUnit { /* 字节 */ static BYTE_VALUE = 1; /* 千字节 */ static KB_VALUE = StorageUnit.BYTE_VALUE << 10; /* 百万字节 */ static MB_VALUE = StorageUnit.KB_VALUE << 10; /* G 字节 */ static GB_VALUE = StorageUnit.MB_VALUE << 10; /* T 字节 */ static TB_VALUE = StorageUnit.GB_VALUE * 1024; /* P 字节 */ static PB_VALUE = StorageUnit.TB_VALUE * 1024; /** * 静态实例: 字节 * @type {StorageUnit} */ static BYTE = new StorageUnit(StorageUnit.BYTE_VALUE, 'Byte'); /** * 静态实例: 千字节 * @type {StorageUnit} */ static KB = new StorageUnit(StorageUnit.KB_VALUE, 'KB'); /** * 静态实例: 兆字节 * @type {StorageUnit} */ static MB = new StorageUnit(StorageUnit.MB_VALUE, 'MB'); /** * 静态实例: GB * @type {StorageUnit} */ static GB = new StorageUnit(StorageUnit.GB_VALUE, 'GB'); /** * 静态实例: TB * @type {StorageUnit} */ static TB = new StorageUnit(StorageUnit.TB_VALUE, 'TB'); /** * 静态实例: PB * @type {StorageUnit} */ static PB = new StorageUnit(StorageUnit.PB_VALUE, 'PB'); /** * 当前单位 一个单位的字节数 * @type {number} * @private */ value; /** * 当前单位 视图名称 * @type {string} * @private */ name; /* 构造, 一般使用静态常量即可 */ constructor(value, name) { this.value = value; this.name = name; } /** * 转换到字节 * 如果可能溢出, 按照JS正常规则溢出到负数 * @param {number} size * @returns {number} */ toByte(size = 1) { return size * (this.value); } /** * 转换到KB * 如果可能溢出, 按照JS正常规则溢出到负数 * 如果从更小的单位转换 会丢失精度 * @param {number} size */ toKByte(size) { return this.convert(size, StorageUnit.KB_VALUE, 10); } /** * 转换到MB * 如果可能溢出, 按照JS正常规则溢出到负数 * 如果从更小的单位转换 会丢失精度 * @param {number} size */ toMByte(size) { return this.convert(size, StorageUnit.MB_VALUE, 20); } /** * 转换到GB * 如果可能溢出, 按照JS正常规则溢出到负数 * 如果从更小的单位转换 会丢失精度 * @param {number} size */ toGByte(size) { return this.convert(size, StorageUnit.GB_VALUE, 30); } /** * 转换到TB * 如果可能溢出, 按照JS正常规则溢出到负数 * 如果从更小的单位转换 会丢失精度 * @param {number} size */ toTBType(size) { return this.convert(size, StorageUnit.TB_VALUE, 40); } /** * @param size 数量 * @param current 当前单位字节 * @param {number} number 右移位 */ convert(size, current, number) { if (this.value > current) { return size * (this.value >> number); } return (size * this.value) >> number; } /* 实用工具 */ /** * checkSize 小于 指定数量 * @param {number} checkSize 待检查字节数 * @param {number} unitSize 当前单位数量 默认 1 * @returns {boolean} 小于 */ lt(checkSize, unitSize = 1) { return checkSize < this.toByte(unitSize); } lte(checkSize, unitSize = 1) { return checkSize <= this.toByte(unitSize); } /** * checkSize 大于指定数量 * @param {number} checkSize 待检查字节数 * @param {number} unitSize 当前单位数量 默认 1 * @returns {boolean} 大于 */ gt(checkSize, unitSize = 1) { return checkSize > this.toByte(unitSize); } gte(checkSize, unitSize = 1) { return checkSize >= this.toByte(unitSize); } /** * 相等 */ eq(checkSize, unitSize = 1) { return checkSize == this.toByte(unitSize); } /** * 友好的显示, 类似 {@code ls -h} * @param {number} size 字节数 * @param {number} decimal 小数位数, 默认两位 * @return {string} 带有可读单位的描述 例: 123 KB */ static displayName(size, decimal = 2) { let unit = StorageUnit.determineUnit(size); return `${(size / unit.value).toFixed(decimal)} ${unit.name}`; } /** * 确定单位 * @param {number} size 字节数 * @return {StorageUnit} 合适的单位, 最大PB 永不为空 */ static determineUnit(size) { if (size < StorageUnit.KB_VALUE) { return StorageUnit.BYTE; } if (size < StorageUnit.MB_VALUE) { return StorageUnit.KB; } if (size < StorageUnit.GB_VALUE) { return StorageUnit.MB; } if (size < StorageUnit.TB_VALUE) { return StorageUnit.GB; } if (size < StorageUnit.PB_VALUE) { return StorageUnit.TB; } return StorageUnit.PB; } } exports.StorageUnit = StorageUnit; //# sourceMappingURL=StorageUnit.js.map