-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
57 lines (52 loc) · 1.6 KB
/
utils.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
57
export const monotonicNowMillis = () => window.performance.now();
export const sleepMillis = (ms) => new Promise(resolve => setTimeout(resolve, ms));
export const divCeil = (a, b) => Math.ceil(a / b);
export const clearArray = (array) => {
array.splice(0, array.length);
};
export const isSubset = (a, b) => {
const unionSet = new Set([...a, ...b]);
const bSet = new Set(b);
return unionSet.size === bSet.size;
};
export const unduplicate = (array) => [...new Set(array)];
const htmlEntityMap = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
'/': '/',
'`': '`',
'=': '=',
};
export const htmlEscape = (s) => {
return String(s).replace(/[&<>"'`=/]/g, c => htmlEntityMap[c]);
};
export const fromHtml = (html) => {
const tmpl = document.createElement('template');
tmpl.innerHTML = html;
return tmpl.content.firstElementChild;
};
export const parseSearchString = (search) => {
const segments = search.slice(search.indexOf('?') + 1).split('&');
const result = {};
for (const segment of segments) {
const [key, value] = segment.split('=', /*limit=*/2);
if (value === undefined)
continue;
result[decodeURIComponent(key)] = decodeURIComponent(value);
}
return result;
};
export const createAnchor = (link) => {
const a = document.createElement('a');
a.setAttribute('href', link);
a.setAttribute('rel', 'noopener noreferrer');
a.setAttribute('target', '_blank');
a.append(link);
return a;
};
export const doReloadPage = () => {
window.location.reload();
};