From 655865d883908b7a1a80385d064b93394ddca11d Mon Sep 17 00:00:00 2001 From: Marcin Szamotulski Date: Sat, 6 May 2017 19:41:08 +0200 Subject: [PATCH] optionalMatch - matching optional path parts --- src/Routing/Match.purs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Routing/Match.purs b/src/Routing/Match.purs index 8c4736e..7b74992 100644 --- a/src/Routing/Match.purs +++ b/src/Routing/Match.purs @@ -176,3 +176,12 @@ eitherMatch (Match r2eab) = Match $ \r -> case eit of Left _ -> invalid $ free $ Fail "Nested check failed" Right res -> pure $ Tuple rs res + +-- | useful for matching optional params at the end of a path +-- | ``` +-- | optParams = maybe M.empty id <$> optionalMatch params <* end +-- | runMatch (lit "path" *> optParams) (parse id "path/?a=1") +-- | -- (Right (fromFoldable [(Tuple "a" "1")])) +-- | ``` +optionalMatch :: forall a. Match a -> Match (Maybe a) +optionalMatch (Match fn) = Match (\route -> unV (const $ pure (Tuple route Nothing)) (pure <<< map Just) $ fn route)