Skip to content

Commit 1c8b522

Browse files
authored
Merge pull request #38 from coot/optionalMatch
optionalMatch - matching optional path parts
2 parents ab4c9ed + 655865d commit 1c8b522

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/Routing/Match.purs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,12 @@ eitherMatch (Match r2eab) = Match $ \r ->
176176
case eit of
177177
Left _ -> invalid $ free $ Fail "Nested check failed"
178178
Right res -> pure $ Tuple rs res
179+
180+
-- | useful for matching optional params at the end of a path
181+
-- | ```
182+
-- | optParams = maybe M.empty id <$> optionalMatch params <* end
183+
-- | runMatch (lit "path" *> optParams) (parse id "path/?a=1")
184+
-- | -- (Right (fromFoldable [(Tuple "a" "1")]))
185+
-- | ```
186+
optionalMatch :: forall a. Match a -> Match (Maybe a)
187+
optionalMatch (Match fn) = Match (\route -> unV (const $ pure (Tuple route Nothing)) (pure <<< map Just) $ fn route)

0 commit comments

Comments
 (0)