-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
96 lines (88 loc) · 1.78 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
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
function genLexicoOrder(...keys) {
return (a, b) => {
for (const key of keys) {
const isInA = key in a;
const isInB = key in b;
if (!isInA) {
if (!isInB) {
continue;
}
return -1;
}
if (!isInB) {
return 1;
}
const x = a[key];
const y = b[key];
if (x > y) {
return 1;
}
if (x < y) {
return -1;
}
}
return 0;
};
}
async function getJSON(url) {
let response;
while (true) {
try {
response = await fetch(url);
break;
} catch {
console.warn(`Retry requesting ${url} in 5 seconds.`);
await new Promise(
(resolve) => setTimeout(resolve, 5000)
);
}
}
if (response.ok) {
return response.json();
}
throw new ReferenceError(`Failed to request ${url}.`);
}
function getJSONList(list) {
return Promise.all(
list.map((item) => utils.getJSON(item))
);
}
function isEmpty(obj) {
if (obj[Symbol.iterator]) {
for (const item of obj) {
return false;
}
}
for (const key in obj) {
return false;
}
return true;
}
export {
/**
* Generate a comparing function of the lexicographical order from keys.
* @param {...string} keys
* @return {function(Object, Object): number}
*/
genLexicoOrder,
/**
* Get an object from the URL to a JSON file.
* @param {string} url
* @return {Promise<Object>}
*/
getJSON,
/**
* Get an array of objects from an array of URLs to JSON files.
* @param {string[]} list
* @return {Promise<Object[]>}
*/
getJSONList,
/**
* Check if an object's `@@iterator`,
* if it exists, produces no next value,
* *and* the object has no enumerable key.
* @param {Object} obj
* @return {boolean}
*/
isEmpty,
};