-
Notifications
You must be signed in to change notification settings - Fork 7
/
route-pattern.js
370 lines (305 loc) · 11.5 KB
/
route-pattern.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
var querystring = require("querystring");
// # Utility functions
//
// ## Shallow merge two or more objects, e.g.
// merge({a: 1, b: 2}, {a: 2}, {a: 3}) => {a: 3, b: 2}
function merge() {
return [].slice.call(arguments).reduce(function (merged, source) {
for (var prop in source) {
merged[prop] = source[prop];
}
return merged;
}, {});
}
// Split a location string into different parts, e.g.:
// splitLocation("/foo/bar?fruit=apple#some-hash") => {
// path: "/foo/bar", queryString: "fruit=apple", hash: "some-hash"
// }
function splitLocation(location) {
var re = /([^\?#]*)?(\?[^#]*)?(#.*)?$/;
var match = re.exec(location);
return {
path: match[1] || '',
queryString: match[2] && match[2].substring(1) || '',
hash: match[3] && match[3].substring(1) || ''
}
}
// # QueryStringPattern
// The QueryStringPattern holds a compiled version of the query string part of a route string, i.e.
// ?foo=:foo&fruit=:fruit
var QueryStringPattern = (function () {
// The RoutePattern constructor
// Takes a route string or regexp as parameter and provides a set of utility functions for matching against a
// location path
function QueryStringPattern(options) {
// The query parameters specified
this.params = options.params;
// if allowWildcards is set to true, unmatched query parameters will be ignored
this.allowWildcards = options.allowWildcards;
// The original route string (optional)
this.routeString = options.routeString;
}
QueryStringPattern.prototype.matches = function (queryString) {
var givenParams = (queryString || '').split("&").reduce(function (params, pair) {
var parts = pair.split("="),
name = parts[0],
value = parts[1];
if (name) params[name] = value;
return params;
}, {});
var requiredParam, requiredParams = [].concat(this.params);
while (requiredParam = requiredParams.shift()) {
if (!givenParams.hasOwnProperty(requiredParam.key)) return false;
if (requiredParam.value && givenParams[requiredParam.key] != requiredParam.value) return false;
}
if (!this.allowWildcards && this.params.length) {
if (Object.getOwnPropertyNames(givenParams).length > this.params.length) return false;
}
return true;
};
QueryStringPattern.prototype.match = function (queryString) {
if (!this.matches(queryString)) return null;
var data = {
params: [],
namedParams: {},
namedQueryParams: {}
};
if (!queryString) {
return data;
}
// Create a mapping from each key in params to their named param
var namedParams = this.params.reduce(function (names, param) {
names[param.key] = param.name;
return names;
}, {});
var parsedQueryString = querystring.parse(queryString);
Object.keys(parsedQueryString).forEach(function(key) {
var value = parsedQueryString[key];
data.params.push(value);
if (namedParams[key]) {
data.namedQueryParams[namedParams[key]] = data.namedParams[namedParams[key]] = value;
}
});
return data;
};
QueryStringPattern.fromString = function (routeString) {
var options = {
routeString: routeString,
allowWildcards: false,
params: []
};
// Extract named parameters from the route string
// Construct an array with some metadata about each of the named parameters
routeString.split("&").forEach(function (pair) {
if (!pair) return;
var parts = pair.split("="),
name = parts[0],
value = parts[1] || '';
var wildcard = false;
var param = { key: name };
// Named parameters starts with ":"
if (value.charAt(0) == ':') {
// Thus the name of the parameter is whatever comes after ":"
param.name = value.substring(1);
}
else if (name == '*' && value == '') {
// If current param is a wildcard parameter, the options are flagged as accepting wildcards
// and the current parameter is not added to the options' list of params
wildcard = options.allowWildcards = true;
}
else {
// The value is an exact match, i.e. the route string
// page=search&q=:query will match only when the page parameter is "search"
param.value = value;
}
if (!wildcard) {
options.params.push(param);
}
});
return new QueryStringPattern(options);
};
return QueryStringPattern;
})();
// # PathPattern
// The PathPattern holds a compiled version of the path part of a route string, i.e.
// /some/:dir
var PathPattern = (function () {
// These are the regexps used to construct a regular expression from a route pattern string
// Based on route patterns in Backbone.js
var
pathParam = /:\w+/g,
splatParam = /\*\w+/g,
namedParams = /(:[^\/\.]+)|(\*\w+)/g,
subPath = /\*/g,
escapeRegExp = /[-[\]{}()+?.,\\^$|#\s]/g;
// The PathPattern constructor
// Takes a route string or regexp as parameter and provides a set of utility functions for matching against a
// location path
function PathPattern(options) {
// The route string are compiled to a regexp (if it isn't already)
this.regexp = options.regexp;
// The query parameters specified in the path part of the route
this.params = options.params;
// The original routestring (optional)
this.routeString = options.routeString;
}
PathPattern.prototype.matches = function (pathname) {
return this.regexp.test(pathname);
};
// Extracts all matched parameters
PathPattern.prototype.match = function (pathname) {
if (!this.matches(pathname)) return null;
// The captured data from pathname
var data = {
params: [],
namedParams: {}
};
// Using a regexp to capture named parameters on the pathname (the order of the parameters is significant)
(this.regexp.exec(pathname) || []).slice(1).forEach(function (value, idx) {
if(value !== undefined) {
value = decodeURIComponent(value);
}
data.namedParams[this.params[idx]] = value;
data.params.push(value);
}, this);
return data;
};
PathPattern.routePathToRegexp = function (path) {
path = path
.replace(escapeRegExp, "\\$&")
.replace(pathParam, "([^/]+)")
.replace(splatParam, "(.*)?")
.replace(subPath, ".*?")
.replace(/\/?$/, "/?");
return new RegExp("^/?" + path + "$");
};
// This compiles a route string into a set of options which a new PathPattern is created with
PathPattern.fromString = function (routeString) {
// Whatever comes after ? and # is ignored
routeString = routeString.split(/\?|#/)[0];
// Create the options object
// Keep the original routeString and a create a regexp for the pathname part of the url
var options = {
routeString: routeString,
regexp: PathPattern.routePathToRegexp(routeString),
params: (routeString.match(namedParams) || []).map(function (param) {
return param.substring(1);
})
};
// Options object are created, now instantiate the PathPattern
return new PathPattern(options);
};
return PathPattern;
}());
// # RegExpPattern
// The RegExpPattern is just a simple wrapper around a regex, used to provide a similar api as the other route patterns
var RegExpPattern = (function () {
// The RegExpPattern constructor
// Wraps a regexp and provides a *Pattern api for it
function RegExpPattern(regex) {
this.regex = regex;
}
RegExpPattern.prototype.matches = function (loc) {
return this.regex.test(loc);
};
// Extracts all matched parameters
RegExpPattern.prototype.match = function (location) {
if (!this.matches(location)) return null;
var loc = splitLocation(location);
return {
params: this.regex.exec(location).slice(1),
queryParams: querystring.parse(loc.queryString),
namedParams: {}
};
};
return RegExpPattern;
}());
// # RoutePattern
// The RoutePattern combines the PathPattern and the QueryStringPattern so it can represent a full location
// (excluding the scheme + domain part)
// It also allows for having path-like routes in the hash part of the location
// Allows for route strings like:
// /some/:page?param=:param&foo=:foo#:bookmark
// /some/:page?param=:param&foo=:foo#/:section/:bookmark
//
// Todo: maybe allow for parameterization of the kind of route pattern to use for the hash?
// Maybe use the QueryStringPattern for cases like
// /some/:page?param=:param&foo=:foo#?onlyCareAbout=:thisPartOfTheHash&*
// Need to test how browsers handles urls like that
var RoutePattern = (function () {
// The RoutePattern constructor
// Takes a route string or regexp as parameter and provides a set of utility functions for matching against a
// location path
function RoutePattern(options) {
// The route string are compiled to a regexp (if it isn't already)
this.pathPattern = options.pathPattern;
this.queryStringPattern = options.queryStringPattern;
this.hashPattern = options.hashPattern;
// The original routestring (optional)
this.routeString = options.routeString;
}
RoutePattern.prototype.matches = function (location) {
// Whatever comes after ? and # is ignored
var loc = splitLocation(location);
return (!this.pathPattern || this.pathPattern.matches(loc.path)) &&
(!this.queryStringPattern || this.queryStringPattern.matches(loc.queryString) ) &&
(!this.hashPattern || this.hashPattern.matches(loc.hash))
};
// Extracts all matched parameters
RoutePattern.prototype.match = function (location) {
if (!this.matches(location)) return null;
// Whatever comes after ? and # is ignored
var loc = splitLocation(location),
match,
pattern;
var data = {
params: [],
namedParams: {},
pathParams: {},
queryParams: querystring.parse(loc.queryString),
namedQueryParams: {},
hashParams: {}
};
var addMatch = function (match) {
data.params = data.params.concat(match.params);
data.namedParams = merge(data.namedParams, match.namedParams);
};
if (pattern = this.pathPattern) {
match = pattern.match(loc.path);
if (match) addMatch(match);
data.pathParams = match ? match.namedParams : {};
}
if (pattern = this.queryStringPattern) {
match = pattern.match(loc.queryString);
if (match) addMatch(match);
data.namedQueryParams = match ? match.namedQueryParams : {};
}
if (pattern = this.hashPattern) {
match = pattern.match(loc.hash);
if (match) addMatch(match);
data.hashParams = match ? match.namedParams : {};
}
return data;
};
// This compiles a route string into a set of options which a new RoutePattern is created with
RoutePattern.fromString = function (routeString) {
var parts = splitLocation(routeString);
var matchPath = parts.path;
var matchQueryString = parts.queryString || routeString.indexOf("?") > -1;
var matchHash = parts.hash || routeString.indexOf("#") > -1;
// Options object are created, now instantiate the RoutePattern
return new RoutePattern({
pathPattern: matchPath && PathPattern.fromString(parts.path),
queryStringPattern: matchQueryString && QueryStringPattern.fromString(parts.queryString),
hashPattern: matchHash && PathPattern.fromString(parts.hash),
routeString: routeString
});
};
return RoutePattern;
}());
// CommonJS export
module.exports = RoutePattern;
// Also export the individual pattern classes
RoutePattern.QueryStringPattern = QueryStringPattern;
RoutePattern.PathPattern = PathPattern;
RoutePattern.RegExpPattern = RegExpPattern;