forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatters.ts
More file actions
34 lines (27 loc) · 965 Bytes
/
formatters.ts
File metadata and controls
34 lines (27 loc) · 965 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const _CONVERSION_UNIT = 1024;
/**
* Given a value in megabytes converts it back down into bytes.
*/
function mbToBytes(megabytes: number): number {
return Math.floor(megabytes * _CONVERSION_UNIT * _CONVERSION_UNIT);
}
/**
* Given an amount of bytes, converts them into a human readable string format
* using "1024" as the divisor.
*/
function bytesToString(bytes: number, decimals = 2): string {
const k = _CONVERSION_UNIT;
if (bytes < 1) return '0 Bytes';
decimals = Math.floor(Math.max(0, decimals));
const i = Math.floor(Math.log(bytes) / Math.log(k));
const value = Number((bytes / Math.pow(k, i)).toFixed(decimals));
return `${value} ${['Bytes', 'KiB', 'MiB', 'GiB', 'TiB'][i]}`;
}
/**
* Formats an IPv4 or IPv6 address.
*/
function ip(value: string): string {
// noinspection RegExpSimplifiable
return /([a-f0-9:]+:+)+[a-f0-9]+/.test(value) ? `[${value}]` : value;
}
export { ip, mbToBytes, bytesToString };