Skip to content
/ csv Public

🚰 RxJS operators for handling CSV data streams

License

Notifications You must be signed in to change notification settings

rxtoolkit/csv

Repository files navigation

@rxtk/csv

🚰 RxJS operators for handling CSV data streams

☠️ Warning: Be careful about upgrading the version of csv-parse used in this package. csv-parse does not use proper semantic versioning. For example upgrading it from 4.15 to 4.16 introduces breaking changes that will cause code that previously parsed CSV properly to no longer do so. Any such updates may require a major version change to this package.

npm i @rxtk/csv
yarn add @rxtk/csv

API

toCSV

import {from} from 'rxjs';
import {toCSV} from '@rxtk/csv';

const pirate$ = from([
  {name: 'Blackbeard', systolicBp: 140, diastolicBp: 91, message: 'Yarr'},
  {name: 'Crunch', systolicBp: 120, diastolicBp: 80, message: 'Arr'},
  {name: 'Blackbeard', systolicBp: 110, diastolicBp: 70, message: 'Savvy'},
]);

const csvRow$ = pirate$.pipe(toCSV(options));
csvRow$.subscribe(console.log);
// "name","systolicBp","dialostilicBp","message"
// "Blackbeard",140,91,"Yarr"
// "Crunch",120,180,"Arr"
// "Sparrow",110,70,"Savvy"

parseCSV

Given a stream of CSV data, parse it into rows/data:

import { from } from 'rxjs';
import { map } from 'rxjs/operators';
import { parseCSV } from '@rxtk/csv';

// Create a stream of raw CSV data
const csvString$ = from([
  '"name","systolicBp","dialostilicBp","message"\n', 
  '"Blackbeard",140,91,"Yarr"\n"Crunch",120,', 
  ',180,"Arr"\n"Sparrow",110,70,"Savvy"\n',
]);

// Stream the CSV data into an RxJS Subject
const row$ = csvString$.pipe(parseCSV());
row$.subscribe(console.log);
// {name: "Blackbeard", systolicBp: 140, diastolicBp: 91, message: 'Yarr'},
// {name: "Crunch", systolicBp: 120, diastolicBp: 80, message: 'Arr'},
// {name: "Sparrow", systolicBp: 110, diastolicBp: 70, message: 'Savvy'},