UNPKG

823 BJavaScriptView Raw
1import CSV from 'csv-string';
2import { writeTo } from './utils';
3
4function CSVParse(data, feed) {
5 const separator = this.getParam('separator');
6 const quote = this.getParam('quote');
7 if (!this.handle) {
8 this.handle = CSV.createStream({ separator, quote });
9 this.handle.on('data', obj => feed.write(obj));
10 }
11 if (!this.isLast()) {
12 writeTo(this.handle,
13 data,
14 () => feed.end());
15 } else {
16 this.handle.end(() => feed.close());
17 }
18}
19
20/**
21 * Take `String` and parse CSV to generate object
22 *
23 * @name CSVParse
24 * @param {String} [separator=auto] to indicate the CSV separator
25 * @param {String} [quote=auto] to indicate the CSV quote.
26 * @returns {Object}
27 * @see https://github.com/Inist-CNRS/node-csv-string
28 */
29export default {
30 CSVParse,
31};