-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.mjs
More file actions
64 lines (56 loc) · 1.84 KB
/
Copy pathutils.mjs
File metadata and controls
64 lines (56 loc) · 1.84 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
59
60
61
62
63
64
export async function fetchText(url) {
const res = await fetch(url);
const content = await res.text();
return content;
}
export async function fetchJson(url) {
const res = await fetch(url);
const content = await res.json();
return content;
}
export function waitForClick() {
return new Promise((resolve) => {
document.addEventListener('click', resolve, { once: true });
});
}
export function sleepSecs(dt) {
return new Promise((resolve) => {
setTimeout(resolve, dt * 1000);
});
}
function removeIfAbove(maxAmount) {
const codeEls = Array.from(document.querySelectorAll('code'));
if (codeEls.length >= maxAmount) {
document.body.removeChild(codeEls[0]);
}
}
export function printAscii(data, classes = []) {
removeIfAbove(22);
const el = document.createElement('code');
['ascii', ...classes].forEach((cn) => el.classList.add(cn));
el.appendChild(document.createTextNode(data));
document.body.appendChild(el);
}
export function download(filename, text, type = 'text/plain') {
const element = document.createElement('a');
const file = new Blob([text], { type });
element.href = URL.createObjectURL(file);
element.download = filename;
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
export function downloadBinary(filename, uint8Array, type = 'application/octet-stream') {
const element = document.createElement('a');
const file = new Blob([uint8Array], { type });
element.href = URL.createObjectURL(file);
element.download = filename;
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
export function percent(r, decimals=0) {
return (r * 100).toFixed(decimals) + '%';
}