Skip to content

Commit 1470b17

Browse files
committed
Simplify logic for checking for path separator
1 parent 4b8e994 commit 1470b17

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

modules/PatternUtils.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export function matchPattern(pattern, pathname) {
9292

9393
let { regexpSource, paramNames, tokens } = compilePattern(pattern)
9494

95-
regexpSource += '/*' // Ignore trailing slashes
95+
regexpSource += '/*' // Capture path separators
9696

9797
// Special-case patterns like '*' for catch-all routes.
9898
const captureRemaining = tokens[tokens.length - 1] !== '*'
@@ -106,28 +106,27 @@ export function matchPattern(pattern, pathname) {
106106

107107
let remainingPathname, paramValues
108108
if (match != null) {
109-
let matchedPath
110109
if (captureRemaining) {
111110
remainingPathname = match.pop()
112-
matchedPath =
111+
const matchedPath =
113112
match[0].substr(0, match[0].length - remainingPathname.length)
114-
} else {
115-
// If this matched at all, then the match was the entire pathname.
116-
matchedPath = match[0]
117-
remainingPathname = ''
118-
}
119113

120-
// Ensure we actually match at a path boundary.
121-
if (remainingPathname && remainingPathname.charAt(0) !== '/') {
122-
// This depends on the leading slash getting added to pathname above to
123-
// work in all cases.
124-
if (!matchedPath || matchedPath.charAt(matchedPath.length - 1) !== '/') {
114+
// If we didn't match the entire pathname, then make sure that the match
115+
// we did get ends at a path separator (potentially the one we added
116+
// above at the beginning of the path, if the actual match was empty).
117+
if (
118+
remainingPathname &&
119+
matchedPath.charAt(matchedPath.length - 1) !== '/'
120+
) {
125121
return {
126122
remainingPathname: null,
127123
paramNames,
128124
paramValues: null
129125
}
130126
}
127+
} else {
128+
// If this matched at all, then the match was the entire pathname.
129+
remainingPathname = ''
131130
}
132131

133132
paramValues = match.slice(1).map(

0 commit comments

Comments
 (0)