implemented Handlers.editParam

This commit is contained in:
Jonathan Lamothe 2021-09-22 17:18:01 -04:00
parent bb3877cd3a
commit 34ac2a2a00
2 changed files with 49 additions and 1 deletions

View File

@ -27,7 +27,8 @@ module Helpers (
urlToText,
makeHttps,
editHost,
dropParam
dropParam,
editParam
) where
import qualified Data.List as L
@ -96,6 +97,27 @@ dropParam pName url = let
params' = filter (\p -> fst p /= pName) $ params url
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 text = case T.splitOn "://" text of
[protT, raw] -> do

View File

@ -36,6 +36,7 @@ spec = do
makeHttpsSpec
editHostSpec
dropParamSpec
editParamSpec
textToUrlSpec :: Spec
textToUrlSpec = describe "textToUrl" $ mapM_
@ -123,6 +124,31 @@ dropParamSpec = describe "dropParam" $ mapM_
withVal = simpleUrl { params = [("b", Nothing)] }
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 = "http://example.com/"