-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_arbitrary.js
executable file
·112 lines (97 loc) · 2.55 KB
/
parse_arbitrary.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
#!/usr/bin/env nodejs
var fs = require('fs');
var path = require('path');
var csvParse = require('csv-parse');
// returns true if any of the values in a hash equal the specified value
function hasValue(h, val) {
var key;
for(key in h) {
if(h[key] == val) {
return true;
}
}
return false;
}
function keyifi(str) {
return str.replace(/[^\w\d]+/g, '_').toLowerCase();
}
function clean(str) {
if(str.replace(/\s+/g, '') == '') {
return null;
}
// remove leading and trailing whitespace
str = str.replace(/^\s+/, '').replace(/\s+$/, '');
return str;
}
function LineParser() {
var keys = null;
var o, fields, j;
return function(i, line, callback) {
if(!keys) {
keys = line;
for(j=0; j < keys.length; j++) {
keys[j] = keyifi(keys[j]);
}
return;
}
o = {};
fields = line;
var cleaned;
for(j=0; j < fields.length; j++) {
if(keys[j]) {
if(fields[j]) {
cleaned = clean(fields[j]);
if(cleaned) {
o[keys[j]] = cleaned;
}
} else {
o[keys[j]] = null;
}
}
}
// if(Object.keys(o).length > 0) {
callback(null, i, o);
// }
};
}
module.exports = function(inFile, callback, done_callback) {
var parser = csvParse()
var i = 1;
var readyForNext = true;
var weAreDone = false;
var doneCallbackCalled = false;
var parseLine = new LineParser();
parser.on('readable', function() {
if(!readyForNext) {
return;
}
var line = parser.read();
if(!line) {
if(weAreDone && !doneCallbackCalled) {
done_callback();
doneCallbackCalled = true;
}
return;
}
if(i > 1) {
readyForNext = false;
}
parseLine(i, line, function(err, line, person) {
callback(err, line, person, function() {
readyForNext = true;
parser.emit('readable');
});
});
i++;
});
parser.on('error', function(err) {
parser.end();
callback("Parser error: " + err);
});
parser.on('end', function() {
weAreDone = true;
parser.emit('readable');
});
var inStream = fs.createReadStream(inFile, {encoding: 'utf8'});
inStream.pipe(parser);
}