-
Notifications
You must be signed in to change notification settings - Fork 1
/
route.js
50 lines (49 loc) · 1.68 KB
/
route.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
const prepare = (base, route) => {
// prefix the base to always start with a '/' and remove trailing slash
base = '/'+base.replace(/^[\/]+|[\/]+$/g, '');
// strip multiple occurences of '/'
route = (`${base}/${route}`).replace(/[\/]+/g, '/');
// remove leading and trailing slashes
route = route.replace(/^[\/]+|[\/]+$/g, '');
// get if it's explicit or not. could be a factor when determining route based on it's size/weight
// in terms of what has presedent when two routes would've matched the same url
const explicit = /\*$/.test(route);
// if it's implicit or explicit
const lazy = explicit ? (route = route.replace(/[\*]+$/g, ''), ''): '/?$';
// store parameters
const parameters = [];
let index = 0;
let regexpRoute = route.replace(/(:)?([^\\/]+)/g, (parameter, colonParameter, identifier) => {
const [ param, boundValue ] = identifier.split('->');
if(colonParameter){
// check for duplicates
const duplicates = parameters.filter(old => old.identifier == boundValue);
if(duplicates.length > 0){
throw new Error(`Duplicated parameter. [${duplicates.map(_=>_.identifier)}]`);
}
// store parameter reference
parameters.push({
index: index++,
parameter,
identifier: param,
});
// bound parameter
return boundValue ? `(${boundValue})` : `([^\/]+)`;
}
return `${parameter}`;
});
regexpRoute = `^/${regexpRoute}${lazy}`;
return {
base,
route,
regexpRoute,
parameters,
}
}
export default class Route{
constructor(base, route, fn, middlewares = []){
Object.assign(this, prepare(base, route));
this.callback = fn;
this.middlewares = middlewares;
}
}