MCPcopy Create free account
hub / github.com/bewcloud/bewcloud / humanFileSize

Function humanFileSize

public/ts/utils/files.ts:5–25  ·  view source on GitHub ↗
(bytes: number)

Source from the content-addressed store, hash-verified

3export const TRASH_PATH = `/.Trash/`;
4
5export function humanFileSize(bytes: number) {
6 if (Math.abs(bytes) < 1024) {
7 return `${bytes} B`;
8 }
9
10 const units = ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
11
12 let unitIndex = -1;
13 const roundedPower = 10 ** 2;
14
15 do {
16 bytes /= 1024;
17 ++unitIndex;
18 } while (Math.round(Math.abs(bytes) * roundedPower) / roundedPower >= 1024 && unitIndex < units.length - 1);
19
20 if (bytes.toFixed(2).split('.')[1] === '00') {
21 return `${bytes.toFixed(0)} ${units[unitIndex]}`;
22 }
23
24 return `${bytes.toFixed(2)} ${units[unitIndex]}`;
25}
26
27export function bytesFromHumanFileSize(humanFileSize: string) {
28 const [numberString, unit] = humanFileSize.split(' ');

Callers 5

ListFilesFunction · 0.90
ListPhotosFunction · 0.90
files_test.tsFile · 0.90
ListFilesFunction · 0.90
ListPhotosFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected