Skip to content

fix #37 #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/Routing/Parser.purs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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