diff --git a/src/Helpers.hs b/src/Helpers.hs index 9f48516..a2c0c21 100644 --- a/src/Helpers.hs +++ b/src/Helpers.hs @@ -28,7 +28,8 @@ module Helpers ( makeHttps, editHost, dropParam, - editParam + editParam, + incStrBy ) where import qualified Data.List as L @@ -118,6 +119,18 @@ editParam pName f url = do ) $ params url Just url { params = params' } +-- | Increment a 'String' representation of an 'Integer' by a given +-- amount (if possible) +incStrBy + :: Integer + -- ^ the amount to increment by + -> String + -- ^ the 'String' to increment + -> Maybe String +incStrBy n str = case reads str of + [(m, "")] -> Just $ show $ n + m + _ -> Nothing + 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 23e59bb..97ed12b 100644 --- a/test/HelpersSpec.hs +++ b/test/HelpersSpec.hs @@ -37,6 +37,7 @@ spec = do editHostSpec dropParamSpec editParamSpec + incStrBySpec textToUrlSpec :: Spec textToUrlSpec = describe "textToUrl" $ mapM_ @@ -149,6 +150,20 @@ editParamSpec = describe "editParam" $ mapM_ , ( "c", Nothing ) ] +incStrBySpec :: Spec +incStrBySpec = describe "incStrBy" $ mapM_ + ( \(n, str, expected) -> + context (show str ++ " + " ++ show n) $ + it ("should be " ++ show expected) $ + incStrBy n str `shouldBe` expected + ) + + -- number, string, expected + [ ( 2, "3", Just "5" ) + , ( 2, "foo", Nothing ) + , ( 2, "", Nothing ) + ] + simpleTxt :: T.Text simpleTxt = "http://example.com/"