Skip to content

Commit

Permalink
Merge pull request #31 from rgrempel/28-update-erl-dependency
Browse files Browse the repository at this point in the history
Update to version 13 of the sporto/erl dependency
  • Loading branch information
rgrempel authored Sep 11, 2017
2 parents 19fb507 + 154383c commit d523cf7
Show file tree
Hide file tree
Showing 10 changed files with 290 additions and 44 deletions.
2 changes: 1 addition & 1 deletion elm-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"elm-lang/html": "2.0.0 <= v < 3.0.0",
"elm-lang/http": "1.0.0 <= v < 2.0.0",
"elm-lang/navigation": "2.0.0 <= v < 3.0.0",
"sporto/erl": "10.0.2 <= v < 11.0.0"
"sporto/erl": "11.0.0 <= v < 14.0.0"
},
"elm-version": "0.18.0 <= v < 0.19.0"
}
10 changes: 5 additions & 5 deletions examples/elm-architecture-tutorial/Example2/CounterPair.elm
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ builder2messages builder =
let
left =
getQuery "top" builder
|> Maybe.map ((List.map Top) << Counter.fragment2messages)
|> List.concatMap Counter.fragment2messages
|> List.map Top

right =
getQuery "bottom" builder
|> Maybe.map ((List.map Bottom) << Counter.fragment2messages)
|> List.concatMap Counter.fragment2messages
|> List.map Bottom
in
[ left, right ]
|> List.filterMap identity
|> List.concat
List.append left right
12 changes: 3 additions & 9 deletions examples/elm-architecture-tutorial/Example8/SpinSquarePair.elm
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,10 @@ builder2messages builder =
let
left =
getQuery "left" builder
|> List.filterMap (Maybe.map Left << SpinSquare.location2action)

right =
getQuery "right" builder
|> List.filterMap (Maybe.map Right << SpinSquare.location2action)
in
case ( left, right ) of
( Just l, Just r ) ->
List.filterMap identity
[ Maybe.map Left <| SpinSquare.location2action l
, Maybe.map Right <| SpinSquare.location2action r
]

_ ->
[]
List.append left right
2 changes: 1 addition & 1 deletion examples/elm-architecture-tutorial/elm-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"elm-lang/http": "1.0.0 <= v < 2.0.0",
"elm-lang/navigation": "2.0.0 <= v < 3.0.0",
"elm-lang/svg": "2.0.0 <= v < 3.0.0",
"sporto/erl": "10.0.2 <= v < 11.0.0"
"sporto/erl": "13.0.0 <= v < 14.0.0"
},
"elm-version": "0.18.0 <= v < 0.19.0"
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
},
"dependencies": {},
"devDependencies": {
"elm": "^0.18.0"
"elm": "^0.18.0",
"elm-test": "^0.18.9"
},
"scripts": {
"test": "cd examples/elm-architecture-tutorial && sh compile.sh"
"test": "sh ./run-tests.sh"
},
"repository": {
"type": "git",
Expand Down
4 changes: 4 additions & 0 deletions run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#! /bin/sh

(cd examples/elm-architecture-tutorial && sh compile.sh)
elm-test
3 changes: 2 additions & 1 deletion src/RouteUrl.elm
Original file line number Diff line number Diff line change
Expand Up @@ -584,10 +584,11 @@ the things we care about).
-}
eqUrl : Url -> Url -> Bool
eqUrl u1 u2 =
-- The queries are `List (String, String)`, so `==` should be OK
(u1.path == u2.path)
&& (u1.hasTrailingSlash == u2.hasTrailingSlash)
&& (u1.hash == u2.hash)
&& (Dict.toList u1.query == Dict.toList u2.query)
&& (u1.query == u2.query)


checkDistinctUrl : Url -> UrlChange -> Maybe UrlChange
Expand Down
95 changes: 70 additions & 25 deletions src/RouteUrl/Builder.elm
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module RouteUrl.Builder
, query
, modifyQuery
, insertQuery
, updateQuery
, addQuery
, removeQuery
, getQuery
, replaceQuery
Expand Down Expand Up @@ -68,7 +68,7 @@ will be done for you.
# Manipulating the query
@docs query, modifyQuery, insertQuery, updateQuery, removeQuery, getQuery, replaceQuery
@docs query, modifyQuery, insertQuery, addQuery, removeQuery, getQuery, replaceQuery
# Manipulating the hash
Expand Down Expand Up @@ -104,7 +104,7 @@ type Builder
= Builder
{ entry : HistoryEntry
, path : List String
, query : Dict String String
, query : List ( String, String )
, hash : String
}

Expand All @@ -124,7 +124,7 @@ builder =
Builder
{ entry = NewEntry
, path = []
, query = Dict.empty
, query = []
, hash = ""
}

Expand Down Expand Up @@ -200,53 +200,98 @@ replacePath list (Builder builder) =
-- QUERY


{-| The query portion of the URL. It is represented by a `Dict` of
{-| The query portion of the URL. It is represented by a `List` of
key/value pairs.
-}
query : Builder -> Dict String String
query : Builder -> List ( String, String )
query (Builder builder) =
builder.query


{-| Replace the query with the result of a function that acts on the current query.
-}
modifyQuery : (Dict String String -> Dict String String) -> Builder -> Builder
modifyQuery : (List ( String, String ) -> List ( String, String )) -> Builder -> Builder
modifyQuery func (Builder builder) =
Builder { builder | query = func builder.query }


{-| Insert a key/value pair into the query. Replaces a key with the same name,
{-| Insert a key/value pair into the query. Replaces keys with the same name,
in case of collision.
-}
insertQuery : String -> String -> Builder -> Builder
insertQuery key value =
modifyQuery (Dict.insert key value)
insertQuery newKey newValue =
modifyQuery
(\query ->
query
|> List.foldl
(\( oldKey, oldValue ) ( acc, replaced ) ->
if newKey == oldKey then
-- If it's the key we're replacing, then see if
-- we've already done it.
if replaced then
-- If so, we just drop the old one ... the new
-- one has already been inserted
( acc, replaced )
else
-- If not, we insert the new one instead of the
-- old one, and remember that we've done it.
( ( newKey, newValue ) :: acc
, True
)
else
-- If it's some other key, just pass it through
( ( oldKey, oldValue ) :: acc
, replaced
)
)
( [], False )
|> \( reversedList, replaced ) ->
-- Since we did a `foldl`, and then a bunch of `::`, the list
-- was reversed. So, check whether we still need to add our
-- new key, and then un-reverse. (This helps us put the new
-- key at the end, if it didn't exist before).
if replaced then
List.reverse reversedList
else
List.reverse <|
( newKey, newValue )
:: reversedList
)


{-| Update a particular query key using the given function.
{-| Add a key/value pair into the query. Does not replace a key with the same name ...
just adds another value.
-}
updateQuery : String -> (Maybe String -> Maybe String) -> Builder -> Builder
updateQuery key func =
modifyQuery (Dict.update key func)
addQuery : String -> String -> Builder -> Builder
addQuery key value =
modifyQuery (\query -> List.reverse (( key, value ) :: List.reverse query))


{-| Remove a query key.
-}
removeQuery : String -> Builder -> Builder
removeQuery =
modifyQuery << Dict.remove
removeQuery key =
modifyQuery (List.filter (\( k, _ ) -> k /= key))


{-| Get the value for a query key.
{-| Get the values for a query key (can return multiple values if the key
is given more than once in the query).
-}
getQuery : String -> Builder -> Maybe String
getQuery : String -> Builder -> List String
getQuery key (Builder builder) =
Dict.get key builder.query
builder.query
|> List.filterMap
(\( k, v ) ->
if k == key then
Just v
else
Nothing
)


{-| Replace the whole query with a different dictionary.
{-| Replace the whole query with a different list of key/value pairs.
-}
replaceQuery : Dict String String -> Builder -> Builder
replaceQuery : List ( String, String ) -> Builder -> Builder
replaceQuery query (Builder builder) =
Builder { builder | query = query }

Expand Down Expand Up @@ -299,13 +344,13 @@ toChange stuffIntoHash (Builder builder) =
String.join "/" (List.map encodeUri builder.path)

joinedQuery =
if Dict.isEmpty builder.query then
if List.isEmpty builder.query then
""
else
queryPrefix ++ String.join "&" (Dict.foldl eachQuery [] builder.query)
queryPrefix ++ String.join "&" (List.map eachQuery builder.query)

eachQuery key value memo =
(encodeUri key ++ "=" ++ encodeUri value) :: memo
eachQuery ( key, value ) =
encodeUri key ++ "=" ++ encodeUri value

hashPrefix =
if stuffIntoHash then
Expand Down
Loading

0 comments on commit d523cf7

Please sign in to comment.