const map: any = { b: 1, kb: 1 << 10, mb: 1 << 20, gb: 1 << 30, tb: Math.pow(1024, 4), pb: Math.pow(1024, 5), } /** * 通过文件尺寸字符串解析字节数 "1KB" => 1024 * @param sizeString */ export function toByte(sizeString: string | number): number { if (typeof sizeString === "number") return sizeString const parseRegExp = /^((-|\+)?(\d+(?:\.\d+)?)) *(kb|mb|gb|tb|pb)$/i // Test if the string passed is valid var results = parseRegExp.exec(sizeString) var floatValue var unit = "b" if (!results) { // Nothing could be extracted from the given string floatValue = parseInt(sizeString, 10) unit = "b" } else { // Retrieve the value and the unit floatValue = parseFloat(results[1]) unit = results[4].toLowerCase() } return Math.floor(map[unit] * floatValue) }