Go to file
2024-05-01 11:05:46 -05:00
bun fix: prepare script 2024-04-30 14:10:49 -05:00
src fix: more bugs 2024-05-01 10:35:29 -05:00
test/Test feat: initial commit 2024-04-30 13:39:51 -05:00
.gitignore feat: initial commit 2024-04-30 13:39:51 -05:00
.tool-versions chore: downgrade to 0.15.15 2024-04-30 14:12:54 -05:00
bun.lockb feat: initial commit 2024-04-30 13:39:51 -05:00
jsconfig.json feat: initial commit 2024-04-30 13:39:51 -05:00
package.json chore: prepare v1.1.7 2024-05-01 11:05:46 -05:00
README.md fix: rename modules, add readme 2024-04-30 14:09:12 -05:00
spago.lock fix: more bugs 2024-05-01 10:02:45 -05:00
spago.yaml chore: prepare v1.1.7 2024-05-01 11:05:46 -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