Permalink
Browse files

Add support for int, float and date in transform

  • Loading branch information...
1 parent 7eff47f commit 56b1248ccd4fac485d99be31904c3f3dd39eef42 @wdavidw committed Sep 26, 2010
Showing with 32 additions and 5 deletions.
  1. +6 −2 lib/csv.js
  2. +1 −0 package.json
  3. +1 −1 sample/columns.js
  4. +1 −1 sample/sample.js
  5. +1 −1 sample/transform.js
  6. +18 −0 test/transform.js
  7. +2 −0 test/transform/types.in
  8. +2 −0 test/transform/types.out
View
@@ -219,8 +219,6 @@ module.exports = function(){
var line = csv.transformer?csv.transformer(state.line,state.count):state.line;
if(line !== null){
csv.emit('data',line,state.count);
- }
- if(line !== null){
if(typeof line === 'object'){
if(!(line instanceof Array)){
var columns = csv.writeOptions.columns||csv.readOptions.columns;
@@ -240,6 +238,12 @@ module.exports = function(){
if(line instanceof Array){
var newLine = state.count?csv.writeOptions.lineBreaks:'';
line.forEach(function(field,i){
+ if(typeof field == 'string'){
+ }else if(typeof field == 'number'){
+ field = ''+field;
+ }else if(field instanceof Date){
+ field = ''+field.getTime();
+ }
var containsdelimiter = field.indexOf(csv.writeOptions.delimiter||csv.readOptions.delimiter)>=0;
var containsQuote = field.indexOf(csv.writeOptions.quote||csv.readOptions.quote)>=0;
if(containsQuote){
View
@@ -2,6 +2,7 @@
, "version": "0.0.1"
, "description": "CSV parser with simple api, full of options and tested against large datasets."
, "author": "David Worms <david@adaltas.com>"
+, "directories" : { "lib" : "./lib" }
, "main": "./lib/csv"
, "engines": { "node": ">= 0.1.90" }
}
View
@@ -18,4 +18,4 @@
// Will print sth like:
// 82,Zbigniew Preisner
- // 94,Serge Gainsbourg
+ // 94,Serge Gainsbourg
View
@@ -19,4 +19,4 @@
})
.on('error',function(error){
console.log(error.message);
- });
+ });
View
@@ -12,4 +12,4 @@
});
// Print sth like:
- // 82:Zbigniew Preisner,94:Serge Gainsbourg
+ // 82:Zbigniew Preisner,94:Serge Gainsbourg
View
@@ -78,5 +78,23 @@ module.exports = {
);
fs.unlink(__dirname+'/transform/string.tmp');
});
+ },
+ 'Test types': function(assert){
+ // Test date, int and float
+ csv()
+ .fromPath(__dirname+'/transform/types.in')
+ .toPath(__dirname+'/transform/types.tmp')
+ .transform(function(data,index){
+ data[3] = data[3].split('-');
+ return [parseInt(data[0]),parseFloat(data[1]),parseFloat(data[2]),new Date(data[3][0],data[3][1],data[3][2])];
+ })
+ .on('end',function(count){
+ assert.strictEqual(2,count);
+ assert.equal(
+ fs.readFileSync(__dirname+'/transform/types.out').toString(),
+ fs.readFileSync(__dirname+'/transform/types.tmp').toString()
+ );
+ fs.unlink(__dirname+'/transform/types.tmp');
+ });
}
}
View
@@ -0,0 +1,2 @@
+20322051544,8.8017226E7,4.5,1978-10-09
+28392898392,8.8392926E7,8.3,2000-01-01
View
@@ -0,0 +1,2 @@
+20322051544,88017226,4.5,279414000000
+28392898392,88392926,8.3,949359600000

0 comments on commit 56b1248

Please sign in to comment.