File tree Expand file tree Collapse file tree 5 files changed +75
-11
lines changed
Expand file tree Collapse file tree 5 files changed +75
-11
lines changed Original file line number Diff line number Diff 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
2936matchHash :: 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
Original file line number Diff line number Diff line change 1- module Routing where
1+ module Routing (
2+ hashChanged ,
3+ hashes ,
4+ matches ,
5+ matches' ,
6+ matchHash ,
7+ matchHash'
8+ ) where
29
310import Control.Monad.Eff
411import Data.Maybe
@@ -8,6 +15,9 @@ import qualified Data.String.Regex as R
815import Routing.Parser
916import Routing.Match
1017
18+
19+ foreign import decodeURIComponent :: String -> String
20+
1121foreign import hashChanged " " "
1222function hashChanged(handler) {
1323 return function() {
@@ -29,11 +39,18 @@ hashes cb =
2939
3040
3141matches :: 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
3852matchHash :: 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
Original file line number Diff line number Diff line change @@ -21,6 +21,8 @@ import Routing.Types
2121import Routing.Match.Class
2222import Routing.Match.Error
2323
24+ import Debug.Foreign
25+
2426newtype Match a = Match (Route -> V (Free MatchError ) (Tuple Route a ))
2527
2628instance matchMatchClass :: MatchClass Match where
@@ -96,6 +98,22 @@ instance matchApply :: Apply Match where
9698instance 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
101119runMatch :: forall a . Match a -> Route -> Either String a
Original file line number Diff line number Diff line change @@ -26,11 +26,12 @@ tryQuery source@(Path string) = fromMaybe source $ do
2626 Tuple <$> (A .head keyVal) <*> (keyVal A .!! 1 )
2727tryQuery 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
Original file line number Diff line number Diff line change @@ -5,19 +5,22 @@ import Debug.Trace
55import Control.Alt
66import Control.Apply
77import Debug.Foreign
8-
8+ import Data.List
99
1010import Routing
1111import Routing.Match
1212import Routing.Match.Class
1313
14- data FooBar = Foo Number | Bar Boolean String
14+ data FooBar = Foo Number | Bar Boolean String | Baz ( List Number )
1515
1616routing :: Match FooBar
1717routing =
1818 Foo <$> (lit " foo" *> num)
1919 <|>
2020 Bar <$> (lit " bar" *> bool) <*> (param " baz" )
21+ <|>
22+ Baz <$> (list num)
23+
2124
2225main = do
2326 fprint $ matchHash routing " food/asdf"
You can’t perform that action at this time.
0 commit comments