From 3bc01c5afa6060734442c49ed82a8585b89c15a1 Mon Sep 17 00:00:00 2001 From: Orion Kindel Date: Wed, 1 May 2024 08:52:13 -0500 Subject: [PATCH] fix: stringify options --- spago.lock | 14 ++++++++++++++ spago.yaml | 1 + src/Node.Stream.CSV.Stringify.purs | 10 ++++++---- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/spago.lock b/spago.lock index d66285a..8f0a9cd 100644 --- a/spago.lock +++ b/spago.lock @@ -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 diff --git a/spago.yaml b/spago.yaml index 8f8fb47..1ff749a 100644 --- a/spago.yaml +++ b/spago.yaml @@ -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" diff --git a/src/Node.Stream.CSV.Stringify.purs b/src/Node.Stream.CSV.Stringify.purs index 5f40b1c..6f55578 100644 --- a/src/Node.Stream.CSV.Stringify.purs +++ b/src/Node.Stream.CSV.Stringify.purs @@ -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