-- | -- Module : Text.Inflections.Humanize -- Copyright : © 2016 Justin Leitgeb -- License : MIT -- -- Maintainer : Justin Leitgeb <justin@stackbuilders.com> -- Stability : experimental -- Portability : portable -- -- Conversion to “humanized” phrases. {-# LANGUAGE CPP #-} {-# LANGUAGE OverloadedStrings #-} module Text.Inflections.Humanize ( humanize , humanizeCustom ) where import Data.Text (Text) import Text.Inflections.Types import qualified Data.Text as T #if !MIN_VERSION_base(4,8,0) import Control.Applicative #endif -- | Capitalize the first word and separate words with spaces. Like -- 'Text.Inflections.Titleize.titleize', this is meant for creating pretty -- output. -- -- >>> foo <- SomeWord <$> mkWord "foo" -- >>> bar <- SomeWord <$> mkAcronym "bar" -- >>> bazz <- SomeWord <$> mkWord "bazz" -- >>> humanize [foo,bar,bazz] -- "Foo bar bazz" humanize :: [SomeWord] -- ^ List of words, first of which will be capitalized -> Text -- ^ The humanized output humanize :: [SomeWord] -> Text humanize = Bool -> [SomeWord] -> Text humanizeCustom Bool True -- | Separate words with spaces, optionally capitalizing the first word. Like -- 'Text.Inflections.Titleize.titleize', this is meant for creating pretty -- output. -- -- >>> foo <- SomeWord <$> mkWord "foo" -- >>> bar <- SomeWord <$> mkAcronym "bar" -- >>> bazz <- SomeWord <$> mkWord "bazz" -- >>> humanizeCustom True [foo,bar,bazz] -- "Foo bar bazz" -- >>> humanizeCustom False [foo,bar,bazz] -- "foo bar bazz" -- -- /since 0.3.0.0/ humanizeCustom :: Bool -- ^ Whether to capitalize the first character in the output String -> [SomeWord] -- ^ List of words, first of which will be capitalized -> Text -- ^ The humanized output humanizeCustom :: Bool -> [SomeWord] -> Text humanizeCustom Bool c [SomeWord] xs' = case (Text -> Text) -> SomeWord -> Text unSomeWord (Text -> Text -> Text -> Text T.replace Text "_" Text " ") forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b <$> [SomeWord] xs' of [] -> Text "" (Text x:[Text] xs) -> [Text] -> Text T.unwords forall a b. (a -> b) -> a -> b $ (if Bool c then Text -> Text T.toTitle else Text -> Text T.toLower) Text x forall a. a -> [a] -> [a] : (Text -> Text T.toLower forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b <$> [Text] xs)