-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcsv-transform.js
39 lines (34 loc) · 903 Bytes
/
csv-transform.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
// node samples/sample.js
var csv = require('csv');
// String process function
function safeString(str) {
if(str == null) str = '';
return str.replace(/[&\/\\#,+()$~%'"*<>{}]/g,'');
}
// Parse email
function parseEmail(str){
var ary = str.split(' ');
return ary[ary.length - 1];
}
var Email = {
pattern: /([^<]+)\s<(.*)>/gi,
parse: function(text) {
return text.match(this.pattern);
}
};
csv()
.fromPath(__dirname+'/index.csv')
.toPath(__dirname+'/pulse-inbox.csv')
.transform(function(data){
//[safeString(data[0]), Email.parse(data[1]).join(' '), Email.parse(data[2]).join(' '), safeString(data[3]), safeString('body')];
return data;
})
.on('data',function(data,index){
console.log('#'+index+' '+JSON.stringify(data));
})
.on('end',function(count){
console.log('Number of lines: '+count);
})
.on('error',function(error){
console.log(error.message);
});