Skip to content

Commit 8e1d059

Browse files
committed
Squashed commit of the following:
commit dbc480a Author: Maxim Zimaliev <zimaliev@yandex.ru> Date: Wed Apr 8 18:22:42 2015 +0300 list combinator commit 66d9eb9 Author: Maxim Zimaliev <zimaliev@yandex.ru> Date: Wed Apr 8 15:05:22 2015 +0300 added matches' string with explicit decoder commit 3f0f5ad Author: Maxim Zimaliev <zimaliev@yandex.ru> Date: Wed Apr 8 15:05:00 2015 +0300 added matches' string with explicit decoder commit 6460b9f Merge: aaa82cc 54a5102 Author: Maxim Zimaliev <zimaliev@yandex.ru> Date: Wed Apr 8 14:44:28 2015 +0300 Merge branch 'squashed' commit 54a5102 Author: Maxim Zimaliev <zimaliev@yandex.ru> Date: Wed Apr 8 14:43:35 2015 +0300 optional decoder for uri support
1 parent aaa82cc commit 8e1d059

File tree

5 files changed

+75
-11
lines changed

5 files changed

+75
-11
lines changed

MODULES.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,27 @@ matches :: forall e a. Match a -> (Maybe a -> a -> Eff e Unit) -> Eff e Unit
2323
```
2424

2525

26+
#### `matches'`
27+
28+
``` purescript
29+
matches' :: forall e a. (String -> String) -> Match a -> (Maybe a -> a -> Eff e Unit) -> Eff e Unit
30+
```
31+
32+
2633
#### `matchHash`
2734

2835
``` purescript
2936
matchHash :: forall a. Match a -> String -> Either String a
3037
```
3138

3239

40+
#### `matchHash'`
41+
42+
``` purescript
43+
matchHash' :: forall a. (String -> String) -> Match a -> String -> Either String a
44+
```
45+
46+
3347

3448
## Module Routing.Hash
3549

@@ -114,6 +128,15 @@ instance matchApplicative :: Applicative Match
114128
```
115129

116130

131+
#### `list`
132+
133+
``` purescript
134+
list :: forall a. Match a -> Match (List a)
135+
```
136+
137+
Matches list of matchers. Useful when argument can easy fail (not `str`)
138+
returns `Match Nil` if no matches
139+
117140
#### `runMatch`
118141

119142
``` purescript
@@ -149,9 +172,11 @@ routes = (pure Routing) <*> (eitherMatch (sortOfString <$> var))
149172
#### `parse`
150173

151174
``` purescript
152-
parse :: String -> Route
175+
parse :: (String -> String) -> String -> Route
153176
```
154177

178+
Parse hash string to `Route` with `decoder` function
179+
applied to every hash part (usually `decodeURIComponent`)
155180

156181

157182
## Module Routing.Types

src/Routing.purs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
module Routing where
1+
module Routing (
2+
hashChanged,
3+
hashes,
4+
matches,
5+
matches',
6+
matchHash,
7+
matchHash'
8+
) where
29

310
import Control.Monad.Eff
411
import Data.Maybe
@@ -8,6 +15,9 @@ import qualified Data.String.Regex as R
815
import Routing.Parser
916
import Routing.Match
1017

18+
19+
foreign import decodeURIComponent :: String -> String
20+
1121
foreign import hashChanged """
1222
function hashChanged(handler) {
1323
return function() {
@@ -29,11 +39,18 @@ hashes cb =
2939

3040

3141
matches :: forall e a. Match a -> (Maybe a -> a -> Eff e Unit) -> Eff e Unit
32-
matches routing cb = hashes $ \old new ->
33-
let mr = matchHash routing
42+
matches = matches' decodeURIComponent
43+
44+
matches' :: forall e a. (String -> String) ->
45+
Match a -> (Maybe a -> a -> Eff e Unit) -> Eff e Unit
46+
matches' decoder routing cb = hashes $ \old new ->
47+
let mr = matchHash' decoder routing
3448
fst = either (const Nothing) Just $ mr old
3549
in either (const $ pure unit) (cb fst) $ mr new
3650

3751

3852
matchHash :: forall a. Match a -> String -> Either String a
39-
matchHash matcher hash = runMatch matcher $ parse hash
53+
matchHash = matchHash' decodeURIComponent
54+
55+
matchHash' :: forall a. (String -> String) -> Match a -> String -> Either String a
56+
matchHash' decoder matcher hash = runMatch matcher $ parse decoder hash

src/Routing/Match.purs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import Routing.Types
2121
import Routing.Match.Class
2222
import Routing.Match.Error
2323

24+
import Debug.Foreign
25+
2426
newtype Match a = Match (Route -> V (Free MatchError) (Tuple Route a))
2527

2628
instance matchMatchClass :: MatchClass Match where
@@ -96,6 +98,22 @@ instance matchApply :: Apply Match where
9698
instance matchApplicative :: Applicative Match where
9799
pure a = Match \r -> pure $ Tuple r a
98100

101+
102+
-- | Matches list of matchers. Useful when argument can easy fail (not `str`)
103+
-- | returns `Match Nil` if no matches
104+
list :: forall a. Match a -> Match (List a)
105+
list (Match r2a) =
106+
Match $ go Nil
107+
where go :: List a -> Route -> V (Free MatchError) (Tuple Route (List a))
108+
go accum r =
109+
runV
110+
(const $ pure (Tuple r (reverse accum)))
111+
(\(Tuple rs a) -> go (Cons a accum) rs)
112+
(r2a r)
113+
114+
115+
116+
99117
-- It groups `Free MatchError` -> [[MatchError]] -map with showMatchError ->
100118
-- [[String]] -fold with semicolon-> [String] -fold with newline-> String
101119
runMatch :: forall a. Match a -> Route -> Either String a

src/Routing/Parser.purs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ tryQuery source@(Path string) = fromMaybe source $ do
2626
Tuple <$> (A.head keyVal) <*> (keyVal A.!! 1)
2727
tryQuery q = q
2828

29-
foreign import decodeURIComponent :: String -> String
3029

31-
parse :: String -> Route
32-
parse hash = tryQuery <$>
30+
-- | Parse hash string to `Route` with `decoder` function
31+
-- | applied to every hash part (usually `decodeURIComponent`)
32+
parse :: (String -> String) -> String -> Route
33+
parse decoder hash = tryQuery <$>
3334
Path <$>
34-
decodeURIComponent <$>
35+
decoder <$>
3536
fromArray (S.split "/" hash)
3637

test/Main.purs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,22 @@ import Debug.Trace
55
import Control.Alt
66
import Control.Apply
77
import Debug.Foreign
8-
8+
import Data.List
99

1010
import Routing
1111
import Routing.Match
1212
import Routing.Match.Class
1313

14-
data FooBar = Foo Number | Bar Boolean String
14+
data FooBar = Foo Number | Bar Boolean String | Baz (List Number)
1515

1616
routing :: Match FooBar
1717
routing =
1818
Foo <$> (lit "foo" *> num)
1919
<|>
2020
Bar <$> (lit "bar" *> bool) <*> (param "baz")
21+
<|>
22+
Baz <$> (list num)
23+
2124

2225
main = do
2326
fprint $ matchHash routing "food/asdf"

0 commit comments

Comments
 (0)