diff --git a/src/Helpers.hs b/src/Helpers.hs index ab0ccd7..78bf03b 100644 --- a/src/Helpers.hs +++ b/src/Helpers.hs @@ -25,6 +25,7 @@ along with this program. If not, see . module Helpers ( textToUrl, urlToText, + editHost ) where import qualified Data.List as L @@ -62,6 +63,17 @@ urlToText url = T.pack $ Just str -> "#" ++ str Nothing -> "" +-- | Edit the 'host' value of a 'Url' +editHost + :: (String -> Maybe String) + -- ^ the transformation function + -> Url + -- ^ the Url being modified + -> Maybe Url +editHost f url = do + host' <- f $ host url + Just url { host = host' } + subToUrl :: T.Text -> Maybe Url subToUrl text = case T.splitOn "://" text of [protT, raw] -> do diff --git a/test/HelpersSpec.hs b/test/HelpersSpec.hs index b2dbfa6..895fafb 100644 --- a/test/HelpersSpec.hs +++ b/test/HelpersSpec.hs @@ -33,6 +33,7 @@ spec :: Spec spec = do textToUrlSpec urlToTextSpec + editHostSpec textToUrlSpec :: Spec textToUrlSpec = describe "textToUrl" $ mapM_ @@ -64,6 +65,22 @@ urlToTextSpec = describe "urlToText" $ mapM_ , ( anchorUrl, anchorTxt ) ] +editHostSpec ::Spec +editHostSpec = describe "editHost" $ mapM_ + ( \(desc, f, url, expected) -> context desc $ + it ("should be " ++ show expected) $ + editHost f url `shouldBe` expected + ) + + -- description, function, url, expected + [ ( "reverse", Just . reverse, simpleUrl, reversed ) + , ( "fail", const Nothing, simpleUrl, Nothing ) + ] + + where + reversed = Just simpleUrl + { host = reverse $ host simpleUrl } + simpleTxt :: T.Text simpleTxt = "http://example.com/"