@@ -0,0 +1,52 @@
1+module Data.Aeson.Inflections where
6+import Data.Text as T (Text, pack, unpack)
8+import qualified Text.Inflections as I
9+import Text.Megaparsec.Error
12+-- | Drops until first uppercase character and apply 'toUnderscore' to
14+defaultFieldLabelModifier :: String -> String
15+defaultFieldLabelModifier = toUnderscore . dropPrefix
17+defaultFieldLabelModifier' :: Text -> Text
18+defaultFieldLabelModifier' = T.pack . defaultFieldLabelModifier . T.unpack
20+-- | Just drops prefix
21+dropPrefix :: String -> String
22+dropPrefix = dropWhile (not . isUpper)
24+-- | Cuts the prefix or throws error if prefix does not match. For TH
26+cutPrefix :: String -> String -> String
27+cutPrefix pref s = if pref == take plen s
29+ else error "Prefix does not match!"
33+-- | Drops prefix and turns first character to lowercase
34+dropPrefixLower :: String -> String
35+dropPrefixLower = over _head toLower . dropPrefix
37+toUnderscore :: String -> String
38+toUnderscore = fromInflection I.toUnderscore
40+toDashed :: String -> String
41+toDashed = fromInflection I.toDashed
44+ :: (Text -> Either (ParseErrorBundle Text Void) Text)
47+fromInflection f = either error' T.unpack . f . T.pack
49+ error' = error . show . I.InflectionParsingFailed
51+toSpaced :: String -> String
52+toSpaced = fromInflection (I.toHumanized False <=< I.toUnderscore)