git.iko.soy/typeable/servant
on master help sign in
Merge pull request #262 from mattjbray/fix-camel-case

Fix camelCase

commit0d8439c5cd6a18ccca62aac5080d0a2f8e19d335 authorJulian Arni <jkarni@gmail.com> dateWed 4 Nov 2015, 17:51 (11 years ago) parent380ad6bf6ee02e
4 files changed+37−4
servant-foreign/servant-foreign.cabal+13 servant-foreign/src/Servant/Foreign.hs+6−4 servant-foreign/test/Servant/ForeignSpec.hs+17 servant-foreign/test/Spec.hs+1
servant-foreign/servant-foreign.cabal+13
@@ -33,3 +33,16 @@ library
3333 hs-source-dirs: src
3434 default-language: Haskell2010
3535 ghc-options: -Wall
36+
37+
38+test-suite spec
39+ type: exitcode-stdio-1.0
40+ hs-source-dirs: test
41+ ghc-options: -Wall
42+ main-is: Spec.hs
43+ other-modules:
44+ Servant.ForeignSpec
45+ build-depends: base
46+ , hspec >= 2.1.8
47+ , servant-foreign
48+ default-language: Haskell2010
servant-foreign/src/Servant/Foreign.hs+6−4
@@ -47,6 +47,7 @@ module Servant.Foreign
4747 ) where
4848
4949import Control.Lens (makeLenses, (%~), (&), (.~), (<>~))
50+import qualified Data.Char as C
5051import Data.Proxy
5152import Data.Text
5253import GHC.Exts (Constraint)
@@ -66,10 +67,11 @@ snakeCase = intercalate "_"
6667-- | Function name builder using the CamelCase convention.
6768-- each part begins with an upper case character.
6869camelCase :: FunctionName -> Text
69−camelCase [] = ""
70−camelCase (p:ps) = concat $ p : camelCase' ps
71− where camelCase' [] = []
72− camelCase' (r:rs) = toUpper r : camelCase' rs
70+camelCase = camelCase' . Prelude.map (replace "-" "")
71+ where camelCase' [] = ""
72+ camelCase' (p:ps) = concat $ p : Prelude.map capitalize ps
73+ capitalize "" = ""
74+ capitalize name = C.toUpper (Data.Text.head name) `cons` Data.Text.tail name
7375
7476type Arg = Text
7577
servant-foreign/test/Servant/ForeignSpec.hsnew file+17
@@ -0,0 +1,17 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+
3+module Servant.ForeignSpec where
4+
5+import Servant.Foreign (camelCase)
6+
7+import Test.Hspec
8+
9+spec :: Spec
10+spec = describe "Servant.Foreign" $ do
11+ camelCaseSpec
12+
13+camelCaseSpec :: Spec
14+camelCaseSpec = describe "camelCase" $ do
15+ it "converts FunctionNames to camelCase" $ do
16+ camelCase ["post", "counter", "inc"] `shouldBe` "postCounterInc"
17+ camelCase ["get", "hyphen-ated", "counter"] `shouldBe` "getHyphenatedCounter"
servant-foreign/test/Spec.hsnew file+1
@@ -0,0 +1 @@
1+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}