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`.
|
* 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.
|
* Added the `CHANGELOG.md` file.
|
||||||
|
|
||||||
* Switched test suite to Hspec.
|
* Switched test suite to Hspec.
|
||||||
|
@ -26,6 +26,8 @@ import qualified Data.Text as T
|
|||||||
|
|
||||||
#if MIN_VERSION_base(4,8,0)
|
#if MIN_VERSION_base(4,8,0)
|
||||||
import Prelude hiding (Word)
|
import Prelude hiding (Word)
|
||||||
|
#else
|
||||||
|
import Data.Foldable
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
-- | Parse a CamelCase string.
|
-- | Parse a CamelCase string.
|
||||||
@ -38,21 +40,23 @@ import Prelude hiding (Word)
|
|||||||
-- 1:4:
|
-- 1:4:
|
||||||
-- unexpected '_'
|
-- unexpected '_'
|
||||||
-- expecting end of input, lowercase letter, or uppercase letter
|
-- expecting end of input, lowercase letter, or uppercase letter
|
||||||
parseCamelCase
|
parseCamelCase :: (Foldable f, Functor f)
|
||||||
:: [Word 'Acronym] -- ^ Collection of acronyms
|
=> f (Word 'Acronym) -- ^ Collection of acronyms
|
||||||
-> Text -- ^ Input
|
-> Text -- ^ Input
|
||||||
-> Either (ParseError Char Dec) [SomeWord] -- ^ Result of parsing
|
-> Either (ParseError Char Dec) [SomeWord] -- ^ Result of parsing
|
||||||
parseCamelCase acronyms = parse (parser acronyms) ""
|
parseCamelCase acronyms = parse (parser acronyms) ""
|
||||||
|
|
||||||
parser
|
parser :: (Foldable f, Functor f)
|
||||||
:: [Word 'Acronym] -- ^ Collection of acronyms
|
=> f (Word 'Acronym) -- ^ Collection of acronyms
|
||||||
-> Parser [SomeWord] -- ^ CamelCase parser
|
-> Parser [SomeWord] -- ^ CamelCase parser
|
||||||
parser acronyms = many (a <|> n) <* eof
|
parser acronyms = many (a <|> n) <* eof
|
||||||
where
|
where
|
||||||
n = SomeWord <$> word
|
n = SomeWord <$> word
|
||||||
a = SomeWord <$> acronym acronyms
|
a = SomeWord <$> acronym acronyms
|
||||||
|
|
||||||
acronym :: [Word 'Acronym] -> Parser (Word 'Acronym)
|
acronym :: (Foldable f, Functor f)
|
||||||
|
=> f (Word 'Acronym)
|
||||||
|
-> Parser (Word 'Acronym)
|
||||||
acronym acronyms = do
|
acronym acronyms = do
|
||||||
x <- T.pack <$> choice (string . T.unpack . unWord <$> acronyms)
|
x <- T.pack <$> choice (string . T.unpack . unWord <$> acronyms)
|
||||||
case mkAcronym x of
|
case mkAcronym x of
|
||||||
|
@ -25,6 +25,8 @@ import qualified Data.Text as T
|
|||||||
|
|
||||||
#if MIN_VERSION_base(4,8,0)
|
#if MIN_VERSION_base(4,8,0)
|
||||||
import Prelude hiding (Word)
|
import Prelude hiding (Word)
|
||||||
|
#else
|
||||||
|
import Data.Foldable
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
-- | Parse a snake_case string.
|
-- | Parse a snake_case string.
|
||||||
@ -37,21 +39,23 @@ import Prelude hiding (Word)
|
|||||||
-- 1:4:
|
-- 1:4:
|
||||||
-- unexpected 'B'
|
-- unexpected 'B'
|
||||||
-- expecting '_', end of input, or lowercase letter
|
-- expecting '_', end of input, or lowercase letter
|
||||||
parseSnakeCase
|
parseSnakeCase :: (Foldable f, Functor f)
|
||||||
:: [Word 'Acronym] -- ^ Collection of acronyms
|
=> f (Word 'Acronym) -- ^ Collection of acronyms
|
||||||
-> Text -- ^ Input
|
-> Text -- ^ Input
|
||||||
-> Either (ParseError Char Dec) [SomeWord] -- ^ Result of parsing
|
-> Either (ParseError Char Dec) [SomeWord] -- ^ Result of parsing
|
||||||
parseSnakeCase acronyms = parse (parser acronyms) ""
|
parseSnakeCase acronyms = parse (parser acronyms) ""
|
||||||
|
|
||||||
parser
|
parser :: (Foldable f, Functor f)
|
||||||
:: [Word 'Acronym]
|
=> f (Word 'Acronym)
|
||||||
-> Parser [SomeWord]
|
-> Parser [SomeWord]
|
||||||
parser acronyms = ((a <|> n) `sepBy` char '_') <* eof
|
parser acronyms = ((a <|> n) `sepBy` char '_') <* eof
|
||||||
where
|
where
|
||||||
n = SomeWord <$> word
|
n = SomeWord <$> word
|
||||||
a = SomeWord <$> acronym acronyms
|
a = SomeWord <$> acronym acronyms
|
||||||
|
|
||||||
acronym :: [Word 'Acronym] -> Parser (Word 'Acronym)
|
acronym :: (Foldable f, Functor f)
|
||||||
|
=> f (Word 'Acronym)
|
||||||
|
-> Parser (Word 'Acronym)
|
||||||
acronym acronyms = do
|
acronym acronyms = do
|
||||||
x <- T.pack <$> choice (string . T.unpack . unWord <$> acronyms)
|
x <- T.pack <$> choice (string . T.unpack . unWord <$> acronyms)
|
||||||
case mkAcronym x of
|
case mkAcronym x of
|
||||||
|
Loading…
Reference in New Issue
Block a user