-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
44 lines (41 loc) · 1.06 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
'use strict';
const parse = require('./src/parse');
const defaultOptions = {
header: true,
columns: [],
delimiter: ',',
quote: '"',
detail: false,
nullOnEmpty: false,
map: record => record,
batch: false,
batchSize: 10000,
batchExecution: batch => batch,
getInitialValue: () => [],
reducer: (current, record) => {
current.push(record);
return current;
}
};
/**
* CSV parse and then batch
* @param {Object} readStream - a readable stream
* @param {Object} overrides - overrides default options
* @return {Promise} - a promise the resolves when all the batches finish or rejects if there was an error
*/
async function csvBatch(readStream, overrides) {
const options = Object.assign({}, defaultOptions, overrides);
return new Promise((resolve, reject) => {
const parser = parse(options);
let results = null;
parser.on('finish', () => {
resolve(results);
});
parser.on('results', result => {
results = result;
});
parser.on('error', reject);
readStream.pipe(parser);
});
}
module.exports = csvBatch;