>
> module Frame.Utilities (
>
> explode,
> explodeURL,
> explodeFieldName,
> implode,
> implodeUrl,
> humanise,
> humaniseCamel,
> humaniseUrl,
> humanisePath,
>
> maybeMaybe,
> headMaybe,
> pop,
> appMaybe,
> allNothing,
>
> (?),
> (??),
> module Data.Map,
> module Data.Maybe
> ) where
> import Data.Map (Map, empty, fromList)
> import Data.Maybe
> import Data.Char
>
> explode :: Char
> -> String
> -> [String]
> explode c s = case dropWhile (==c) s of
> "" -> []
> s' -> w : explode c s''
> where (w, s'') = break (==c) s'
>
> explodeURL :: String -> [String]
> explodeURL = explode '/'
>
> explodeFieldName :: String
> -> (Int, [String])
> explodeFieldName fn = let fns = explode '.' fn in
> (length fns, fns)
>
> implode :: Char -> [String] -> String
> implode _ [] = ""
> implode _ [w] = w
> implode c (w:ws) = w ++ c : implode c ws
>
> implodeUrl :: [String] -> String
> implodeUrl ws = implode '/' ws
>
> maybeMaybe :: Maybe (Maybe a) -> Maybe a
> maybeMaybe (Just ma) = ma
> maybeMaybe Nothing = Nothing
>
> headMaybe :: [a] -> Maybe a
> headMaybe [] = Nothing
> headMaybe (a:_) = Just a
>
> appMaybe :: (a -> b) -> Maybe a -> Maybe b
> appMaybe f Nothing = Nothing
> appMaybe f (Just a) = Just $ f a
>
> pop :: (Monad m) => m [a] -> m (Maybe a)
> pop s = do s' <- s
> return $ headMaybe s'
>
> allNothing :: [Maybe a] -> Bool
> allNothing [] = True
> allNothing (Nothing:ns) = allNothing ns
> allNothing (Just _:ns) = False
>
> showMaybeString :: Maybe String -> String
> showMaybeString (Just s) = s
> showMaybeString Nothing = ""
>
> humaniseCamel :: String -> String
> humaniseCamel [] = ""
> humaniseCamel (s:ss) = (toUpper s):humaniseCamel' ss
> humaniseCamel' :: String -> String
> humaniseCamel' [] = ""
> humaniseCamel' (s:ss) = if isUpper s then ' ':s:humaniseCamel' ss else s:humaniseCamel' ss
>
> humanise :: String -> String
> humanise = humaniseGen ' ' ' '
> humaniseGen :: Char -> Char -> String -> String
> humaniseGen e i = implode i . humaniseGen' . explode e
> humaniseGen' :: [String] -> [String]
> humaniseGen' [] = []
> humaniseGen' ((h:ts):ws) = ((toUpper h):ts):humaniseGen' ws
>
> humaniseUrl :: String -> String -> String
> humaniseUrl "" d = d
> humaniseUrl "/" d = d
> humaniseUrl u _ = humaniseGen '/' '/' $ humaniseGen '_' ' ' u
>
> humanisePath :: [String] -> String -> String
> humanisePath u d = humaniseUrl (implodeUrl u) d
? offers a way to catch validation failures
> infix 3 ?
>
> (?) :: Bool
> -> String
> -> Maybe String
> False ? s = Just s
> True ? _ = Nothing
> infix 3 ??
>
> (??) :: Bool -> String -> String
> b ?? s = showMaybeString $ b ? s