git.iko.soy/typeable/aeson-inflections
on master help sign in
2 files changed+36−24
since splitsnapshots
unifiedsplit
aeson-inflections.cabal+29−23 src/Data/Aeson/Inflections.hs+7−1
aeson-inflections.cabal+29−23
@@ -1,25 +1,31 @@
1−cabal-version: 2.4
2−name: aeson-inflections
3−version: 0.1.0.0
4−synopsis: Collection of useful functions for aeson instances and stuff
5−author: Author name here
6−maintainer: example@example.com
7−license-file: LICENSE
8−category: Web
9−build-type: Simple
1+cabal-version: 2.4
2+name: aeson-inflections
3+version: 0.1.0.0
4+synopsis:
5+ Collection of useful functions for aeson instances and stuff
6+
7+author: Author name here
8+maintainer: example@example.com
9+license-file: LICENSE
10+category: Web
11+build-type: Simple
1012
1113library
12− hs-source-dirs: src
13− ghc-options: -Wall
14− exposed-modules: Data.Aeson.Inflections
15− , Data.Aeson.Inflections.Generic
16− build-depends: base >= 4.7 && < 5
17− , aeson
18− , deriving-aeson
19− , inflections
20− , lens
21− , megaparsec
22− , template-haskell
23− , text
24− default-extensions: DataKinds
25− default-language: Haskell2010
14+ hs-source-dirs: src
15+ ghc-options: -Wall
16+ exposed-modules:
17+ Data.Aeson.Inflections
18+ Data.Aeson.Inflections.Generic
19+
20+ build-depends:
21+ , aeson
22+ , base >=4.12 && <5
23+ , deriving-aeson >=0.2
24+ , inflections
25+ , lens
26+ , megaparsec >=7.0
27+ , template-haskell
28+ , text
29+
30+ default-extensions: DataKinds
31+ default-language: Haskell2010
src/Data/Aeson/Inflections.hs+7−1
@@ -7,6 +7,7 @@ module Data.Aeson.Inflections
77 , dropPrefixLower
88 , dropByPrefixLength
99 , cutPrefix
10+ , cutOptionalPrefix
1011 , toUnderscore
1112 , toDashed
1213 , toSpaced
@@ -17,8 +18,9 @@ import Control.Monad
1718import Data.Aeson
1819import Data.Aeson.TH
1920import Data.Char
21+import Data.Maybe
2022import Data.Text (Text)
21−import qualified Data.Text as T (length, pack, unpack)
23+import qualified Data.Text as T
2224import Data.Void
2325import Language.Haskell.TH
2426import qualified Text.Inflections as I
@@ -60,6 +62,10 @@ cutPrefix pref s = if pref == take plen s
6062 where
6163 plen = length pref
6264
65+-- | Drop prefix if exists
66+cutOptionalPrefix :: Text -> Text -> Text
67+cutOptionalPrefix prefix txt = fromMaybe txt $ T.stripPrefix prefix txt
68+
6369-- | Drops prefix and turns first character to lowercase
6470dropPrefixLower :: String -> String
6571dropPrefixLower = over _head toLower . dropPrefix