UNPKG

node-web-mvc

Version:
36 lines (35 loc) 978 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * @module BytesConverter * @description 字节单位转换 */ const unitValues = { b: 1, kb: 1024, mb: Math.pow(1024, 2), gb: Math.pow(1024, 3), tb: Math.pow(1024, 4), pb: Math.pow(1024, 5), }; class Bytes { constructor(input, defaultValue) { input = (input === '' || input === null || input === undefined) ? defaultValue : input; if (typeof input == 'number') { this.bytes = input; } else if (typeof input === 'string') { this.bytes = this.parse(input); } } parse(input) { const values = /^((-|\+)?(\d+(?:\.\d+)?)) *(kb|mb|gb|tb|pb)$/i.exec(input) || []; const unit = (values[4] || '').toLowerCase(); const value = parseFloat(values[1]); if (value > 0) { return unitValues[unit] * value; } return 0; } } exports.default = Bytes;