Add decamelize function

This commit is contained in:
Justin Leitgeb 2014-03-01 05:45:19 -05:00
parent e66a860ec8
commit 49ae4e76fa
4 changed files with 34 additions and 5 deletions

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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") @?=