Skip to content

Commit aaa82cc

Browse files
committed
Merge pull request #7 from cryogenian/squashed
Squashed commit of the following:
2 parents 0a79435 + f98465e commit aaa82cc

File tree

6 files changed

+110
-78
lines changed

6 files changed

+110
-78
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ language: node_js
22
node_js:
33
- 0.10
44
env:
5-
- TAG=v0.6.8
5+
- TAG=v0.6.9.3
66
install:
77
- wget -O $HOME/purescript.tar.gz https://github.com/purescript/purescript/releases/download/$TAG/linux64.tar.gz
88
- sudo tar zxvf $HOME/purescript.tar.gz -C /usr/local/bin purescript/psc{,i,-docs,-make} --strip-components=1
99
- sudo chmod a+x /usr/local/bin/psc{,i,-docs,-make}
1010
- npm install bower gulp -g
1111
- npm install && bower install
1212
script:
13-
- gulp bundle-test
13+
- gulp bundle-dev

MODULES.md

Lines changed: 62 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,37 @@ matchHash :: forall a. Match a -> String -> Either String a
3131

3232

3333

34+
## Module Routing.Hash
35+
36+
#### `setHash`
37+
38+
``` purescript
39+
setHash :: forall e. String -> Eff (dom :: DOM | e) Unit
40+
```
41+
42+
43+
#### `getHash`
44+
45+
``` purescript
46+
getHash :: forall e. Eff (dom :: DOM | e) String
47+
```
48+
49+
50+
#### `modifyHash`
51+
52+
``` purescript
53+
modifyHash :: forall e. (String -> String) -> Eff (dom :: DOM | e) Unit
54+
```
55+
56+
57+
3458
## Module Routing.Match
3559

3660
#### `Match`
3761

3862
``` purescript
3963
newtype Match a
40-
= Match (Route -> Either String (Tuple Route a))
64+
= Match (Route -> V (Free MatchError) (Tuple Route a))
4165
```
4266

4367

@@ -90,35 +114,36 @@ instance matchApplicative :: Applicative Match
90114
```
91115

92116

93-
#### `matchBind`
94-
95-
``` purescript
96-
instance matchBind :: Bind Match
97-
```
98-
99-
100-
#### `matchMonad`
117+
#### `runMatch`
101118

102119
``` purescript
103-
instance matchMonad :: Monad Match
120+
runMatch :: forall a. Match a -> Route -> Either String a
104121
```
105122

106-
107-
#### `matchMonadPlus`
123+
#### `eitherMatch`
108124

109125
``` purescript
110-
instance matchMonadPlus :: MonadPlus Match
126+
eitherMatch :: forall a b. Match (Either a b) -> Match b
111127
```
112128

113-
114-
#### `runMatch`
115-
116-
``` purescript
117-
runMatch :: forall a. Match a -> Route -> Either String a
129+
if we match something that can fail then we have to
130+
match `Either a b`. This function converts matching on such
131+
sum to matching on right subpart. Matching on left branch fails.
132+
i.e.
133+
```purescript
134+
data Sort = Asc | Desc
135+
sortOfString :: String -> Either String Sort
136+
sortOfString "asc" = Right Asc
137+
sortOfString "desc" = Right Desc
138+
sortOfString _ = Left "incorrect sort"
139+
140+
newtype Routing = Routing Sort
141+
routes :: Match Routing
142+
routes = (pure Routing) <*> (eitherMatch (sortOfString <$> var))
143+
118144
```
119145

120146

121-
122147
## Module Routing.Parser
123148

124149
#### `parse`
@@ -129,33 +154,6 @@ parse :: String -> Route
129154

130155

131156

132-
## Module Routing.Setter
133-
134-
#### `setHash`
135-
136-
``` purescript
137-
setHash :: forall e. String -> Eff e Unit
138-
```
139-
140-
141-
#### `RouteState`
142-
143-
``` purescript
144-
class RouteState a where
145-
toHash :: a -> String
146-
```
147-
148-
Class of types that can be converted to hashes
149-
150-
#### `setRouteState`
151-
152-
``` purescript
153-
setRouteState :: forall r e. (RouteState r) => r -> Eff e Unit
154-
```
155-
156-
wrapper over `setHash` that uses `RouteState`
157-
158-
159157
## Module Routing.Types
160158

161159
#### `RoutePart`
@@ -170,7 +168,7 @@ data RoutePart
170168
#### `Route`
171169

172170
``` purescript
173-
type Route = [RoutePart]
171+
type Route = List RoutePart
174172
```
175173

176174

@@ -180,28 +178,38 @@ type Route = [RoutePart]
180178
#### `MatchClass`
181179

182180
``` purescript
183-
class (MonadPlus f) <= MatchClass f where
181+
class (Alternative f) <= MatchClass f where
184182
lit :: String -> f Unit
185-
var :: f String
183+
str :: f String
186184
param :: String -> f String
185+
num :: f Number
186+
bool :: f Boolean
187187
fail :: forall a. String -> f a
188188
```
189189

190190

191191

192-
## Module Routing.Match.Combinators
192+
## Module Routing.Match.Error
193193

194-
#### `num`
194+
#### `MatchError`
195195

196196
``` purescript
197-
num :: forall f. (MatchClass f) => String -> f Number
197+
data MatchError
198+
= UnexpectedPath String
199+
| ExpectedBoolean
200+
| ExpectedNumber
201+
| ExpectedString
202+
| ExpectedQuery
203+
| ExpectedPathPart
204+
| KeyNotFound String
205+
| Fail String
198206
```
199207

200208

201-
#### `bool`
209+
#### `showMatchError`
202210

203211
``` purescript
204-
bool :: forall f. (MatchClass f) => String -> f Boolean
212+
showMatchError :: MatchError -> String
205213
```
206214

207215

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"purescript-validation": "~0.1.1",
2828
"purescript-semirings": "~0.1.1",
2929
"purescript-lists": "~0.6.0",
30-
"purescript-globals": "~0.1.6"
30+
"purescript-globals": "~0.1.6",
31+
"purescript-dom": "~0.1.2"
3132
},
3233
"devDependencies": {
3334
"purescript-timers": "~0.0.8",

src/Routing/Hash.purs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module Routing.Hash where
2+
3+
import Control.Monad.Eff
4+
import DOM
5+
6+
foreign import setHash """
7+
function setHash(hash) {
8+
return function() {
9+
document.location.hash = hash;
10+
};
11+
}
12+
""" :: forall e. String -> Eff (dom :: DOM |e) Unit
13+
14+
foreign import getHash """
15+
function getHash() {
16+
return document.location.hash.replace(/^[^#]*#/g, "");
17+
}
18+
""" :: forall e. Eff (dom :: DOM |e) String
19+
20+
modifyHash :: forall e. (String -> String) -> Eff (dom :: DOM|e) Unit
21+
modifyHash fn = (fn <$> getHash) >>= setHash

src/Routing/Match.purs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,26 @@ runMatch (Match fn) route =
107107
pure $ foldl (\b a -> a <> ";" <> b) "" $ showMatchError <$> es
108108

109109

110-
110+
-- | if we match something that can fail then we have to
111+
-- | match `Either a b`. This function converts matching on such
112+
-- | sum to matching on right subpart. Matching on left branch fails.
113+
-- | i.e.
114+
-- | ```purescript
115+
-- | data Sort = Asc | Desc
116+
-- | sortOfString :: String -> Either String Sort
117+
-- | sortOfString "asc" = Right Asc
118+
-- | sortOfString "desc" = Right Desc
119+
-- | sortOfString _ = Left "incorrect sort"
120+
-- |
121+
-- | newtype Routing = Routing Sort
122+
-- | routes :: Match Routing
123+
-- | routes = (pure Routing) <*> (eitherMatch (sortOfString <$> var))
124+
-- |
125+
-- | ```
126+
eitherMatch :: forall a b. Match (Either a b) -> Match b
127+
eitherMatch (Match r2eab) = Match $ \r ->
128+
runV invalid runEither $ (r2eab r)
129+
where runEither (Tuple rs eit) =
130+
case eit of
131+
Left _ -> invalid $ free $ Fail "Nested check failed"
132+
Right res -> pure $ Tuple rs res

src/Routing/Setter.purs

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)