implemented Handlers.editParam
This commit is contained in:
parent
bb3877cd3a
commit
34ac2a2a00
|
@ -27,7 +27,8 @@ module Helpers (
|
||||||
urlToText,
|
urlToText,
|
||||||
makeHttps,
|
makeHttps,
|
||||||
editHost,
|
editHost,
|
||||||
dropParam
|
dropParam,
|
||||||
|
editParam
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import qualified Data.List as L
|
import qualified Data.List as L
|
||||||
|
@ -96,6 +97,27 @@ dropParam pName url = let
|
||||||
params' = filter (\p -> fst p /= pName) $ params url
|
params' = filter (\p -> fst p /= pName) $ params url
|
||||||
in url { params = params' }
|
in url { params = params' }
|
||||||
|
|
||||||
|
-- | Edit a parameter
|
||||||
|
editParam
|
||||||
|
:: String
|
||||||
|
-- ^ the parameter name
|
||||||
|
-> (String -> Maybe String)
|
||||||
|
-- ^ the transformation function
|
||||||
|
-> Url
|
||||||
|
-- ^ the 'Url' being edited
|
||||||
|
-> Maybe Url
|
||||||
|
editParam pName f url = do
|
||||||
|
params' <- mapM
|
||||||
|
( \case
|
||||||
|
(name, Just val) -> if name == pName
|
||||||
|
then do
|
||||||
|
val' <- f val
|
||||||
|
Just (name, Just val')
|
||||||
|
else Just (name, Just val)
|
||||||
|
x -> Just x
|
||||||
|
) $ params url
|
||||||
|
Just url { params = params' }
|
||||||
|
|
||||||
subToUrl :: T.Text -> Maybe Url
|
subToUrl :: T.Text -> Maybe Url
|
||||||
subToUrl text = case T.splitOn "://" text of
|
subToUrl text = case T.splitOn "://" text of
|
||||||
[protT, raw] -> do
|
[protT, raw] -> do
|
||||||
|
|
|
@ -36,6 +36,7 @@ spec = do
|
||||||
makeHttpsSpec
|
makeHttpsSpec
|
||||||
editHostSpec
|
editHostSpec
|
||||||
dropParamSpec
|
dropParamSpec
|
||||||
|
editParamSpec
|
||||||
|
|
||||||
textToUrlSpec :: Spec
|
textToUrlSpec :: Spec
|
||||||
textToUrlSpec = describe "textToUrl" $ mapM_
|
textToUrlSpec = describe "textToUrl" $ mapM_
|
||||||
|
@ -123,6 +124,31 @@ dropParamSpec = describe "dropParam" $ mapM_
|
||||||
withVal = simpleUrl { params = [("b", Nothing)] }
|
withVal = simpleUrl { params = [("b", Nothing)] }
|
||||||
withoutVal = simpleUrl { params = [("a", Just "1")] }
|
withoutVal = simpleUrl { params = [("a", Just "1")] }
|
||||||
|
|
||||||
|
editParamSpec :: Spec
|
||||||
|
editParamSpec = describe "editParam" $ mapM_
|
||||||
|
( \(desc, pName, f, expected) -> context desc $
|
||||||
|
it ("should be " ++ show expected) $
|
||||||
|
editParam pName f url `shouldBe` expected
|
||||||
|
)
|
||||||
|
|
||||||
|
-- description, param name, function, expected
|
||||||
|
[ ( "reverse", "a", Just . reverse, Just reversed )
|
||||||
|
, ( "fail", "a", const Nothing, Nothing )
|
||||||
|
, ( "reverse empty", "c", Just . reverse, Just url )
|
||||||
|
, ( "fail empty", "c", const Nothing, Just url )
|
||||||
|
, ( "no reverse", "d", Just . reverse, Just url )
|
||||||
|
, ( "no fail", "d", const Nothing, Just url )
|
||||||
|
]
|
||||||
|
|
||||||
|
where
|
||||||
|
url = simpleUrl { params = mkParams "foo" }
|
||||||
|
reversed = simpleUrl { params = mkParams "oof" }
|
||||||
|
mkParams val =
|
||||||
|
[ ( "a", Just val )
|
||||||
|
, ( "b", Just "bar" )
|
||||||
|
, ( "c", Nothing )
|
||||||
|
]
|
||||||
|
|
||||||
simpleTxt :: T.Text
|
simpleTxt :: T.Text
|
||||||
simpleTxt = "http://example.com/"
|
simpleTxt = "http://example.com/"
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user