MCPcopy Create free account
hub / github.com/zxlie/FeHelper / toBytes

Function toBytes

apps/byte-unit/byte-utils.js:3–13  ·  view source on GitHub ↗
(value, unit)

Source from the content-addressed store, hash-verified

1export const BYTE_UNITS = ['B', 'KB', 'MB', 'GB', 'TB'];
2
3export function toBytes(value, unit) {
4 const num = Number(value);
5 if (!Number.isFinite(num)) {
6 throw new Error('请输入合法的数字');
7 }
8 const index = BYTE_UNITS.indexOf(unit);
9 if (index === -1) {
10 throw new Error('不支持的单位');
11 }
12 return num * Math.pow(1024, index);
13}
14
15export function convertBytes(value, fromUnit, toUnit) {
16 const bytes = toBytes(value, fromUnit);

Callers 1

convertBytesFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected