-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathindex.js
179 lines (156 loc) · 3.75 KB
/
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
var localRoutes = [];
/**
* Convert path to route object
*
* A string or RegExp should be passed,
* will return { re, src, keys} obj
*
* @param {String / RegExp} path
* @return {Object}
*/
var Route = function(path){
//using 'new' is optional
var src, re, keys = [];
if(path instanceof RegExp){
re = path;
src = path.toString();
}else{
re = pathToRegExp(path, keys);
src = path;
}
return {
re: re,
src: path.toString(),
keys: keys
}
};
/**
* Normalize the given path string,
* returning a regular expression.
*
* An empty array should be passed,
* which will contain the placeholder
* key names. For example "/user/:id" will
* then contain ["id"].
*
* @param {String} path
* @param {Array} keys
* @return {RegExp}
*/
var pathToRegExp = function (path, keys) {
path = path
.concat('/?')
.replace(/\/\(/g, '(?:/')
.replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?|\*/g, function(_, slash, format, key, capture, optional){
if (_ === "*"){
keys.push(undefined);
return _;
}
keys.push(key);
slash = slash || '';
return ''
+ (optional ? '' : slash)
+ '(?:'
+ (optional ? slash : '')
+ (format || '') + (capture || '([^/]+?)') + ')'
+ (optional || '');
})
.replace(/([\/.])/g, '\\$1')
.replace(/\*/g, '(.*)');
return new RegExp('^' + path + '$', 'i');
};
/**
* Attempt to match the given request to
* one of the routes. When successful
* a {fn, params, splats} obj is returned
*
* @param {Array} routes
* @param {String} uri
* @return {Object}
*/
var match = function (routes, uri, startAt) {
var captures, i = startAt || 0;
for (var len = routes.length; i < len; ++i) {
var route = routes[i],
re = route.re,
keys = route.keys,
splats = [],
params = {};
if (captures = uri.match(re)) {
for (var j = 1, len = captures.length; j < len; ++j) {
var key = keys[j-1],
val = typeof captures[j] === 'string'
? unescape(captures[j])
: captures[j];
if (key) {
params[key] = val;
} else {
splats.push(val);
}
}
return {
params: params,
splats: splats,
route: route.src,
next: i + 1
};
}
}
};
/**
* Default "normal" router constructor.
* accepts path, fn tuples via addRoute
* returns {fn, params, splats, route}
* via match
*
* @return {Object}
*/
var Router = function(){
//using 'new' is optional
return {
routes: [],
routeMap : {},
addRoute: function(path, fn){
if (!path) throw new Error(' route requires a path');
if (!fn) throw new Error(' route ' + path.toString() + ' requires a callback');
if (this.routeMap[path]) {
throw new Error('path is already defined: ' + path);
}
var route = Route(path);
route.fn = fn;
this.routes.push(route);
this.routeMap[path] = fn;
},
removeRoute: function(path) {
if (!path) throw new Error(' route requires a path');
if (!this.routeMap[path]) {
throw new Error('path does not exist: ' + path);
}
var match;
var newRoutes = [];
// copy the routes excluding the route being removed
for (var i = 0; i < this.routes.length; i++) {
var route = this.routes[i];
if (route.src !== path) {
newRoutes.push(route);
}
}
this.routes = newRoutes;
delete this.routeMap[path];
},
match: function(pathname, startAt){
var route = match(this.routes, pathname, startAt);
if(route){
route.fn = this.routeMap[route.route];
route.next = this.match.bind(this, pathname, route.next)
}
return route;
}
}
};
Router.Route = Route
Router.pathToRegExp = pathToRegExp
Router.match = match
// back compat
Router.Router = Router
module.exports = Router