212
212
213
213
` fast- csv` also allows to you to create create a ` CSV ` from data.
214
214
215
- Formatting accepts the same options as parsing.
215
+ Formatting accepts the same options as parsing with an additional ` transform` option.
216
+
217
+ * ` transform (row)` : A function that accepts a row and returns a transformed one to be written.
216
218
217
219
**` createWriteStream (options)` **
218
220
@@ -235,6 +237,32 @@ csvStream.write({a: "a3", b: "b4"});
235
237
csvStream .write (null );
236
238
` ` `
237
239
240
+ If you wish to transform rows as writing then you can use the ` .transform ` method.
241
+
242
+ ` ` ` javascript
243
+ var csvStream = csv
244
+ .createWriteStream ({headers: true })
245
+ .transform (function (row ){
246
+ return {
247
+ A : row .a ,
248
+ B : row .b
249
+ };
250
+ }),
251
+ writableStream = fs .createWriteStream (" my.csv" );
252
+
253
+ writableStream .on (" finish" , function (){
254
+ console .log (" DONE!" );
255
+ });
256
+
257
+ csvSream .pipe (writableStream);
258
+ csvStream .write ({a: " a0" , b: " b0" });
259
+ csvStream .write ({a: " a1" , b: " b1" });
260
+ csvStream .write ({a: " a2" , b: " b2" });
261
+ csvStream .write ({a: " a3" , b: " b4" });
262
+ csvStream .write ({a: " a3" , b: " b4" });
263
+ csvStream .write (null );
264
+ ` ` `
265
+
238
266
**Writing Data**
239
267
240
268
Each of the following methods accept an array of values to be written, however each value must be an ` array` of ` array` s or ` object` s.
264
292
.pipe (ws);
265
293
` ` `
266
294
295
+ ` ` ` javascript
296
+ var ws = fs .createWriteStream (" my.csv" );
297
+ csv
298
+ .write ([
299
+ {a: " a1" , b: " b1" },
300
+ {a: " a2" , b: " b2" }
301
+ ], {
302
+ headers: true
303
+ transform : function (row ){
304
+ return {
305
+ A : row .a ,
306
+ B : row .b
307
+ };
308
+ }
309
+ })
310
+ .pipe (ws);
311
+ ` ` `
312
+
313
+
314
+
267
315
**` writeToStream (stream,arr[, options])` **
268
316
269
317
Write an array of values to a ` WritableStream`
286
334
.pipe (ws);
287
335
` ` `
288
336
337
+ ` ` ` javascript
338
+ csv
339
+ .writeToStream (fs .createWriteStream (" my.csv" ), [
340
+ {a: " a1" , b: " b1" },
341
+ {a: " a2" , b: " b2" }
342
+ ], {
343
+ headers: true ,
344
+ transform : function (row ){
345
+ return {
346
+ A : row .a ,
347
+ B : row .b
348
+ };
349
+ }
350
+ })
351
+ .pipe (ws);
352
+ ` ` `
353
+
289
354
**` writeToPath (arr[, options])` **
290
355
291
356
Write an array of values to the specified path
313
378
});
314
379
` ` `
315
380
381
+ ` ` ` javascript
382
+ csv
383
+ .writeToStream (" my.csv" , [
384
+ {a: " a1" , b: " b1" },
385
+ {a: " a2" , b: " b2" }
386
+ ], {
387
+ headers: true ,
388
+ transform : function (row ){
389
+ return {
390
+ A : row .a ,
391
+ B : row .b
392
+ };
393
+ }
394
+ })
395
+ .on (" finish" , function (){
396
+ console .log (" done!" );
397
+ });
398
+ ` ` `
399
+
316
400
**` writeToString (arr[, options])` **
317
401
318
402
` ` ` javascript
@@ -330,6 +414,21 @@ csv.writeToString([
330
414
], {headers: true }); // "a,b\na1,b1\na2,b2\n"
331
415
` ` `
332
416
417
+ ` ` ` javascript
418
+ csv .writeToString ([
419
+ {a: " a1" , b: " b1" },
420
+ {a: " a2" , b: " b2" }
421
+ ], {
422
+ headers: true ,
423
+ transform : function (row ){
424
+ return {
425
+ A : row .a ,
426
+ B : row .b
427
+ };
428
+ }
429
+ }); // "a,b\na1,b1\na2,b2\n"
430
+ ` ` `
431
+
333
432
## Piping from Parser to Writer
334
433
335
434
You can use ` fast- csv` to pipe the output from a parsed CSV to a transformed CSV by setting the parser to ` objectMode` and using ` createWriteStream` .
361
460
362
461
The output will contain formatted result from the transform function.
363
462
463
+ If you want to tranform on the formatting side
464
+
465
+
466
+ ` ` ` javascript
467
+ var formatStream = csv
468
+ .createWriteStream ({headers: true })
469
+ .transform (function (){
470
+ return {
471
+ name: obj .Name ,
472
+ address: obj .Address ,
473
+ emailAddress: obj .Email_Address ,
474
+ verified: obj .Verified
475
+ };
476
+ });
477
+ csv
478
+ .fromPath (" in.csv" , {headers: true })
479
+ .pipe (formatStream)
480
+ .pipe (fs .createWriteStream (" out.csv" , {encoding: " utf8" }));
481
+ ` ` `
482
+
483
+
364
484
## Benchmarks
365
485
366
486
` Parsing 20000 records AVG over 3 runs`
0 commit comments