comma 1.0.1 → 1.1.0
raw patch · 2 files changed
+9/−8 lines, 2 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- comma.cabal +1/−1
- src/Text/Comma.hs +8/−7
comma.cabal view
@@ -1,5 +1,5 @@ name: comma-version: 1.0.1+version: 1.1.0 synopsis: CSV Parser & Producer description: Comma is a simple CSV format parser and producer that closely follows the RFC4180 document.
src/Text/Comma.hs view
@@ -1,7 +1,7 @@ {- | Module : Text.Comma Description : CSV Parser & Producer-Copyright : (c) Daniel Lovasko, 2017+Copyright : (c) 2017 Daniel Lovasko License : BSD2 Maintainer : Daniel Lovasko <daniel.lovasko@gmail.com>@@ -26,24 +26,25 @@ field :: A.Parser T.Text -- ^ parser field = fmap T.concat quoted <|> normal A.<?> "field" where- normal = A.takeWhile (A.notInClass "\n\r,\"") A.<?> "normal field"+ normal = A.takeWhile (A.notInClass "\n,\"") A.<?> "normal field" quoted = A.char '"' *> many between <* A.char '"' A.<?> "quoted field" between = A.takeWhile1 (/= '"') <|> (A.string "\"\"" *> pure "\"") -- | Parse a block of text into a CSV table. comma :: T.Text -- ^ CSV text -> Either String [[T.Text]] -- ^ error | table-comma text = A.parseOnly table (T.stripEnd text)+comma text = A.parseOnly table fixEnd where- table = A.sepBy1 record A.endOfLine A.<?> "table"- record = A.sepBy1 field (A.char ',') A.<?> "record"+ table = A.sepBy1 record (A.char '\n') A.<?> "table"+ record = A.sepBy1 field (A.char ',') A.<?> "record"+ fixEnd = maybe text id (T.stripSuffix "\n" text) -- | Render a table of texts into a valid CSV output. uncomma :: [[T.Text]] -- ^ table -> T.Text -- ^ CSV text-uncomma = T.unlines . map (\r -> T.concat $ intersperse "," (map conv r))+uncomma = T.unlines . map (T.concat . intersperse "," . map conv) where- isQuoted = T.any (`elem` ['"', '\n', '\r', ','])+ isQuoted = T.any (`elem` ['"', '\n', ',']) enquote x = T.concat ["\"", x, "\""] conv f | isQuoted f = enquote (T.replace "\"" "\"\"" f)