-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (45 loc) · 1017 Bytes
/
index.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
const api = Object.freeze({
promise: {
allFullfilled: promiseAllFullfilled,
timeout: promiseTimeout
},
iterable: {
sequence: sequentialIterator
},
number: {
isValidHex
}
})
module.exports = api
function sequentialIterator (arr, sequence) {
arr[Symbol.iterator] = function * () {
while (arr.length) {
if (arr.length < sequence) {
yield arr.splice(0, arr.length)
} else {
yield arr.splice(0, sequence)
}
}
}
return arr[Symbol.iterator]()
}
function promiseTimeout (length = 0) {
const timeout = new Promise(function (resolve, reject) {
setTimeout(resolve, length)
})
return timeout
}
function promiseAllFullfilled (promises) {
return Promise.all(promises.map((prom) => prom.catch(err => err)))
}
function isValidHex (num) {
for (let cha of num) {
if (typeof cha !== 'string' || cha.length > 1) {
return false
}
if (isNaN(parseInt('0x' + cha, 16))) {
return false
}
}
return !!num.length
}