-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
160 lines (137 loc) · 2.74 KB
/
index.ts
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
export type Unit =
| 'B'
| 'Byte'
| 'Bytes'
| 'KB'
| 'Kilobyte'
| 'Kilobytes'
| 'MB'
| 'Megabyte'
| 'Megabytes'
| 'GB'
| 'Gigabyte'
| 'Gigabytes'
| 'TB'
| 'Terabyte'
| 'Terabytes'
| 'PB'
| 'Petabyte'
| 'Petabytes'
| 'EB'
| 'Exabyte'
| 'Exabytes'
| 'ZB'
| 'Zettabyte'
| 'Zettabytes'
| 'YB'
| 'Yottabyte'
| 'Yottabytes'
| 'KiB'
| 'Kibibyte'
| 'Kibibytes'
| 'MiB'
| 'Mebibyte'
| 'Mebibytes'
| 'GiB'
| 'Gibibyte'
| 'Gibibytes'
| 'TiB'
| 'Tebibyte'
| 'Tebibytes'
| 'PiB'
| 'Pebibyte'
| 'Pebibytes'
| 'EiB'
| 'Exbibyte'
| 'Exbibytes'
| 'ZiB'
| 'Zebibyte'
| 'Zebibytes'
| 'YiB'
| 'Yobibyte'
| 'Yobibytes'
const METRIC: Readonly<[string, string][]> = [
['KB', 'Kilobyte'],
['MB', 'Megabyte'],
['GB', 'Gigabyte'],
['TB', 'Terabyte'],
['PB', 'Petabyte'],
['EB', 'Exabyte'],
['ZB', 'Zettabyte'],
['YB', 'Yottabyte']
]
const BINARY: Readonly<[string, string][]> = [
['KiB', 'Kibibyte'],
['MiB', 'Mebibyte'],
['GiB', 'Gibibyte'],
['TiB', 'Tebibyte'],
['PiB', 'Pebibyte'],
['EiB', 'Exbibyte'],
['ZiB', 'Zebibyte'],
['YiB', 'Yobibyte']
]
/**
* Convert bytes to readable size and vice versa.
*/
export function bytes(
value: number,
options?: {
fmt?: 'long' | 'short'
unit?: 'metric' | 'binary'
}
): `${number} ${Unit}`
export function bytes(value: `${number} ${Unit}`): number
export function bytes(
val: `${number} ${Unit}` | number,
opt: {
fmt?: 'long' | 'short'
unit?: 'metric' | 'binary'
} = {}
): `${number} ${Unit}` | number {
if (typeof val === 'string') {
const arr = val.split(' ')
if (arr.length !== 2)
throw Error()
if (arr[1].endsWith('s'))
arr[1] = arr[1].slice(0, -1)
if (arr[1] === 'B' || arr[1] === 'Byte')
return parseInt(arr[0])
for (let i = 0; i < BINARY.length; i++)
if (arr[1] === BINARY[i][0] || arr[1] === BINARY[i][1])
return parseInt(arr[0]) * 1024 ** (i + 1)
for (let i = 0; i < METRIC.length; i++)
if (arr[1] === METRIC[i][0] || arr[1] === METRIC[i][1])
return parseInt(arr[0]) * 1000 ** (i + 1)
throw Error()
}
let div = 1000
, units = METRIC
if (opt.unit === 'binary') {
div = 1024
units = BINARY
}
if (val < div) {
const unit = opt.fmt === 'long' ? 'Byte' : 'B'
return val + ' ' + unit as `${number} ${Unit}`
}
let i = 0
, rest = val
while (rest >= div && i < 8) {
rest = val / (div ** (i + 1))
i++
}
let unit
if (opt.fmt === 'long') {
unit = units[i - 1][1]
if (rest > 1)
unit += 's'
} else {
unit = units[i - 1][0]
}
rest = Number(
Math.round(
Number(rest + 'e' + 2)
) + 'e' + -2
)
return rest + ' ' + unit as `${number} ${Unit}`
}