Skip to content

Commit

Permalink
Combat the problem in issue #40
Browse files Browse the repository at this point in the history
  • Loading branch information
frzi committed Dec 17, 2021
1 parent b7600df commit 35bb55c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 5 additions & 3 deletions Sources/Route.swift
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,14 @@ final class PathMatcher: ObservableObject {
.replacingOccurrences(of: "^[^/]/$", with: "", options: .regularExpression) // Trailing slash.
.replacingOccurrences(of: #"\/?\*"#, with: "", options: .regularExpression) // Trailing asterisk.

for variable in variables {
for (index, variable) in variables.enumerated() {
let isAtRoot = index == 0 && glob.starts(with: "/:")
pattern = pattern.replacingOccurrences(
of: "/:" + variable,
with: "/(?<" + variable + ">[^/?]+)", // Named capture group.
of: (isAtRoot ? ":" : "/:") + variable,
with: "(?<\(variable)>" + (isAtRoot ? "" : "/") + "[^/?]+)", // Named capture group.
options: .regularExpression)
}

pattern = "^" +
(pattern.isEmpty ? "" : "(\(pattern))") +
(endsWithAsterisk ? "(/.*)?$" : "$")
Expand Down
4 changes: 4 additions & 0 deletions Tests/SwiftUIRouterTests/SwiftUIRouterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ final class SwiftUIRouterTests: XCTestCase {
("/news/latest", "/news/latest"),
("/user/:id/*", "/user/1"),
("/user/:id/*", "/user/1/settings"),
("/user/:id?", "/user"),
("/user/:id?", "/user/mark"),
("/user/:id/:group?", "/user/mark"),
("/user/:id/:group?", "/user/mark/admin"),
]

for (glob, path) in notNil {
Expand Down

0 comments on commit 35bb55c

Please sign in to comment.