-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (43 loc) · 1004 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'use strict';
var XHRStream = require('buffered-xhr-stream');
module.exports = CSVStream;
require('component-emitter')(CSVStream.prototype);
function CSVStream() {
var self = this;
this.open = function(url) {
var stream = new XHRStream({url: url});
var head = true;
var row = [];
var pos = 0;
var cell = '';
stream.on('data', function(res) {
var i = 0;
var len = res.length;
var ch;
for (; i < len; i++) {
ch = res[i];
if (ch === ',') {
row[pos++] = cell.trim();
cell = '';
continue;
}
if (ch === '\n') {
row[pos++] = cell.trim();
self.emit('data', row, head);
row.length = 0;
pos = 0;
cell = '';
head = false;
continue;
}
cell += ch;
}
});
stream.on('end', function() {
self.emit('end');
});
};
this.dispose = function() {
self.removeAllListeners();
};
}