-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
283 lines (235 loc) · 7.09 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
function extend(target, source) {
if (!target) {
target = {};
}
if (source) {
for (var key in source) {
if (source.hasOwnProperty(key)) {
target[key] = source[key];
}
}
}
return target;
}
function Placeholder(key) {
this.key = key;
}
Placeholder.prototype.toString = function(params) {
if (params) {
var value = params[this.key];
return (value == null) ? '' : value.toString();
} else {
return ':' + this.key;
}
};
function Route(config) {
if (config) {
this.path = config.path;
this._tokens = config.tokens;
this._regex = config.regex;
this._placeholders = config.placeholders;
}
}
var Route_prototype = Route.prototype;
/**
* @return object containing parameters (or empty object if no placeholders) or
* false if the path does not match this route
*/
Route_prototype.matches = function(path) {
var placeholders = this._placeholders;
if (this._regex) {
var captures = this._regex.exec(path);
if (!captures) {
return false;
}
var params = {};
for (var i = 1; i < captures.length; i++) {
var capture = captures[i];
params[placeholders[i-1].key] = capture;
}
return params;
} else {
return (this.path === path) ? {} : false;
}
};
Route_prototype.toString = function(params) {
if (params === undefined) {
return this.path;
} else if (this._tokens) {
var tokens = this._tokens;
var parts = new Array(tokens.length);
for (var i = 0; i < tokens.length; i++) {
parts[i] = tokens[i].toString(params);
}
return parts.join('/');
} else {
return this.path;
}
};
Route_prototype.isRoutable = function() {
return !!this.path;
};
Route_prototype.getPlaceholders = function() {
var placeholders = this._placeholders;
if (!placeholders) {
return [];
}
var result = new Array(placeholders.length);
var i = placeholders.length;
while(--i >= 0) {
result[i] = placeholders[i].key;
}
return result;
};
/*
* The following characters will automatically be escaped with backslash when building
* regex pattern. We only need to add the characters that are "special" for regex and
* that are allowed to appear in URL.
*/
var escapeRegex = /([\/.\\])/g;
function _parseRoute(path) {
// the tokens that can be used to serialize a route
var tokens = [];
// location of dynamic properties (assumes each propery is only used once)
var placeholders = [];
var regexPattern = [];
// first, split the route into parts using the '/' as a delimiter
var parts = path.split('/');
for (var i = 0; i < parts.length; i++) {
var part = parts[i];
if (part.length > 0) {
var key;
var placeholder;
// placeholder parts always begin with ':'
if (part.charAt(0) === ':') {
// placeholder
key = part.substring(1);
placeholder = new Placeholder(key);
placeholders.push(placeholder);
tokens.push(placeholder);
regexPattern.push('([^/]*)');
} else if (part === '**') {
// wildcard
key = placeholders.length;
placeholder = new Placeholder(key);
placeholders.push(placeholder);
tokens.push(placeholder);
regexPattern.push('(.*)');
} else if (part === '?') {
// optional slash
regexPattern.push('?');
} else {
// normal path part
tokens.push(part);
regexPattern.push(part.replace(escapeRegex, '\\$1'));
}
} else {
// this part is an empty string (for example, the part before the leading '/')
tokens.push('');
regexPattern.push('');
}
}
if (regexPattern.length > 0) {
// combine the regex parts to form the final regex pattern
var pattern = '^' + regexPattern.join('\\/') + '$';
return new Route({
path: path,
tokens: tokens,
placeholders: (placeholders.length > 0) ? placeholders : undefined,
regex: new RegExp(pattern)
});
} else {
// path contains no placeholders so we will only need to check for exact match
return new Route({
path: path
});
}
}
function _createRoute(routeConfig) {
var route;
if (routeConfig.constructor === String) {
// routeConfig is a simple route pattern
route = _parseRoute(routeConfig);
} else if (routeConfig.constructor === Route) {
route = routeConfig;
} else {
if (routeConfig.path === undefined) {
route = new Route();
} else {
// routeConfig is a route configuration
// that contains a "path" property.
route = _parseRoute(routeConfig.path);
}
// transfer config properties to route
extend(route, routeConfig);
}
return route;
}
function Router(options) {
this.routes = [];
if (options && options.routes) {
for (var i = 0; i < options.routes.length; i++) {
this.addRoute(options.routes[i]);
}
}
}
extend(Router.prototype, {
/*
* Find the route that matches the given path and return
* an object with "route" and "params" property.
* The "route" property is the Route entry that matched.
* The "params" property is an object that contains the
* placeholder string values.
*
* @param {String} path the path to match
* @param {Number} i the index to start searching from
*/
findRoute: function(path, i) {
if (i === undefined) {
i = 0;
}
var len = this.routes.length;
for (; i < len; i++) {
var route = this.routes[i];
var params = route.matches(path);
if (params) {
// found a matching route so return the match
return {
route: route,
params: params,
routeIndex: i
};
}
}
return null;
},
addRoute: function(routeConfig) {
var route = _createRoute(routeConfig);
if (route.isRoutable()) {
// add route to our array
this.routes.push(route);
}
// return the Route instance
return route;
},
getRoutes: function() {
return this.routes;
},
reset: function() {
this.routes = [];
}
});
module.exports = {
create: function(options) {
return new Router(options);
},
createRoute: function(routeConfig) {
return _createRoute(routeConfig);
},
isRoute: function(route) {
return (route != null) && (route.constructor === Route);
},
Router: Router,
Route: Route
};
module.exports.createRouter = module.exports.create;