-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadCsvFile.js
More file actions
36 lines (33 loc) · 965 Bytes
/
readCsvFile.js
File metadata and controls
36 lines (33 loc) · 965 Bytes
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
import {parse} from 'csv-parse';
import readTextFile from './readTextFile.js';
/**
* @param {object} arg
* @param {string} arg.filePath
* @param {string} [arg.columnSeparator]
* @param {string} [arg.rowSeparator]
* @param {string[]} [arg.columns]
* @returns {Promise<string[][]>}
*/
const readCsvFile = async ({filePath, columnSeparator = ',', rowSeparator = '\n', columns = undefined }) => {
const csvString = await readTextFile({filePath});
return new Promise((resolve, reject) => {
parse(
csvString,
{
delimiter: columnSeparator,
record_delimiter: rowSeparator, // eslint-disable-line camelcase
trim: true, // Remove surrounding whitespaces
skip_empty_lines: false, // eslint-disable-line camelcase
columns,
},
(err, records) => {
if (err) {
reject(err);
} else {
resolve(records);
}
}
);
});
};
export default readCsvFile;