on master help sign in
Use a better version of camelTo

For example, it handles a case "CamelAPICase" into "camel_api_case"

commitc43f9b0f7955bb7a7225496311173b02a483b489 authorKostiantyn Rybnikov <k-bx@k-bx.com> dateWed 13 May 2015, 15:40 (11 years ago) parent09b2ddc
1 file changed+9−12
Data/Aeson/Types/Internal.hs+9−12
Data/Aeson/Types/Internal.hs+9−12
@@ -56,7 +56,7 @@ import Control.Applicative
5656import Control.Monad
5757import Control.DeepSeq (NFData(..))
5858import Data.ByteString.Builder (Builder, char7, toLazyByteString)
59−import Data.Char (toLower, isUpper)
59+import Data.Char (toLower, isUpper, isLower)
6060import Data.Scientific (Scientific)
6161import Data.Hashable (Hashable(..))
6262import Data.Data (Data)
@@ -519,15 +519,12 @@ defaultTaggedObject = TaggedObject
519519-- For use by Aeson template haskell calls.
520520--
521521-- > camelTo '_' 'CamelCaseAPI' == "camel_case_api"
522+-- > camelTo '_' 'CamelAPICase' == "camel_api_case"
522523camelTo :: Char -> String -> String
523−camelTo c = lastWasCap True
524− where
525− lastWasCap :: Bool -- ^ Previous was a capital letter
526− -> String -- ^ The remaining string
527− -> String
528− lastWasCap _ [] = []
529− lastWasCap prev (x : xs) = if isUpper x
530− then if prev
531− then toLower x : lastWasCap True xs
532− else c : toLower x : lastWasCap True xs
533− else x : lastWasCap False xs
524+camelTo c = map toLower . go2 . go1
525+ where go1 "" = ""
526+ go1 (x:u:l:xs) | isUpper u && isLower l = x : c : u : l : go1 xs
527+ go1 (x:xs) = x : go1 xs
528+ go2 "" = ""
529+ go2 (l:u:xs) | isLower l && isUpper u = l : c : u : go2 xs
530+ go2 (x:xs) = x : go2 xs