@@ -2,16 +2,20 @@ module Routing.Parser (
22 parse
33 ) where
44
5- import Prelude (map , discard , (>>>), ($), (<<<), (==), (<*>), (<$>), (<=))
6- import Routing.Types (Route , RoutePart (..))
7- import Data.Array as A
8- import Data.Map as M
9- import Data.String as S
105import Control.MonadPlus (guard )
6+ import Data.Array as A
7+ import Data.Either (fromRight )
118import Data.List (fromFoldable , List )
9+ import Data.Map as M
1210import Data.Maybe (Maybe , fromMaybe )
11+ import Data.String as S
12+ import Data.String.Regex (Regex , regex , split ) as R
13+ import Data.String.Regex.Flags (noFlags ) as R
1314import Data.Traversable (traverse )
1415import Data.Tuple (Tuple (..))
16+ import Partial.Unsafe (unsafePartial )
17+ import Prelude (map , discard , (>>>), ($), (<<<), (==), (<*>), (<$>), (<=))
18+ import Routing.Types (Route , RoutePart (..))
1519
1620-- | Parse part of hash. Will return `Query (Map String String)` for query
1721-- | i.e. `"?foo=bar&bar=baz"` -->
@@ -32,8 +36,11 @@ parsePart str = fromMaybe (Path str) do
3236 Tuple <$> (A .head keyVal) <*> (keyVal A .!! 1 )
3337
3438
39+ splitRegex :: R.Regex
40+ splitRegex = unsafePartial fromRight $ R .regex " \\ /|(?=\\ ?)" R .noFlags
41+
3542-- | Parse hash string to `Route` with `decoder` function
3643-- | applied to every hash part (usually `decodeURIComponent`)
3744parse :: (String -> String ) -> String -> Route
3845parse decoder hash =
39- map ( decoder >>> parsePart ) $ fromFoldable (S .split ( S.Pattern " / " ) hash)
46+ map ( decoder >>> parsePart ) $ fromFoldable (R .split splitRegex hash)
0 commit comments