{-# LANGUAGE OverloadedStrings #-}
module Servant.Foreign.Inflections
( concatCase
, snakeCase
, camelCase
, concatCaseL
, snakeCaseL
, camelCaseL
) where
import Control.Lens hiding
(cons)
import qualified Data.Char as C
import Data.Text hiding
(map)
import Prelude hiding
(head, tail)
import Servant.Foreign.Internal
concatCase :: FunctionName -> Text
concatCase :: FunctionName -> Text
concatCase = Getting Text FunctionName Text -> FunctionName -> Text
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting Text FunctionName Text
Getter FunctionName Text
concatCaseL
concatCaseL :: Getter FunctionName Text
concatCaseL :: Getter FunctionName Text
concatCaseL = ([Text] -> f [Text]) -> FunctionName -> f FunctionName
Iso' FunctionName [Text]
_FunctionName (([Text] -> f [Text]) -> FunctionName -> f FunctionName)
-> ((Text -> f Text) -> [Text] -> f [Text])
-> (Text -> f Text)
-> FunctionName
-> f FunctionName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Text] -> Text) -> (Text -> f Text) -> [Text] -> f [Text]
forall (p :: * -> * -> *) (f :: * -> *) s a.
(Profunctor p, Contravariant f) =>
(s -> a) -> Optic' p f s a
to [Text] -> Text
forall a. Monoid a => [a] -> a
mconcat
snakeCase :: FunctionName -> Text
snakeCase :: FunctionName -> Text
snakeCase = Getting Text FunctionName Text -> FunctionName -> Text
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting Text FunctionName Text
Getter FunctionName Text
snakeCaseL
snakeCaseL :: Getter FunctionName Text
snakeCaseL :: Getter FunctionName Text
snakeCaseL = ([Text] -> f [Text]) -> FunctionName -> f FunctionName
Iso' FunctionName [Text]
_FunctionName (([Text] -> f [Text]) -> FunctionName -> f FunctionName)
-> ((Text -> f Text) -> [Text] -> f [Text])
-> (Text -> f Text)
-> FunctionName
-> f FunctionName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Text] -> Text) -> (Text -> f Text) -> [Text] -> f [Text]
forall (p :: * -> * -> *) (f :: * -> *) s a.
(Profunctor p, Contravariant f) =>
(s -> a) -> Optic' p f s a
to (Text -> [Text] -> Text
intercalate Text
"_")
camelCase :: FunctionName -> Text
camelCase :: FunctionName -> Text
camelCase = Getting Text FunctionName Text -> FunctionName -> Text
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting Text FunctionName Text
Getter FunctionName Text
camelCaseL
camelCaseL :: Getter FunctionName Text
camelCaseL :: Getter FunctionName Text
camelCaseL = ([Text] -> f [Text]) -> FunctionName -> f FunctionName
Iso' FunctionName [Text]
_FunctionName (([Text] -> f [Text]) -> FunctionName -> f FunctionName)
-> ((Text -> f Text) -> [Text] -> f [Text])
-> (Text -> f Text)
-> FunctionName
-> f FunctionName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Text] -> Text) -> (Text -> f Text) -> [Text] -> f [Text]
forall (p :: * -> * -> *) (f :: * -> *) s a.
(Profunctor p, Contravariant f) =>
(s -> a) -> Optic' p f s a
to [Text] -> Text
convert
where
convert :: [Text] -> Text
convert [] = Text
""
convert (Text
p:[Text]
ps) = [Text] -> Text
forall a. Monoid a => [a] -> a
mconcat ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ Text
p Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: (Text -> Text) -> [Text] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Text
capitalize [Text]
ps
capitalize :: Text -> Text
capitalize Text
"" = Text
""
capitalize Text
name = Char -> Char
C.toUpper (HasCallStack => Text -> Char
Text -> Char
head Text
name) Char -> Text -> Text
`cons` HasCallStack => Text -> Text
Text -> Text
tail Text
name