mirror of
https://github.com/typeable/inflections-hs.git
synced 2024-10-26 08:08:21 +03:00
Add decamelize function
This commit is contained in:
parent
e66a860ec8
commit
49ae4e76fa
@ -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)
|
||||
|
20
Text/Inflections/Decamelize.hs
Normal file
20
Text/Inflections/Decamelize.hs
Normal 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
|
@ -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
|
||||
|
@ -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") @?=
|
||||
|
Loading…
Reference in New Issue
Block a user