2018-01-17 11:52:47 +01:00
|
|
|
{-# 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
|
|
|
|
import Data.Text hiding
|
|
|
|
(map)
|
|
|
|
import Prelude hiding
|
|
|
|
(head, tail)
|
2016-02-17 22:47:30 +01:00
|
|
|
import Servant.Foreign.Internal
|
|
|
|
|
2021-02-03 07:52:28 +01:00
|
|
|
-- | Simply concat each part of the FunctionName together.
|
|
|
|
--
|
|
|
|
-- @[ "get", "documents", "by", "id" ] → "getdocumentsbyid"@
|
|
|
|
concatCase :: FunctionName -> Text
|
|
|
|
concatCase = view concatCaseL
|
|
|
|
|
2016-02-17 22:47:30 +01:00
|
|
|
concatCaseL :: Getter FunctionName Text
|
|
|
|
concatCaseL = _FunctionName . to mconcat
|
|
|
|
|
2021-02-03 07:52:28 +01:00
|
|
|
-- | Use the snake_case convention.
|
|
|
|
-- Each part is separated by a single underscore character.
|
|
|
|
--
|
|
|
|
-- @[ "get", "documents", "by", "id" ] → "get_documents_by_id"@
|
|
|
|
snakeCase :: FunctionName -> Text
|
|
|
|
snakeCase = view snakeCaseL
|
2016-02-17 22:47:30 +01:00
|
|
|
|
|
|
|
snakeCaseL :: Getter FunctionName Text
|
|
|
|
snakeCaseL = _FunctionName . to (intercalate "_")
|
|
|
|
|
2021-02-03 07:52:28 +01:00
|
|
|
-- | Use the camelCase convention.
|
|
|
|
-- The first part is lower case, every other part starts with an upper case character.
|
|
|
|
--
|
|
|
|
-- @[ "get", "documents", "by", "id" ] → "getDocumentsById"@
|
|
|
|
camelCase :: FunctionName -> Text
|
|
|
|
camelCase = view camelCaseL
|
2016-02-17 22:47:30 +01:00
|
|
|
|
|
|
|
camelCaseL :: Getter FunctionName Text
|
2016-07-24 15:46:55 +02:00
|
|
|
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
|