-
Notifications
You must be signed in to change notification settings - Fork 2
/
csv-server.js
118 lines (91 loc) · 2.63 KB
/
csv-server.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
/**
*
* @author evaisse
* @internal changelog:
* Emmanuel VAISSE - 2015-03-18 17:25:19
* Add some helpers on CSV parsing
*/
CSV = Baby;
/**
* Read a given filepath, for each line a fibers will
*
* @param {String} filepath path to csv file
* @param {Function} linecallback a callback that will recieve every parsed lines
* @param {Object} config papaParse config object : http://papaparse.com/docs#config
* @return {Object} an Array of fields if CSV file has no headers, or a Hash of key values if CSV file has header
*/
CSV.readCsvFileLineByLine = function (filepath, config, linecallback) {
var readCsv,
lineParser,
rd,
lineCount = 0,
cfg = {
fileHasHeaders: false,
},
headers;
/*
Alternative syntax
*/
if (typeof config === "function") {
linecallback = config;
config = {};
}
linecallback = linecallback || function () {};
config = config || {};
if (config.skipEmptyLines === undefined) {
config.skipEmptyLines = true;
}
/*
We handle headers ourself
*/
cfg.fileHasHeaders = !!config.headers;
delete config['headers'];
/**
* [lineParser description]
* @param {[type]} line [description]
* @return {[type]} [description]
*/
linePreprocessorParser = function (line) {
var row = {},
parsed = CSV.parse(line, config);
if (!parsed.data[0]) {
return;
}
if (cfg.fileHasHeaders) {
if (!headers) {
headers = parsed.data[0];
return;
}
headers.forEach(function (e, i) {
row[e] = parsed.data[0][i];
});
} else {
row = parsed.data[0];
}
linecallback(row, lineCount++, parsed);
}
/**
* @param {string} filepath filepath path to csv file
* @param {Function} onLineCallback Callback to be executed on line
*/
readCsv = function (filepath) {
var rd,
fs = Npm.require('fs'),
byline = Npm.require('byline'),
Future = Npm.require('fibers/future'),
future;
future = new Future;
rd = byline.createStream(fs.createReadStream(filepath));
rd.on('error', function (err) {
future.error();
});
rd.on('data', function (chunk, enc, next) {
linePreprocessorParser(chunk.toString());
});
rd.on('end', function(line) {
future.return();
});
return future.wait();
};
readCsv(filepath);
}