module Web.Slack.Util
( formOpts
, jsonOpts
)
where
import Data.Aeson.TH
import Data.Char
import Web.FormUrlEncoded (FormOptions(FormOptions))
import Data.Text (Text)
import qualified Data.Text as Text
formOpts
:: Text
-> FormOptions
formOpts prefix =
FormOptions (modifyLabel prefix)
jsonOpts
:: Text
-> Options
jsonOpts prefix =
defaultOptions
{ fieldLabelModifier = modifyLabel prefix
}
modifyLabel
:: Text
-> String
-> String
modifyLabel prefix =
fmap toLower
. addUnderscores
. drop (Text.length prefix)
addUnderscores
:: String
-> String
addUnderscores =
let
go res [] = res
go [] (x:xs) = go [toLower x] xs
go res (x:xs)
| isUpper x = go (toLower x : '_' : res) xs
| otherwise = go (x : res) xs
in
reverse . go []