module Control.Monad.Ox.Text
( prefix
, suffix
, substr
, shape
, pack
) where
import qualified Data.Char as C
import qualified Data.Text as T
prefix :: Int -> T.Text -> Maybe T.Text
prefix k xs
| k > 0 && k <= n = Just $ T.take k xs
| k <= 0 && n + k > 0 = Just $ T.take (n + k) xs
| otherwise = Nothing
where
n = T.length xs
suffix :: Int -> T.Text -> Maybe T.Text
suffix k xs
| k > 0 && k <= n = Just . takeR k $ xs
| k <= 0 && n + k > 0 = Just . takeR (n + k) $ xs
| otherwise = Nothing
where
takeR i = T.reverse . T.take i . T.reverse
n = T.length xs
substr :: Int -> T.Text -> [T.Text]
substr k xs
| T.length x < k = []
| otherwise = x : substr k (T.tail xs)
where
x = T.take k xs
shape :: T.Text -> T.Text
shape = T.map translate
where
translate char
| C.isLower char = 'l'
| C.isUpper char = 'u'
| C.isDigit char = 'd'
| otherwise = 'x'
pack :: T.Text -> T.Text
pack = T.pack . map T.head . T.group