From 3df133147fe94fdbfac70133fbb74efbdbca4834 Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Sun, 24 Apr 2022 15:17:52 -0400 Subject: [PATCH] implemented encodeRows --- src/Data/CSV/Sip.hs | 8 +++++++- test/Data/CSV/SipSpec.hs | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Data/CSV/Sip.hs b/src/Data/CSV/Sip.hs index c940cd6..09b9a23 100644 --- a/src/Data/CSV/Sip.hs +++ b/src/Data/CSV/Sip.hs @@ -33,6 +33,7 @@ module Data.CSV.Sip ( slurpRawLabelledCSV, -- * Conduits -- ** Encoding + encodeRows, encodeRawRows, -- ** Decoding labelFields, @@ -60,7 +61,7 @@ import Data.Conduit.List (consume) import qualified Data.Map as M import Data.Maybe (fromMaybe) import qualified Data.Text as T -import Data.Text.Encoding (decodeUtf8') +import Data.Text.Encoding (decodeUtf8', encodeUtf8) import Data.Word (Word8) -- | read an entire CSV file @@ -97,6 +98,11 @@ slurpRawLabelledCSV slurpRawLabelledCSV file = runConduit $ sourceFile file .| decodeRawRows .| labelFields .|consume +-- | encode a CSV stream row by row, each element in the list read +-- represents a field, with the entire list representing a row +encodeRows :: Monad m => ConduitT [T.Text] BS.ByteString m () +encodeRows = mapC (map encodeUtf8) .| encodeRawRows + -- | encode raw CSV stream row by row, each element in the list read -- represents a field, with the entire list representing a row encodeRawRows :: Monad m => ConduitT [BS.ByteString] BS.ByteString m () diff --git a/test/Data/CSV/SipSpec.hs b/test/Data/CSV/SipSpec.hs index 71c749f..02f69b1 100644 --- a/test/Data/CSV/SipSpec.hs +++ b/test/Data/CSV/SipSpec.hs @@ -33,6 +33,7 @@ import Data.CSV.Sip spec :: Spec spec = describe "Data.CSV.Sip" $ do + encodeRowsSpec encodeRawRowsSpec labelFieldsSpec decodeRowsSpec @@ -40,6 +41,24 @@ spec = describe "Data.CSV.Sip" $ do decodeUTF8Spec toBytesSpec +encodeRowsSpec :: Spec +encodeRowsSpec = describe "encodeRows" $ do + result <- BS.concat <$> runConduit + (sourceList input .| encodeRows .| consume) + it ("shouldBe " ++ show expected) $ + result `shouldBe` expected + where + + input = + [ [ "foo", "a\"b" ] + , [ "a\rb", "a\nb" ] + ] + + expected = BS.concat + [ "\"foo\",\"a\"\"b\"\r\n" + , "\"a\rb\",\"a\nb\"\r\n" + ] + encodeRawRowsSpec :: Spec encodeRawRowsSpec = describe "encodeRawRows" $ do result <- BS.concat <$> runConduit