-
Notifications
You must be signed in to change notification settings - Fork 1
/
write-csv.js
41 lines (37 loc) · 1.11 KB
/
write-csv.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
var async = require('async');
var stringify = require('csv-stringify');
/**
* Write checkins from `checkins` to Express `res` object.
*
* Checkins should be an array of objects with properties sectionName, uin,
* netid, and timestamp.
*/
var writeCSV = function(checkins, res) {
// Set up CSV stringifier
var stringifier = stringify();
stringifier.on('readable', function() {
while ((row = stringifier.read()) !== null) {
res.write(row);
}
});
stringifier.on('finish', function() {
res.end();
});
// Write header to stringifier
var header = ['section_name', 'uin', 'netid', 'timestamp', 'secret_word'];
stringifier.write(header);
// Write checkins to stringifier
async.eachSeries(checkins, function(checkin, callback) {
stringifier.write([
checkin.sectionName,
checkin.uin,
checkin.netid || '',
checkin.timestamp.toISOString(),
checkin.secretWord
]);
setImmediate(callback);
}, function() {
stringifier.end();
});
};
module.exports = writeCSV;