Go to file
2024-07-10 13:49:02 -05:00
bun feat: Parser.foreach should concurrently process records as they are read 2024-05-03 10:41:33 -05:00
src fix: bump node-stream-pipes 2024-06-23 20:35:07 -05:00
test/Test fix: bump node-stream-pipes to 2.0.2 (big perf improvement) 2024-06-22 19:57:56 -05:00
.gitignore feat: rework API, use node-stream-pipes 2024-05-10 18:40:47 -05:00
.tool-versions chore: downgrade to 0.15.15 2024-04-30 14:12:54 -05:00
bun.lockb fix: improve performance 2024-05-13 15:06:23 -05:00
jsconfig.json feat: initial commit 2024-04-30 13:39:51 -05:00
LICENSE docs: add LICENSE 2024-07-10 13:49:02 -05:00
package.json chore: prepare v2.3.0 2024-06-23 20:35:18 -05:00
README.md fix: rename modules, add readme 2024-04-30 14:09:12 -05:00
spago.lock fix: bump node-stream-pipes 2024-06-23 20:35:07 -05:00
spago.yaml chore: prepare v2.3.0 2024-06-23 20:35:18 -05:00

purescript-csv-stream

Type-safe bindings for the streaming API of csv-parse and csv-stringify.

Installing

spago install csv-stream
{bun|yarn|npm|pnpm} install csv-parse csv-stringify

Examples

Stream

module Main where

import Prelude

import Effect (Effect)
import Effect.Class (liftEffect)
import Effect.Aff (launchAff_)
import Node.Stream (pipe)
import Node.Stream as Stream
import Node.Stream.CSV.Stringify as CSV.Stringify
import Node.Stream.CSV.Parse as CSV.Parse

type MyCSVType1 = {a :: Int, b :: Int, bar :: String, baz :: Boolean}
type MyCSVType2 = {ab :: Int, bar :: String, baz :: Boolean}

atob :: MyCSVType1 -> MyCSVType2
atob {a, b, bar, baz} = {ab: a + b, bar, baz}

myCSV :: String
myCSV = "a,b,bar,baz\n1,2,\"hello, world!\",true\n3,3,,f"

main :: Effect Unit
main = launchAff_ do
  parser <- liftEffect $ CSV.Parse.make {}
  stringifier <- liftEffect $ CSV.Stringify.make {}

  input <- liftEffect $ Stream.readableFromString myCSV
  liftEffect $ Stream.pipe input parser

  records <- CSV.Parse.readAll parser
  liftEffect $ for_ records \r -> CSV.Stringify.write $ atob r
  liftEffect $ Stream.end stringifier

  -- "ab,bar,baz\n3,\"hello, world!\",true\n6,,false"
  csvString <- CSV.Stringify.readAll stringifier
  pure unit

Synchronous

module Main where

import Prelude

import Effect (Effect)
import Effect.Class (liftEffect)
import Effect.Aff (launchAff_)
import Node.Stream (pipe)
import Node.Stream as Stream
import Node.Stream.CSV.Stringify as CSV.Stringify
import Node.Stream.CSV.Parse as CSV.Parse

type MyCSVType1 = {a :: Int, b :: Int, bar :: String, baz :: Boolean}
type MyCSVType2 = {ab :: Int, bar :: String, baz :: Boolean}

atob :: MyCSVType1 -> MyCSVType2
atob {a, b, bar, baz} = {ab: a + b, bar, baz}

myCSV :: String
myCSV = "a,b,bar,baz\n1,2,\"hello, world!\",true\n3,3,,f"

main :: Effect Unit
main = launchAff_ do
  records :: Array MyCSVType1 <- CSV.Parse.parse myCSV
  -- "ab,bar,baz\n3,\"hello, world!\",true\n6,,false"
  csvString <- CSV.Stringify.stringify (atob <$> records)
  pure unit