Skip to content

Commit 967af5a

Browse files
authored
Merge pull request #31 from menelaos/match-int
Add support for matching integer path components
2 parents 5bbf94d + 35f7fa1 commit 967af5a

File tree

5 files changed

+27
-4
lines changed

5 files changed

+27
-4
lines changed

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
"purescript-validation": "^2.0.0",
3838
"purescript-aff": "^2.0.0",
3939
"purescript-control": "^2.0.0",
40-
"purescript-console": "^2.0.0"
40+
"purescript-console": "^2.0.0",
41+
"purescript-integers": "^2.1.0"
4142
},
4243
"devDependencies": {
4344
"purescript-console": "^2.0.0"

src/Routing/Match.purs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Control.Plus (class Plus)
88

99
import Data.Either (Either(..))
1010
import Data.Foldable (foldl)
11+
import Data.Int (fromString)
1112
import Data.List (List(..), reverse)
1213
import Data.Map as M
1314
import Data.Maybe (Maybe(..))
@@ -50,6 +51,14 @@ instance matchMatchClass :: MatchClass Match where
5051
_ ->
5152
invalid $ free ExpectedNumber
5253

54+
int = Match \route ->
55+
case route of
56+
Cons (Path input) rs -> case fromString input of
57+
Nothing -> invalid $ free ExpectedInt
58+
Just res -> pure $ Tuple rs res
59+
_ ->
60+
invalid $ free ExpectedInt
61+
5362
bool = Match \route ->
5463
case route of
5564
Cons (Path input) rs | input == "true" ->

src/Routing/Match/Class.purs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class (Alternative f) <= MatchClass f where
2626
-- | `num` matches any numerical path component.
2727
num :: f Number
2828

29+
-- | `int` matches any integer path component.
30+
int :: f Int
31+
2932
-- | `bool` matches any boolean path component.
3033
bool :: f Boolean
3134

src/Routing/Match/Error.purs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ data MatchError
99
| ExpectedBoolean
1010
-- expected numeric literal
1111
| ExpectedNumber
12+
-- expected integer literal
13+
| ExpectedInt
1214
-- expected string literal (found query probably or eol)
1315
| ExpectedString
1416
-- expected query found path part or eol
@@ -27,6 +29,7 @@ showMatchError err =
2729
KeyNotFound str -> "key: " <> str <> " has not found in query part"
2830
ExpectedQuery -> "expected query - found path"
2931
ExpectedNumber -> "expected number"
32+
ExpectedInt -> "expected int"
3033
ExpectedBoolean -> "expected boolean"
3134
ExpectedString -> "expected string var"
3235
ExpectedPathPart -> "expected path part, found query"

test/Test/Main.purs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Test.Main where
22

3-
import Prelude (class Show, Unit, show, ($), (<$>), (*>), (<*>), (<>))
3+
import Prelude (class Show, Unit, bind, show, ($), (<$>), (*>), (<*>), (<>))
44
import Control.Monad.Eff (Eff)
55
import Control.Monad.Eff.Console (CONSOLE(), logShow)
66
import Control.Alt ((<|>))
@@ -10,25 +10,32 @@ import Data.Map as M
1010

1111
import Routing (matchHash)
1212
import Routing.Match (Match, list)
13-
import Routing.Match.Class (num, param, bool, lit, params)
13+
import Routing.Match.Class (num, int, param, bool, lit, params)
1414

15-
data FooBar = Foo Number (M.Map String String) | Bar Boolean String | Baz (List Number)
15+
data FooBar
16+
= Foo Number (M.Map String String)
17+
| Bar Boolean String
18+
| Baz (List Number)
19+
| Quux Int
1620

1721
instance showFooBar :: Show FooBar where
1822
show (Foo num q) = "(Foo " <> show num <> " " <> show q <> ")"
1923
show (Bar bool str) = "(Bar " <> show bool <> " " <> show str <> ")"
2024
show (Baz lst) = "(Baz " <> show lst <> ")"
25+
show (Quux i) = "(Quux " <> show i <> ")"
2126

2227
routing :: Match FooBar
2328
routing =
2429
Foo <$> (lit "foo" *> num) <*> params
2530
<|> Bar <$> (lit "bar" *> bool) <*> (param "baz")
31+
<|> Quux <$> (lit "" *> lit "quux" *> int)
2632
<|> Baz <$> (list num)
2733

2834

2935
main :: Eff (console :: CONSOLE) Unit
3036
main = do
3137
logShow $ matchHash routing "foo/12/?welp='hi'&b=false"
38+
logShow $ matchHash routing "/quux/42"
3239

3340
-- (minimal test for browser)
3441

0 commit comments

Comments
 (0)