@@ -2,16 +2,20 @@ module Routing.Parser (
2
2
parse
3
3
) where
4
4
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
10
5
import Control.MonadPlus (guard )
6
+ import Data.Array as A
7
+ import Data.Either (fromRight )
11
8
import Data.List (fromFoldable , List )
9
+ import Data.Map as M
12
10
import 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
13
14
import Data.Traversable (traverse )
14
15
import Data.Tuple (Tuple (..))
16
+ import Partial.Unsafe (unsafePartial )
17
+ import Prelude (map , discard , (>>>), ($), (<<<), (==), (<*>), (<$>), (<=))
18
+ import Routing.Types (Route , RoutePart (..))
15
19
16
20
-- | Parse part of hash. Will return `Query (Map String String)` for query
17
21
-- | i.e. `"?foo=bar&bar=baz"` -->
@@ -32,8 +36,11 @@ parsePart str = fromMaybe (Path str) do
32
36
Tuple <$> (A .head keyVal) <*> (keyVal A .!! 1 )
33
37
34
38
39
+ splitRegex :: R.Regex
40
+ splitRegex = unsafePartial fromRight $ R .regex " \\ /|(?=\\ ?)" R .noFlags
41
+
35
42
-- | Parse hash string to `Route` with `decoder` function
36
43
-- | applied to every hash part (usually `decodeURIComponent`)
37
44
parse :: (String -> String ) -> String -> Route
38
45
parse decoder hash =
39
- map ( decoder >>> parsePart ) $ fromFoldable (S .split ( S.Pattern " / " ) hash)
46
+ map ( decoder >>> parsePart ) $ fromFoldable (R .split splitRegex hash)
0 commit comments