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() {
@@ -28,12 +38,22 @@ hashes cb =
2838 where dropHash h = R .replace (R .regex " ^[^#]*#" R .noFlags) " " h
2939
3040
41+ -- | Stream of hash changed, callback called when new hash can be matched
42+ -- | First argument of callback is `Just a` when old hash can be matched
43+ -- | and `Nothing` when it can't.
3144matches :: 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
45+ matches = matches' decodeURIComponent
46+
47+ matches' :: forall e a . (String -> String ) ->
48+ Match a -> (Maybe a -> a -> Eff e Unit ) -> Eff e Unit
49+ matches' decoder routing cb = hashes $ \old new ->
50+ let mr = matchHash' decoder routing
3451 fst = either (const Nothing ) Just $ mr old
3552 in either (const $ pure unit) (cb fst) $ mr new
3653
3754
3855matchHash :: forall a . Match a -> String -> Either String a
39- matchHash matcher hash = runMatch matcher $ parse hash
56+ matchHash = matchHash' decodeURIComponent
57+
58+ matchHash' :: forall a . (String -> String ) -> Match a -> String -> Either String a
59+ matchHash' decoder matcher hash = runMatch matcher $ parse decoder hash
0 commit comments