Skip to content

Commit

Permalink
Merge pull request #39 from coot/url-parsing
Browse files Browse the repository at this point in the history
fix #37
  • Loading branch information
garyb committed Sep 23, 2017
2 parents 106e930 + bbaea0e commit bc97e20
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/Routing/Parser.purs
Expand Up @@ -2,16 +2,20 @@ module Routing.Parser (
parse
) where

import Prelude (map, discard, (>>>), ($), (<<<), (==), (<*>), (<$>), (<=))
import Routing.Types (Route, RoutePart(..))
import Data.Array as A
import Data.Map as M
import Data.String as S
import Control.MonadPlus (guard)
import Data.Array as A
import Data.Either (fromRight)
import Data.List (fromFoldable, List)
import Data.Map as M
import Data.Maybe (Maybe, fromMaybe)
import Data.String as S
import Data.String.Regex (Regex, regex, split) as R
import Data.String.Regex.Flags (noFlags) as R
import Data.Traversable (traverse)
import Data.Tuple (Tuple(..))
import Partial.Unsafe (unsafePartial)
import Prelude (map, discard, (>>>), ($), (<<<), (==), (<*>), (<$>), (<=))
import Routing.Types (Route, RoutePart(..))

-- | Parse part of hash. Will return `Query (Map String String)` for query
-- | i.e. `"?foo=bar&bar=baz"` -->
Expand All @@ -32,8 +36,11 @@ parsePart str = fromMaybe (Path str) do
Tuple <$> (A.head keyVal) <*> (keyVal A.!! 1)


splitRegex :: R.Regex
splitRegex = unsafePartial fromRight $ R.regex "\\/|(?=\\?)" R.noFlags

-- | Parse hash string to `Route` with `decoder` function
-- | applied to every hash part (usually `decodeURIComponent`)
parse :: (String -> String) -> String -> Route
parse decoder hash =
map ( decoder >>> parsePart ) $ fromFoldable (S.split (S.Pattern "/") hash)
map ( decoder >>> parsePart ) $ fromFoldable (R.split splitRegex hash)
1 change: 1 addition & 0 deletions test/Test/Main.purs
Expand Up @@ -39,6 +39,7 @@ routing =
main :: Eff (console :: CONSOLE) Unit
main = do
print "Foo: " $ match routing "foo/12/?welp='hi'&b=false" -- foo
print "Foo: " $ match routing "foo/12?welp='hi'&b=false" -- foo
print "Quux: " $ match routing "/quux/42" -- quux
print "Baz: " $ match routing "/123/" -- baz
print "End: " $ match routing "/1" -- end
Expand Down

0 comments on commit bc97e20

Please sign in to comment.