Skip to content

Commit

Permalink
Handle boolean; update column sample
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Feb 12, 2012
1 parent 896baed commit c339184
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 20 deletions.
3 changes: 3 additions & 0 deletions lib/csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ module.exports = function(){
}else if(typeof field === 'number'){
// Cast number to string
field = '' + field;
}else if(typeof field === 'boolean'){
// Cast number to string
field = field ? '1' : '';
}else if(field instanceof Date){
// Cast date to timestamp string
field = '' + field.getTime();
Expand Down
7 changes: 4 additions & 3 deletions samples/columns.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
// CSV sample - Copyright David Worms <open@adaltas.com> (BSD Licensed)

// node samples/column.js
var csv = require('csv');
var csv = require('..');

csv()
.fromPath(__dirname+'/columns.in',{
columns: true
})
.toStream(process.stdout,{
columns: ['id', 'name']
.toStream(process.stdout, {
columns: ['id', 'name'],
end: false
})
.transform(function(data){
data.name = data.firstname + ' ' + data.lastname
Expand Down
26 changes: 14 additions & 12 deletions samples/duplicate-headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,28 @@
// every other data line in the file.
//

var csv = require('csv'),
header;
var csv = require('..');
var header;

process.stdin.resume();
csv()

csv()
.fromStream(process.stdin)
.toStream(process.stdout, {end: false})
.transform(function(data){
if (header) {
this.write(header);
} else {
header=data;
return null;
}
return data;
if (header) {
this.write(header);
} else {
header=data;
return null;
}
return data;
})
.on('end',function(error){
process.stdout.write("\n");
process.stdout.write("\n");
})
.on('error',function(error){
console.log(error.message);
console.log(error.message);
});

//
Expand Down
2 changes: 1 addition & 1 deletion test/transform.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ module.exports =
.toPath("#{__dirname}/transform/types.tmp")
.transform( (data, index) ->
data[3] = data[3].split('-')
return [parseInt(data[0]), parseFloat(data[1]), parseFloat(data[2]) ,Date.UTC(data[3][0], data[3][1], data[3][2])]
return [parseInt(data[0]), parseFloat(data[1]), parseFloat(data[2]) ,Date.UTC(data[3][0], data[3][1], data[3][2]), !!data[4], !!data[5]]
)
.on 'end', (count) ->
assert.strictEqual(2,count)
Expand Down
4 changes: 2 additions & 2 deletions test/transform/types.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
20322051544,8.8017226E7,4.5,1978-10-09
28392898392,8.8392926E7,8.3,2000-01-01
20322051544,8.8017226E7,4.5,1978-10-09,,1
28392898392,8.8392926E7,8.3,2000-01-01,1,
4 changes: 2 additions & 2 deletions test/transform/types.out
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
20322051544,88017226,4.5,279417600000
28392898392,88392926,8.3,949363200000
20322051544,88017226,4.5,279417600000,,1
28392898392,88392926,8.3,949363200000,1,

0 comments on commit c339184

Please sign in to comment.