servant/servant-foreign/src/Servant/Foreign/Inflections.hs

50 lines
1.4 KiB
Haskell
Raw Normal View History

{-# LANGUAGE OverloadedStrings #-}
2016-02-17 22:47:30 +01:00
module Servant.Foreign.Inflections
( concatCase
, snakeCase
, camelCase
-- lenses
, concatCaseL
, snakeCaseL
, camelCaseL
) where
2018-06-29 21:08:26 +02:00
import Control.Lens hiding
(cons)
import qualified Data.Char as C
2016-02-17 22:47:30 +01:00
import Data.Monoid
2018-06-29 21:08:26 +02:00
import Data.Text hiding
(map)
import Prelude hiding
(head, tail)
2016-02-17 22:47:30 +01:00
import Servant.Foreign.Internal
concatCaseL :: Getter FunctionName Text
concatCaseL = _FunctionName . to mconcat
-- | Function name builder that simply concat each part together
concatCase :: FunctionName -> Text
concatCase = view concatCaseL
snakeCaseL :: Getter FunctionName Text
snakeCaseL = _FunctionName . to (intercalate "_")
-- | Function name builder using the snake_case convention.
-- each part is separated by a single underscore character.
snakeCase :: FunctionName -> Text
snakeCase = view snakeCaseL
camelCaseL :: Getter FunctionName Text
camelCaseL = _FunctionName . to convert
2016-02-17 22:47:30 +01:00
where
convert [] = ""
convert (p:ps) = mconcat $ p : map capitalize ps
capitalize "" = ""
capitalize name = C.toUpper (head name) `cons` tail name
-- | Function name builder using the CamelCase convention.
-- each part begins with an upper case character.
camelCase :: FunctionName -> Text
camelCase = view camelCaseL