Skip to content

Commit bc97e20

Browse files
authored
Merge pull request #39 from coot/url-parsing
fix #37
2 parents 106e930 + bbaea0e commit bc97e20

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/Routing/Parser.purs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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
105
import Control.MonadPlus (guard)
6+
import Data.Array as A
7+
import Data.Either (fromRight)
118
import Data.List (fromFoldable, List)
9+
import Data.Map as M
1210
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
1314
import Data.Traversable (traverse)
1415
import 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`)
3744
parse :: (String -> String) -> String -> Route
3845
parse decoder hash =
39-
map ( decoder >>> parsePart ) $ fromFoldable (S.split (S.Pattern "/") hash)
46+
map ( decoder >>> parsePart ) $ fromFoldable (R.split splitRegex hash)

test/Test/Main.purs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ routing =
3939
main :: Eff (console :: CONSOLE) Unit
4040
main = do
4141
print "Foo: " $ match routing "foo/12/?welp='hi'&b=false" -- foo
42+
print "Foo: " $ match routing "foo/12?welp='hi'&b=false" -- foo
4243
print "Quux: " $ match routing "/quux/42" -- quux
4344
print "Baz: " $ match routing "/123/" -- baz
4445
print "End: " $ match routing "/1" -- end

0 commit comments

Comments
 (0)