UNPKG

byte-parser

Version:

Parse byte string to byte number, e.g. 1.2 Kb -> 1228.8, Kb, Mb, Gb, Tb, Pb, Eb, Zb, Yb supported.

28 lines (27 loc) 678 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var MAP = { k: 1, m: 2, g: 3, t: 4, p: 5, e: 6, z: 7, y: 8, }; /** * parse byte string into a number * @param byteString */ function default_1(byteString, ratio) { if (ratio === void 0) { ratio = 1024; } var match = byteString.match(/^(\d*\.*\d*)\s*([kKmMgGtTpPeEzZyY]{0,1}[bB]{0,1})$/); if (!match) { throw "invalid byte string '" + byteString + "'!"; } var size = match[1]; var unit = match[2].toLowerCase().replace(/(b$)/, ''); // 删除最后的 b return size * Math.pow(ratio, MAP[unit] || 0); } exports.default = default_1;