forked from atanas-dev/vue-router-multiguard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
32 lines (26 loc) · 760 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
function isUndefined(value) {
return value === undefined;
}
function evaluateGuards(guards, to, from, next) {
const guardsLeft = guards.slice(0); // clone the array so we do not accidentally modify it
const nextGuard = guardsLeft.shift();
if (isUndefined(nextGuard)) {
next();
return;
}
nextGuard(to, from, (nextArg) => {
if (isUndefined(nextArg)) {
evaluateGuards(guardsLeft, to, from, next);
return;
}
next(nextArg);
});
}
module.exports = function(guards) {
if (!Array.isArray(guards)) {
throw new Error('You must specify an array of guards');
}
return (to, from, next) => {
return evaluateGuards(guards, to, from, next);
};
}