mirror of
https://github.com/typeable/inflections-hs.git
synced 2024-09-11 06:05:33 +03:00
Make collection type for acronyms polymorphic
This commit is contained in:
parent
0a43bc0690
commit
477a36de5c
@ -9,6 +9,9 @@
|
||||
|
||||
* Renamed `defaultMap` to `defaultTransliterations`.
|
||||
|
||||
* `parseCamelCase` and `parseSnakeCase` take any instance of `Foldable` as a
|
||||
collection of acronyms, not just lists.
|
||||
|
||||
* Added the `CHANGELOG.md` file.
|
||||
|
||||
* Switched test suite to Hspec.
|
||||
|
@ -26,6 +26,8 @@ import qualified Data.Text as T
|
||||
|
||||
#if MIN_VERSION_base(4,8,0)
|
||||
import Prelude hiding (Word)
|
||||
#else
|
||||
import Data.Foldable
|
||||
#endif
|
||||
|
||||
-- | Parse a CamelCase string.
|
||||
@ -38,21 +40,23 @@ import Prelude hiding (Word)
|
||||
-- 1:4:
|
||||
-- unexpected '_'
|
||||
-- expecting end of input, lowercase letter, or uppercase letter
|
||||
parseCamelCase
|
||||
:: [Word 'Acronym] -- ^ Collection of acronyms
|
||||
parseCamelCase :: (Foldable f, Functor f)
|
||||
=> f (Word 'Acronym) -- ^ Collection of acronyms
|
||||
-> Text -- ^ Input
|
||||
-> Either (ParseError Char Dec) [SomeWord] -- ^ Result of parsing
|
||||
parseCamelCase acronyms = parse (parser acronyms) ""
|
||||
|
||||
parser
|
||||
:: [Word 'Acronym] -- ^ Collection of acronyms
|
||||
parser :: (Foldable f, Functor f)
|
||||
=> f (Word 'Acronym) -- ^ Collection of acronyms
|
||||
-> Parser [SomeWord] -- ^ CamelCase parser
|
||||
parser acronyms = many (a <|> n) <* eof
|
||||
where
|
||||
n = SomeWord <$> word
|
||||
a = SomeWord <$> acronym acronyms
|
||||
|
||||
acronym :: [Word 'Acronym] -> Parser (Word 'Acronym)
|
||||
acronym :: (Foldable f, Functor f)
|
||||
=> f (Word 'Acronym)
|
||||
-> Parser (Word 'Acronym)
|
||||
acronym acronyms = do
|
||||
x <- T.pack <$> choice (string . T.unpack . unWord <$> acronyms)
|
||||
case mkAcronym x of
|
||||
|
@ -25,6 +25,8 @@ import qualified Data.Text as T
|
||||
|
||||
#if MIN_VERSION_base(4,8,0)
|
||||
import Prelude hiding (Word)
|
||||
#else
|
||||
import Data.Foldable
|
||||
#endif
|
||||
|
||||
-- | Parse a snake_case string.
|
||||
@ -37,21 +39,23 @@ import Prelude hiding (Word)
|
||||
-- 1:4:
|
||||
-- unexpected 'B'
|
||||
-- expecting '_', end of input, or lowercase letter
|
||||
parseSnakeCase
|
||||
:: [Word 'Acronym] -- ^ Collection of acronyms
|
||||
parseSnakeCase :: (Foldable f, Functor f)
|
||||
=> f (Word 'Acronym) -- ^ Collection of acronyms
|
||||
-> Text -- ^ Input
|
||||
-> Either (ParseError Char Dec) [SomeWord] -- ^ Result of parsing
|
||||
parseSnakeCase acronyms = parse (parser acronyms) ""
|
||||
|
||||
parser
|
||||
:: [Word 'Acronym]
|
||||
parser :: (Foldable f, Functor f)
|
||||
=> f (Word 'Acronym)
|
||||
-> Parser [SomeWord]
|
||||
parser acronyms = ((a <|> n) `sepBy` char '_') <* eof
|
||||
where
|
||||
n = SomeWord <$> word
|
||||
a = SomeWord <$> acronym acronyms
|
||||
|
||||
acronym :: [Word 'Acronym] -> Parser (Word 'Acronym)
|
||||
acronym :: (Foldable f, Functor f)
|
||||
=> f (Word 'Acronym)
|
||||
-> Parser (Word 'Acronym)
|
||||
acronym acronyms = do
|
||||
x <- T.pack <$> choice (string . T.unpack . unWord <$> acronyms)
|
||||
case mkAcronym x of
|
||||
|
Loading…
Reference in New Issue
Block a user