-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
58 lines (48 loc) · 1.22 KB
/
Copy pathutils.js
File metadata and controls
58 lines (48 loc) · 1.22 KB
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
function pad0(str, len) {
const r = str || str === 0 ? `${str}` : '';
return r.padStart(len, '0');
}
function formatTimestamp(time) {
if (time < 60) {
return `${time}s`;
}
if (time < 3600) {
return `${(time / 60) | 0}m ${pad0(time % 60, 2)}s`;
}
return `${(time / 3600) | 0}h ${pad0(((time / 60) | 0) % 60, 2)}m ${pad0(
time % 60,
2,
)}s`;
}
function readFile(file) {
return new Promise((resolve, reject) => {
const fr = new FileReader();
fr.onload = () => resolve(fr.result);
fr.onerror = (e) => reject(e);
fr.readAsArrayBuffer(file);
});
}
function humanFileSize(bytes, si = false, dp = 1) {
const thresh = si ? 1000 : 1024;
if (Math.abs(bytes) < thresh) {
return `${bytes} B`;
}
const units = si
? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
let u = -1;
const r = 10 ** dp;
let result = bytes;
do {
result /= thresh;
u += 1;
} while (
Math.round(Math.abs(result) * r) / r >= thresh &&
u < units.length - 1
);
return `${result.toFixed(dp)} ${units[u]}`;
}
function $(id) {
return document.getElementById(id);
}
export { $, readFile, formatTimestamp, pad0, humanFileSize };