-
Notifications
You must be signed in to change notification settings - Fork 0
/
csvReader.js
173 lines (98 loc) · 3.51 KB
/
csvReader.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/****************************************************************************
* csvReader.js
* openacousticdevices.info
* June 2020
*****************************************************************************/
'use strict';
const fs = require('fs');
const FILE_BUFFER_SIZE = 32 * 1024;
const fileBuffer = Buffer.alloc(FILE_BUFFER_SIZE);
/* Function to handle an individual line */
function parseLine (columnNames, tokens, parseFunctions, tokenMap, data) {
for (let i = 0; i < columnNames.length; i += 1) {
const name = columnNames[i];
const index = tokenMap[name];
if (typeof (index) === 'number') data[name].push(parseFunctions[i](tokens[index]));
}
}
/* Function to read the file */
function readFile (inputPath, columnNames, parseFunctions) {
let fi, fileSize, data, tokenMap, numberOfColumns;
/* Check arguments */
if (columnNames.length === 0 || parseFunctions.length === 0 || columnNames.length !== parseFunctions.length) {
return {
success: false,
error: 'Arguments are incorrect.'
};
}
/* Open input file */
try {
fi = fs.openSync(inputPath, 'r');
} catch (e) {
return {
success: false,
error: 'Could not open input CSV file.'
};
}
try {
fileSize = fs.statSync(inputPath).size;
} catch (e) {
return {
success: false,
error: 'Could not read input CSV file size.'
};
}
if (fileSize === 0) {
return {
success: false,
error: 'Input CSV file has zero size.'
};
}
/* Read first line */
try {
let buffer = '';
let numberOfBytesRead = 0;
while (numberOfBytesRead < fileSize) {
const numberOfBytes = Math.min(FILE_BUFFER_SIZE, fileSize - numberOfBytesRead);
fs.readSync(fi, fileBuffer, 0, numberOfBytes, null);
const newLines = buffer.concat(fileBuffer.slice(0, numberOfBytes)).toString().split(/\r?\n/);
buffer = newLines.pop();
while (newLines.length > 0) {
const line = newLines.shift();
const tokens = line.split(',');
if (tokenMap) {
if (tokens.length === numberOfColumns) parseLine(columnNames, tokens, parseFunctions, tokenMap, data);
} else {
data = {};
tokenMap = {};
numberOfColumns = tokens.length;
for (let i = 0; i < columnNames.length; i += 1) {
const name = columnNames[i];
for (let j = 0; j < tokens.length; j += 1) {
if (name === tokens[j] && tokenMap[name] === undefined) {
tokenMap[name] = j;
data[name] = [];
}
}
}
}
}
numberOfBytesRead += numberOfBytes;
}
/* Handle last line */
const tokens = buffer.split(',');
if (tokens && tokens.length === numberOfColumns) parseLine(columnNames, tokens, parseFunctions, tokenMap, data);
} catch (e) {
return {
success: false,
error: 'Something went wrong parsing CSV file.'
};
}
/* Return data */
return {
success: true,
data: data
};
}
/* Exports */
exports.readFile = readFile;