From 49ae4e76fae297400eaebbf75c0b0713ab61320a Mon Sep 17 00:00:00 2001 From: Justin Leitgeb Date: Sat, 1 Mar 2014 05:45:19 -0500 Subject: [PATCH] Add decamelize function --- Text/Inflections.hs | 14 +++++++++++--- Text/Inflections/Decamelize.hs | 20 ++++++++++++++++++++ inflections.cabal | 1 + test/Text/Inflections/Parse/CamelCaseTest.hs | 4 ++-- 4 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 Text/Inflections/Decamelize.hs diff --git a/Text/Inflections.hs b/Text/Inflections.hs index 20b9683..0d17a02 100644 --- a/Text/Inflections.hs +++ b/Text/Inflections.hs @@ -1,12 +1,17 @@ module Text.Inflections ( dasherize - , transliterate - , transliterateCustom + + , decamelize + , decamelizeCustom , defaultMap , parameterize - , parameterizeCustom ) + , parameterizeCustom + + , transliterate + , transliterateCustom + ) where import Data.Char (isAscii) @@ -18,6 +23,9 @@ import Text.Inflections.Parameterize ( Transliterations , parameterize , parameterizeCustom ) +import Text.Inflections.Decamelize ( decamelize + , decamelizeCustom ) + -- |Replaces underscores with dashes in the string. dasherize :: String -> String dasherize = map (\c -> if c == ' ' then '-' else c) diff --git a/Text/Inflections/Decamelize.hs b/Text/Inflections/Decamelize.hs new file mode 100644 index 0000000..f643315 --- /dev/null +++ b/Text/Inflections/Decamelize.hs @@ -0,0 +1,20 @@ +module Text.Inflections.Decamelize (decamelize, decamelizeCustom) where + +import Text.Inflections.Parse.CamelCase (Word(..), parser) +import Text.Parsec (ParseError, parse) +import Data.Char (toLower) +import Data.List (intercalate) + +decamelize :: String -> Either ParseError String +decamelize s = decamelizeCustom [] s + +decamelizeCustom :: [String] -> String -> Either ParseError String +decamelizeCustom acronyms s = + case parse (parser acronyms) "(unknown)" s of + Left errs -> Left errs + Right res -> Right $ intercalate "_" $ map toDowncasedString res + +toDowncasedString :: Word -> String +toDowncasedString w = map toLower $ wordToS w + where wordToS (Word s) = s + wordToS (Acronym s) = s diff --git a/inflections.cabal b/inflections.cabal index 9366272..097135d 100644 --- a/inflections.cabal +++ b/inflections.cabal @@ -24,6 +24,7 @@ library , Text.Inflections.Parse.Parameterizable other-modules: Text.Inflections.Data , Text.Inflections.Parameterize + , Text.Inflections.Decamelize ghc-options: -Wall build-depends: base >=4.5 && <4.7, parsec, containers default-language: Haskell2010 diff --git a/test/Text/Inflections/Parse/CamelCaseTest.hs b/test/Text/Inflections/Parse/CamelCaseTest.hs index 687b70e..ea70369 100644 --- a/test/Text/Inflections/Parse/CamelCaseTest.hs +++ b/test/Text/Inflections/Parse/CamelCaseTest.hs @@ -28,8 +28,8 @@ tests = [testGroup "parsing" ] fromRight :: Either a b -> b -fromRight (Left _) = error - "Either.Unwrap.fromRight: Argument takes form 'Left _'" +fromRight (Left _) = + error "Either.Unwrap.fromRight: Argument takes form 'Left _'" fromRight (Right x) = x test_lowerCamelCase = fromRight (parse (parser []) "" "testThis") @?=