Module

Routing.Match

Package
purescript-routing
Repository
slamdata/purescript-routing

#eitherMatch Source

eitherMatch :: forall b a. Match (Either a b) -> Match b

if we match something that can fail then we have to match Either a b. This function converts matching on such sum to matching on right subpart. Matching on left branch fails. i.e.

data Sort = Asc | Desc
sortOfString :: String -> Either String Sort
sortOfString "asc" = Right Asc
sortOfString "desc" = Right Desc
sortOfString _ = Left "incorrect sort"

newtype Routing = Routing Sort
routes :: Match Routing
routes = (pure Routing) <*> (eitherMatch (sortOfString <$> var))

#list Source

list :: forall a. Match a -> Match (List a)

Matches list of matchers. Useful when argument can easy fail (not str) returns Match Nil if no matches

#nonempty Source

nonempty :: Match NonEmptyString

Matches a non-empty string.

#optionalMatch Source

optionalMatch :: forall a. Match a -> Match (Maybe a)

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")]))

#runMatch Source

runMatch :: forall a. Match a -> Route -> Either String a

Re-exports from Routing.Match.Class

#MatchClass Source

class (Alternative f) <= MatchClass f  where

Members

  • lit :: String -> f Unit

    lit x will match exactly the path component x. For example, lit "x" matches /x.

  • str :: f String

    str matches any path string component. For example, str matches /foo as "foo".

  • param :: String -> f String

    param p matches a parameter assignment q=v within a query block. For example, param "q" matches /?q=a&r=b as "a".

  • params :: f (Map String String)

    params matches an entire query block. For exmaple, params matches /?q=a&r=b as the map {q : "a", r : "b"}. Note that lit "foo" *> params does not match /foo, since a query component is required.

  • num :: f Number

    num matches any numerical path component.

  • int :: f Int

    int matches any integer path component.

  • bool :: f Boolean

    bool matches any boolean path component.

  • end :: f Unit

    end matches the end of a route.

  • fail :: forall a. String -> f a

#root Source

root :: forall f. MatchClass f => f Unit

Matches a leading slash.