implemented Helpers.editHost
This commit is contained in:
parent
34ee4a57bc
commit
b7737766f6
|
@ -25,6 +25,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
module Helpers (
|
module Helpers (
|
||||||
textToUrl,
|
textToUrl,
|
||||||
urlToText,
|
urlToText,
|
||||||
|
editHost
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import qualified Data.List as L
|
import qualified Data.List as L
|
||||||
|
@ -62,6 +63,17 @@ urlToText url = T.pack $
|
||||||
Just str -> "#" ++ str
|
Just str -> "#" ++ str
|
||||||
Nothing -> ""
|
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 :: T.Text -> Maybe Url
|
||||||
subToUrl text = case T.splitOn "://" text of
|
subToUrl text = case T.splitOn "://" text of
|
||||||
[protT, raw] -> do
|
[protT, raw] -> do
|
||||||
|
|
|
@ -33,6 +33,7 @@ spec :: Spec
|
||||||
spec = do
|
spec = do
|
||||||
textToUrlSpec
|
textToUrlSpec
|
||||||
urlToTextSpec
|
urlToTextSpec
|
||||||
|
editHostSpec
|
||||||
|
|
||||||
textToUrlSpec :: Spec
|
textToUrlSpec :: Spec
|
||||||
textToUrlSpec = describe "textToUrl" $ mapM_
|
textToUrlSpec = describe "textToUrl" $ mapM_
|
||||||
|
@ -64,6 +65,22 @@ urlToTextSpec = describe "urlToText" $ mapM_
|
||||||
, ( anchorUrl, anchorTxt )
|
, ( 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 :: T.Text
|
||||||
simpleTxt = "http://example.com/"
|
simpleTxt = "http://example.com/"
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user