Browse code
Refactor ugliness
Ed Langley authored on 16/06/2017 02:35:20
Showing 1 changed files
Showing 1 changed files
... | ... |
@@ -1,28 +1,38 @@ |
1 | 1 |
import R from 'ramda'; |
2 | 2 |
|
3 |
+function pathSplit(path) { |
|
4 |
+ return path.split('/'); |
|
5 |
+} |
|
6 |
+ |
|
3 | 7 |
function mostSpecificRouteMatch(match1, match2) { |
4 | 8 |
|
5 | 9 |
if (!match1) { |
6 | 10 |
return match2; |
7 | 11 |
} |
8 | 12 |
|
9 |
- const match1ParamLength = match1.routeParams.length; |
|
10 |
- const match2ParamLength = match2.routeParams.length; |
|
13 |
+ const paramLength1 = match1.routeParams.length; |
|
14 |
+ const paramLength2 = match2.routeParams.length; |
|
11 | 15 |
|
12 | 16 |
let result = null; |
13 |
- if (match1ParamLength === match2ParamLength) { |
|
14 |
- for (let [segment1, segment2] of R.zip(match1.path.split("/"), match2.path.split("/"))) { |
|
15 |
- if (R.head(segment1) === ":") { |
|
17 |
+ |
|
18 |
+ if (paramLength1 === paramLength2) { |
|
19 |
+ let path1Parts = pathSplit(match1.path); |
|
20 |
+ let path2Parts = pathSplit(match2.path); |
|
21 |
+ |
|
22 |
+ for (let [segment1, segment2] of R.zip(path1Parts, path2Parts)) { |
|
23 |
+ if (isWildcard(segment1)) { |
|
16 | 24 |
result = match2; |
17 |
- break; |
|
18 |
- } else if (R.head(segment2) === ":") { |
|
25 |
+ } else if (isWildcard(segment2)) { |
|
19 | 26 |
result = match1; |
27 |
+ } |
|
28 |
+ |
|
29 |
+ if (result !== null) { |
|
20 | 30 |
break; |
21 | 31 |
} |
22 | 32 |
} |
23 |
- } else if (match1ParamLength > match2ParamLength) { |
|
33 |
+ } else if (paramLength1 > paramLength2) { |
|
24 | 34 |
result = match2; |
25 |
- } else if (match2ParamLength > match1ParamLength) { |
|
35 |
+ } else if (paramLength2 > paramLength1) { |
|
26 | 36 |
result = match1; |
27 | 37 |
} |
28 | 38 |
|
... | ... |
@@ -45,7 +55,11 @@ function matchRoute(loc, matchers) { |
45 | 55 |
const matchedParams = pathMatcher(inputPath); |
46 | 56 |
|
47 | 57 |
if (matchedParams) { |
48 |
- return (matcherType === 'exact')? buildMatch(matchedParams, route) : mostSpecificRouteMatch(match, buildMatch(matchedParams, route)); |
|
58 |
+ if (matcherType === 'exact') { |
|
59 |
+ return buildMatch(matchedParams, route) |
|
60 |
+ } else { |
|
61 |
+ return mostSpecificRouteMatch(match, buildMatch(matchedParams, route)); |
|
62 |
+ } |
|
49 | 63 |
} else { |
50 | 64 |
return match; |
51 | 65 |
} |