feat: foreach
This commit is contained in:
parent
e594472a9c
commit
ad46b8daa7
@ -87,6 +87,18 @@ parse config csv = do
|
|||||||
liftEffect $ Stream.end stream
|
liftEffect $ Stream.end stream
|
||||||
readAll stream
|
readAll stream
|
||||||
|
|
||||||
|
-- | Loop until the stream is closed, invoking the callback with each record as it is parsed.
|
||||||
|
foreach :: forall @r rl x. RowToList r rl => ReadCSVRecord r rl => CSVParser r x -> ({ | r } -> Aff Unit) -> Aff Unit
|
||||||
|
foreach stream cb = whileJust do
|
||||||
|
isReadable <- liftEffect $ Stream.readable stream
|
||||||
|
when (not isReadable) $ makeAff \res -> mempty <* flip (Event.once Stream.readableH) stream $ res $ Right unit
|
||||||
|
whileJust do
|
||||||
|
r <- liftEffect $ read @r stream
|
||||||
|
for_ r cb
|
||||||
|
pure $ void r
|
||||||
|
isClosed <- liftEffect $ Stream.closed stream
|
||||||
|
pure $ if isClosed then Nothing else Just unit
|
||||||
|
|
||||||
-- | Reads a parsed record from the stream.
|
-- | Reads a parsed record from the stream.
|
||||||
-- |
|
-- |
|
||||||
-- | Returns `Nothing` when either:
|
-- | Returns `Nothing` when either:
|
||||||
@ -101,18 +113,7 @@ read stream = runMaybeT do
|
|||||||
readAll :: forall @r rl a. RowToList r rl => ReadCSVRecord r rl => CSVParser r a -> Aff (Array { | r })
|
readAll :: forall @r rl a. RowToList r rl => ReadCSVRecord r rl => CSVParser r a -> Aff (Array { | r })
|
||||||
readAll stream = do
|
readAll stream = do
|
||||||
records <- liftEffect $ ST.toEffect $ Array.ST.new
|
records <- liftEffect $ ST.toEffect $ Array.ST.new
|
||||||
|
foreach stream $ void <<< liftEffect <<< ST.toEffect <<< flip Array.ST.push records
|
||||||
whileJust do
|
|
||||||
isReadable <- liftEffect $ Stream.readable stream
|
|
||||||
when (not isReadable) $ makeAff \res -> mempty <* flip (Event.once Stream.readableH) stream $ res $ Right unit
|
|
||||||
liftEffect $ whileJust do
|
|
||||||
r <- read @r stream
|
|
||||||
for_ r \r' -> ST.toEffect $ Array.ST.push r' records
|
|
||||||
pure $ void r
|
|
||||||
|
|
||||||
isClosed <- liftEffect $ Stream.closed stream
|
|
||||||
pure $ if isClosed then Nothing else Just unit
|
|
||||||
|
|
||||||
liftEffect $ ST.toEffect $ Array.ST.unsafeFreeze records
|
liftEffect $ ST.toEffect $ Array.ST.unsafeFreeze records
|
||||||
|
|
||||||
-- | `data` event. Emitted when a CSV record has been parsed.
|
-- | `data` event. Emitted when a CSV record has been parsed.
|
||||||
|
@ -79,22 +79,22 @@ stringify config records = do
|
|||||||
write :: forall @r rl a. RowToList r rl => WriteCSVRecord r rl => CSVStringifier r a -> { | r } -> Effect Unit
|
write :: forall @r rl a. RowToList r rl => WriteCSVRecord r rl => CSVStringifier r a -> { | r } -> Effect Unit
|
||||||
write s = writeImpl s <<< writeCSVRecord @r @rl
|
write s = writeImpl s <<< writeCSVRecord @r @rl
|
||||||
|
|
||||||
|
-- | Loop until the stream is closed, invoking the callback with each chunk of stringified CSV text.
|
||||||
|
foreach :: forall r x. CSVStringifier r x -> (String -> Aff Unit) -> Aff Unit
|
||||||
|
foreach stream cb = whileJust do
|
||||||
|
isReadable <- liftEffect $ Stream.readable stream
|
||||||
|
when (not isReadable) $ makeAff \res -> mempty <* flip (Event.once Stream.readableH) stream $ res $ Right unit
|
||||||
|
whileJust do
|
||||||
|
s <- liftEffect $ (join <<< map blush) <$> Stream.readEither stream
|
||||||
|
for_ s cb
|
||||||
|
pure $ void s
|
||||||
|
isClosed <- liftEffect $ Stream.closed stream
|
||||||
|
pure $ if isClosed then Nothing else Just unit
|
||||||
|
|
||||||
-- | Read the stringified chunks until end-of-stream, returning the entire CSV string.
|
-- | Read the stringified chunks until end-of-stream, returning the entire CSV string.
|
||||||
readAll :: forall r a. CSVStringifier r a -> Aff String
|
readAll :: forall r a. CSVStringifier r a -> Aff String
|
||||||
readAll stream = do
|
readAll stream = do
|
||||||
chunks <- liftEffect $ ST.toEffect $ Array.ST.new
|
chunks <- liftEffect $ ST.toEffect $ Array.ST.new
|
||||||
|
foreach stream $ void <<< liftEffect <<< ST.toEffect <<< flip Array.ST.push chunks
|
||||||
whileJust do
|
|
||||||
isReadable <- liftEffect $ Stream.readable stream
|
|
||||||
when (not isReadable) $ makeAff \res -> mempty <* flip (Event.on Stream.readableH) stream $ res $ Right unit
|
|
||||||
|
|
||||||
liftEffect $ whileJust do
|
|
||||||
s <- (join <<< map blush) <$> Stream.readEither stream
|
|
||||||
for_ s \s' -> ST.toEffect $ Array.ST.push s' chunks
|
|
||||||
pure $ void s
|
|
||||||
|
|
||||||
isClosed <- liftEffect $ Stream.closed stream
|
|
||||||
pure $ if isClosed then Nothing else Just unit
|
|
||||||
|
|
||||||
chunks' <- liftEffect $ ST.toEffect $ Array.ST.unsafeFreeze chunks
|
chunks' <- liftEffect $ ST.toEffect $ Array.ST.unsafeFreeze chunks
|
||||||
pure $ fold chunks'
|
pure $ fold chunks'
|
||||||
|
@ -1,2 +1 @@
|
|||||||
module Node.Stream.CSV where
|
module Node.Stream.CSV where
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user