MCPcopy Index your code
hub / github.com/PleasureTools/joyBox / SizeStrToByte

Function SizeStrToByte

Shared/Util.ts:7–26  ·  view source on GitHub ↗
(size: string, defaultMetric: SizeToByteMetric = SizeToByteMetric.BYTE)

Source from the content-addressed store, hash-verified

5 * @returns Rounded number of bytes or NaN
6 */
7export function SizeStrToByte(size: string, defaultMetric: SizeToByteMetric = SizeToByteMetric.BYTE): number {
8 if (typeof size !== 'string') {
9 return NaN;
10 }
11
12 const MetricMult = (m: string): number => {
13 return Math.pow(1000, [
14 SizeToByteMetric.BYTE,
15 SizeToByteMetric.KB,
16 SizeToByteMetric.MB,
17 SizeToByteMetric.GB,
18 SizeToByteMetric.TB].findIndex(x => x === m));
19 };
20
21 return Math.ceil(size.endsWith('bit') ?
22 Number.parseFloat(size) * MetricMult(size[size.length - 4]) / 8 :
23 size.endsWith('B') ?
24 Number.parseFloat(size) * MetricMult(size[size.length - 2]) :
25 Number.parseFloat(size) * MetricMult(defaultMetric));
26}

Callers 3

StartRecordingMethod · 0.90
Util.tsFile · 0.90
SizeTestMethod · 0.90

Calls 1

MetricMultFunction · 0.85

Tested by

no test coverage detected