-
Notifications
You must be signed in to change notification settings - Fork 32
/
utils.js
38 lines (33 loc) · 795 Bytes
/
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
'use strict';
const debug = require('debug')('rbac');
module.exports.any = any;
module.exports.isGlob = isGlob;
module.exports.globToRegex = globToRegex;
/**********************/
function any(promises) {
if(promises.length < 1) {
return Promise.resolve(false);
}
return Promise.all(
promises.map($p =>
$p
.catch(err => {
debug('Underlying promise rejected', err);
return false;
})
.then(result => {
if(result) {
throw new Error('authorized');
}
})
)
)
.then(() => false)
.catch(err => err && err.message === 'authorized');
}
function isGlob(string) {
return string.includes('*');
}
function globToRegex(string) {
return new RegExp('^' + string.replace(/\*/g, '.*'));
}