An open API service indexing awesome lists of open source software.

https://github.com/haoliangyu/rx-to-csv

RxJS 5 operator to write data into a CSV file
https://github.com/haoliangyu/rx-to-csv

csv operator read rxjs

Last synced: about 1 year ago
JSON representation

RxJS 5 operator to write data into a CSV file

Awesome Lists containing this project

README

          

# rx-to-csv

![build status](https://travis-ci.org/haoliangyu/rx-to-csv.svg?branch=master)

[![ReactiveX](http://reactivex.io/assets/Rx_Logo_S.png)](http://reactivex.io/)

[RxJS 5](http://reactivex.io/) operator to write data into a [CSV](https://en.wikipedia.org/wiki/Comma-separated_values) file

Work in both JavaScript and TypeScript

## Installation

```
npm install rx-to-csv
```

## Use

Import this library and it will add `toCSV` operator to the rxjs `Observable` class.

```
public toCSV(path: string, columns: Array, options?: any): Observable
```

This operator will search values in its input by column names and write them into the target CSV file via a write file stream.

Parameters:

* **path**: csv file path
* **columns**: an array of column names
* **options**: optional configuration for the csv creation
* **wrapText**: a boolean value indicating whether to wrap text values with `"`. Default: `true`
* **delimiter**: a character to separate values. Default: `,`

## Example

Generate a CSV file from data flow:

``` javascript
import { Observable } from 'rxjs';
import 'rx-to-csv';

let data = [
{ id: 1, name: 'Mike' },
{ id: 2, name: 'Tommy' }
];

Observable.of(...data)
.toCSV('data.csv', ['id', 'name'])
.subscribe();
```

Download data from a PostgreSQL dadtabase and save it as a CSV file:

``` javascript
import pgrx from 'pg-reactive';
import 'rx-to-csv';

let db = new pgrx('connection string');

db.query('SELECT id, display_name FROM users')
.map((row) => {
// convert the data to match column names
return {
id: row.id,
name: row.display_name
};
})
.toCSV('data.csv', ['id', 'name'])
.subscribe();
```

## License

MIT