fix: stringify options

This commit is contained in:
orion 2024-05-01 08:52:13 -05:00
parent 67353391b2
commit 3bc01c5afa
Signed by: orion
GPG Key ID: 6D4165AE4C928719
3 changed files with 21 additions and 4 deletions

View File

@ -25,6 +25,7 @@ workspace:
- precise-datetime: ">=7.0.0 <8.0.0"
- prelude: ">=6.0.1 <7.0.0"
- record: ">=4.0.0 <5.0.0"
- record-extra
- st: ">=6.2.0 <7.0.0"
- strings: ">=6.0.1 <7.0.0"
- tailrec: ">=6.1.0 <7.0.0"
@ -82,6 +83,7 @@ workspace:
- prelude
- profunctor
- record
- record-extra
- refs
- safe-coerce
- st
@ -641,6 +643,18 @@ packages:
- functions
- prelude
- unsafe-coerce
record-extra:
type: registry
version: 5.0.1
integrity: sha256-7vnREK2fpGJ7exswSeA9UpZFuU+UXRt3SA7AFUldT/Y=
dependencies:
- arrays
- functions
- lists
- prelude
- record
- tuples
- typelevel-prelude
refs:
type: registry
version: 6.0.0

View File

@ -10,6 +10,7 @@ package:
strict: true
pedanticPackages: true
dependencies:
- record-extra
- aff: ">=7.1.0 <8.0.0"
- arrays: ">=7.3.0 <8.0.0"
- bifunctors: ">=6.0.0 <7.0.0"

View File

@ -4,6 +4,7 @@ import Prelude
import Control.Monad.Rec.Class (whileJust)
import Control.Monad.ST.Global as ST
import Data.Array as Array
import Data.Array.ST as Array.ST
import Data.CSV.Record (class WriteCSVRecord, writeCSVRecord)
import Data.Either (Either(..), blush)
@ -22,6 +23,8 @@ import Node.Stream (Read, Stream, Write)
import Node.Stream as Stream
import Prim.Row (class Union)
import Prim.RowList (class RowToList)
import Record.Extra (class Keys, keys)
import Type.Prelude (Proxy(..))
import Unsafe.Coerce (unsafeCoerce)
data CSVWrite
@ -40,7 +43,6 @@ type CSVStringifier a r = Stream (read :: Read, write :: Write, csv :: CSVWrite
-- | https://csv.js.org/stringify/options/
type Config r =
( bom :: Boolean
, group_columns_by_name :: Boolean
, delimiter :: String
, record_delimiter :: String
, escape :: String
@ -61,11 +63,11 @@ recordToForeign :: forall r. Record r -> Object Foreign
recordToForeign = unsafeCoerce
-- | Create a CSVStringifier
make :: forall @r rl @config @missing @extra. RowToList r rl => WriteCSVRecord r rl => Union config missing (Config extra) => { | config } -> Effect (CSVStringifier r ())
make = makeImpl <<< unsafeToForeign <<< Object.union (recordToForeign {columns: true, cast: false, cast_date: false}) <<< recordToForeign
make :: forall @r rl @config @missing @extra. Keys rl => RowToList r rl => WriteCSVRecord r rl => Union config missing (Config extra) => { | config } -> Effect (CSVStringifier r ())
make = makeImpl <<< unsafeToForeign <<< Object.union (recordToForeign {columns: Array.fromFoldable $ keys (Proxy @r)}) <<< recordToForeign
-- | Synchronously stringify a collection of records
stringify :: forall @r rl f @config missing extra. Foldable f => RowToList r rl => WriteCSVRecord r rl => Union config missing (Config extra) => { | config } -> f { | r } -> Aff String
stringify :: forall @r rl f @config missing extra. Keys rl => Foldable f => RowToList r rl => WriteCSVRecord r rl => Union config missing (Config extra) => { | config } -> f { | r } -> Aff String
stringify config records = do
stream <- liftEffect $ make @r @config @missing @extra config
liftEffect $ for_ records \r -> write stream r