27 lines
655 B
Haskell
27 lines
655 B
Haskell
|
module TitleCase (titleCase) where
|
||
|
|
||
|
import qualified Data.Char as Char
|
||
|
|
||
|
titleCase :: String -> String -> String
|
||
|
titleCase minor title =
|
||
|
unwords $
|
||
|
map toTitle (take 1 titleWords)
|
||
|
<> map maybeToTitle (drop 1 titleWords)
|
||
|
where
|
||
|
titleWords, minorWords :: [String]
|
||
|
titleWords = words . toLower $ title
|
||
|
minorWords = words . toLower $ minor
|
||
|
|
||
|
maybeToTitle :: String -> String
|
||
|
maybeToTitle word
|
||
|
| word `elem` minorWords = word
|
||
|
| otherwise = toTitle word
|
||
|
|
||
|
toTitle :: String -> String
|
||
|
toTitle t = case t of
|
||
|
"" -> ""
|
||
|
(x:xs) -> Char.toUpper x : xs
|
||
|
|
||
|
toLower :: String -> String
|
||
|
toLower = map Char.toLower
|