-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
41 lines (35 loc) · 871 Bytes
/
util.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
const { JOB_ID_LENGTH } = require('./constants')
const isBuffer = require('is-buffer')
function isValidJobId(jobId) {
if (!jobId || ('string' !== typeof jobId && !isBuffer(jobId))) {
return false
}
if (isBuffer(jobId)) {
jobId = jobId.toString('hex')
}
const n = 2
const isHex = '0x' === jobId.slice(0, n)
if ((isHex && JOB_ID_LENGTH !== jobId.length - n)
|| (!isHex && (JOB_ID_LENGTH !== jobId.length))) {
return false
}
return true
}
async function isValidArray(arr, fn) {
if (arr && Array.isArray(arr) && arr.length > 0) {
let valid = true
if (fn) {
/* eslint-disable no-await-in-loop */
for (let i = 0; i < arr.length; i++) {
const result = await fn(arr[i], i)
valid = valid && result
}
}
return valid
}
return false
}
module.exports = {
isValidJobId,
isValidArray
}