-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
36 lines (29 loc) · 859 Bytes
/
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
var errorsFactory = require('@debitoor/nodeerrors')
var detectDelimiter = require('./lib/detectDelimiter');
var toUtf8 = require('./lib/toUtf8');
var csvParser = require('./lib/csvParser');
var removeEmptyRows = require('./lib/removeEmptyRows');
var trimAllCells = require('./lib/trimAllCells');
var errors = errorsFactory();
module.exports = function(buffer, opts) {
opts = opts || {};
var bufferUtf8 = toUtf8(buffer);
var csv = csvParser();
csv.DELIMITER = detectDelimiter(bufferUtf8, opts);
csv.DETECT_TYPES = false;
csv.RELAXED = true;
var parsed;
try {
parsed = csv.parse(bufferUtf8);
} catch (ex) {
return {error: errors.system().innerError(ex)};
}
var rows = removeEmptyRows(parsed);
if (rows.length < 2) {
return {error: errors.tooFewRows()};
}
if (opts.trim !== false) {
trimAllCells(rows);
}
return {rows: rows};
};