Skip to content

Commit bbaea0e

Browse files
author
Marcin Szamotulski
committed
Merge branch 'master' into url-parsing
2 parents 93317f0 + 106e930 commit bbaea0e

File tree

5 files changed

+20
-12
lines changed

5 files changed

+20
-12
lines changed

GUIDE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ A couple notes upfront:
55
* This library facilitates hash-based routing. If you're looking to do pushstate routing with the [history](https://developer.mozilla.org/en-US/docs/Web/API/History_API) object, then you are in the wrong place.
66
* Routes are declared using [applicative](https://pursuit.purescript.org/packages/purescript-prelude/0.1.4/docs/Prelude#t:Applicative) syntax. If you're not yet comfortable with applicatives, see this [chapter](http://learnyouahaskell.com/functors-applicative-functors-and-monoids#applicative-functors) or this [paper](http://www.staff.city.ac.uk/~ross/papers/Applicative.pdf).
77

8-
####Usage
8+
#### Usage
99

1010
First, define some locations:
1111

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"purescript-semirings": "^4.0.0",
3636
"purescript-tuples": "^4.0.0",
3737
"purescript-validation": "^3.0.0",
38-
"purescript-aff": "^3.0.0",
38+
"purescript-aff": "^4.0.0",
3939
"purescript-control": "^3.0.0",
4040
"purescript-console": "^3.0.0",
4141
"purescript-integers": "^3.0.0"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"private": true,
33
"scripts": {
44
"clean": "rimraf output && rimraf .pulp-cache",
5-
"build": "pulp build --censor-lib --strict",
5+
"build": "pulp build -- --censor-lib --strict",
66
"test": "pulp test"
77
},
88
"devDependencies": {

src/Routing.purs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,16 @@ module Routing (
99
matchesAff'
1010
) where
1111

12-
import Prelude (Unit, unit, pure, const, ($))
13-
import Control.Monad.Eff (Eff())
14-
import Control.Monad.Aff (Aff(), makeAff)
15-
import Data.Maybe (Maybe(..))
12+
import Control.Monad.Aff (Aff, makeAff, nonCanceler)
13+
import Control.Monad.Eff (Eff)
1614
import Data.Either (Either(..), either)
17-
import Data.Tuple (Tuple(..))
15+
import Data.Maybe (Maybe(..))
1816
import Data.String.Regex as R
1917
import Data.String.Regex.Flags as RF
20-
21-
import Routing.Parser (parse)
18+
import Data.Tuple (Tuple(..))
19+
import Prelude (Unit, const, pure, unit, ($), (<$))
2220
import Routing.Match (Match, runMatch)
21+
import Routing.Parser (parse)
2322

2423

2524
foreign import decodeURIComponent :: String -> String
@@ -53,9 +52,9 @@ matches' decoder routing cb = hashes $ \old new ->
5352
matchesAff' :: forall e a. (String -> String) ->
5453
Match a -> Aff e (Tuple (Maybe a) a)
5554
matchesAff' decoder routing =
56-
makeAff \_ k -> do
55+
makeAff \k -> nonCanceler <$
5756
matches' decoder routing \old new ->
58-
k $ Tuple old new
57+
k $ Right $ Tuple old new
5958

6059
matchesAff :: forall e a. Match a -> Aff e (Tuple (Maybe a) a)
6160
matchesAff = matchesAff' decodeURIComponent

src/Routing/Match.purs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,12 @@ eitherMatch (Match r2eab) = Match $ \r ->
176176
case eit of
177177
Left _ -> invalid $ free $ Fail "Nested check failed"
178178
Right res -> pure $ Tuple rs res
179+
180+
-- | useful for matching optional params at the end of a path
181+
-- | ```
182+
-- | optParams = maybe M.empty id <$> optionalMatch params <* end
183+
-- | runMatch (lit "path" *> optParams) (parse id "path/?a=1")
184+
-- | -- (Right (fromFoldable [(Tuple "a" "1")]))
185+
-- | ```
186+
optionalMatch :: forall a. Match a -> Match (Maybe a)
187+
optionalMatch (Match fn) = Match (\route -> unV (const $ pure (Tuple route Nothing)) (pure <<< map Just) $ fn route)

0 commit comments

Comments
 (0)