forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatchRoutes.js
More file actions
121 lines (107 loc) · 3.51 KB
/
matchRoutes.js
File metadata and controls
121 lines (107 loc) · 3.51 KB
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
import { loopAsync } from './AsyncUtils';
import { matchPattern } from './PatternUtils';
function getChildRoutes(route, location, callback) {
if (route.childRoutes) {
callback(null, route.childRoutes);
} else if (route.getChildRoutes) {
route.getChildRoutes(location, callback);
} else {
callback();
}
}
function getIndexRoute(route, location, callback) {
if (route.indexRoute) {
callback(null, route.indexRoute);
} else if (route.getIndexRoute) {
route.getIndexRoute(location, callback);
} else {
callback();
}
}
function assignParams(params, paramNames, paramValues) {
return paramNames.reduceRight(function (params, paramName, index) {
var paramValue = paramValues && paramValues[index];
if (Array.isArray(params[paramName])) {
params[paramName].unshift(paramValue);
} else if (paramName in params) {
params[paramName] = [ paramValue, params[paramName] ];
} else {
params[paramName] = paramValue;
}
return params;
}, params);
}
function createParams(paramNames, paramValues) {
return assignParams({}, paramNames, paramValues);
}
function matchRouteDeep(basename, route, location, callback) {
var pattern = route.path || '';
if (pattern.indexOf('/') !== 0)
pattern = basename.replace(/\/*$/, '/') + pattern; // Relative paths build on the parent's path.
var { remainingPathname, paramNames, paramValues } = matchPattern(pattern, location.pathname);
var isExactMatch = remainingPathname === '';
if (isExactMatch && route.path) {
var match = {
routes: [ route ],
params: createParams(paramNames, paramValues)
};
getIndexRoute(route, location, function (error, indexRoute) {
if (error) {
callback(error);
} else {
if (indexRoute)
match.routes.push(indexRoute);
callback(null, match);
}
});
} else if (remainingPathname != null || route.childRoutes) {
// Either a) this route matched at least some of the path or b)
// we don't have to load this route's children asynchronously. In
// either case continue checking for matches in the subtree.
getChildRoutes(route, location, function (error, childRoutes) {
if (error) {
callback(error);
} else if (childRoutes) {
// Check the child routes to see if any of them match.
matchRoutes(childRoutes, location, function (error, match) {
if (error) {
callback(error);
} else if (match) {
// A child route matched! Augment the match and pass it up the stack.
match.routes.unshift(route);
callback(null, match);
} else {
callback();
}
}, pattern);
} else {
callback();
}
});
} else {
callback();
}
}
/**
* Asynchronously matches the given location to a set of routes and calls
* callback(error, state) when finished. The state object will have the
* following properties:
*
* - routes An array of routes that matched, in hierarchical order
* - params An object of URL parameters
*
* Note: This operation may finish synchronously if no routes have an
* asynchronous getChildRoutes method.
*/
function matchRoutes(routes, location, callback, basename='') {
loopAsync(routes.length, function (index, next, done) {
matchRouteDeep(basename, routes[index], location, function (error, match) {
if (error || match) {
done(error, match);
} else {
next();
}
});
}, callback);
}
export default matchRoutes;