-- |Combinators for 'Text'.
module Ribosome.Text where

import Data.Char (toUpper)
import qualified Data.Text as Text

-- |Escape a single quote Neovim-style by replacing it with two single quotes.
escapeQuote :: Char -> Text
escapeQuote :: Char -> Text
escapeQuote = \case
  Char
'\'' ->
    Text
"''"
  Char
a ->
    Char -> Text
Text.singleton Char
a

-- |Escape all single quotes Neovim-style by replacing them with two single quotes.
escapeQuotes :: Text -> Text
escapeQuotes :: Text -> Text
escapeQuotes =
  (Char -> Text) -> Text -> Text
Text.concatMap Char -> Text
escapeQuote

-- |Upcase the first letter of a 'Text', if any.
capitalize :: Text -> Text
capitalize :: Text -> Text
capitalize Text
a =
  Text -> ((Char, Text) -> Text) -> Maybe (Char, Text) -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"" (Char, Text) -> Text
run (Text -> Maybe (Char, Text)
Text.uncons Text
a)
  where
    run :: (Char, Text) -> Text
run (Char
h, Text
t) =
      Char -> Text -> Text
Text.cons (Char -> Char
toUpper Char
h) Text
t