{-# LANGUAGE OverloadedStrings #-}

-- | Some standard replacements for text
module Tophat.Text
  (
    literalNewlines,
    stripNewlines,
    stripAroundNewlines
  ) where

import Data.Text (Text)
import qualified Data.Text as T


-- | Replace "\n" with an actual newline
literalNewlines :: Text -> Text
literalNewlines :: Text -> Text
literalNewlines = Text -> Text -> Text -> Text
T.replace Text
"\\n" Text
"\n"

-- | Remove newlines
stripNewlines :: Text -> Text
stripNewlines :: Text -> Text
stripNewlines = Text -> Text -> Text -> Text
T.replace Text
"\n" Text
""

-- | Remove newlines, and whitespace around them
stripAroundNewlines :: Text -> Text
stripAroundNewlines :: Text -> Text
stripAroundNewlines = [Text] -> Text
T.concat ([Text] -> Text) -> (Text -> [Text]) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text -> Text) -> [Text] -> [Text]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> Text
T.strip ([Text] -> [Text]) -> (Text -> [Text]) -> Text -> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Text]
T.lines