-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
format.js
56 lines (44 loc) · 1.04 KB
/
format.js
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
const units = [ 'B', 'KB', 'MB', 'GB', 'TB', 'PB' ]
export function humanStorageSize (bytes, decimals = 1) {
let u = 0
while (parseInt(bytes, 10) >= 1024 && u < units.length - 1) {
bytes /= 1024
++u
}
return `${ bytes.toFixed(decimals) }${ units[ u ] }`
}
export function capitalize (str) {
return str.charAt(0).toUpperCase() + str.slice(1)
}
export function between (v, min, max) {
return max <= min
? min
: Math.min(max, Math.max(min, v))
}
export function normalizeToInterval (v, min, max) {
if (max <= min) {
return min
}
const size = (max - min + 1)
let index = min + (v - min) % size
if (index < min) {
index = size + index
}
return index === 0 ? 0 : index // fix for (-a % a) => -0
}
export function pad (v, length = 2, char = '0') {
if (v === void 0 || v === null) {
return v
}
const val = '' + v
return val.length >= length
? val
: new Array(length - val.length + 1).join(char) + val
}
export default {
humanStorageSize,
capitalize,
between,
normalizeToInterval,
pad
}