Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

writeOptions.newColumns #32

Merged
merged 1 commit into from Jun 20, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions lib/csv.js
Expand Up @@ -54,6 +54,7 @@ module.exports = function(){
flags: 'w',
encoding: 'utf8',
bufferSize: null,
newColumns: false,
end: true // Call `end()` on close
};
// A boolean that is true by default, but turns false after an 'error' occurred,
Expand Down Expand Up @@ -334,17 +335,24 @@ module.exports = function(){
state.line = line;
line = null;
}
if(state.count === 0 && csv.writeOptions.header === true){
write(csv.writeOptions.columns || csv.readOptions.columns);
}
var line;
if(csv.transformer){
transforming = true;
line = csv.transformer(state.line, state.count);

if (csv.writeOptions.newColumns && !csv.writeOptions.columns && typeof line === 'object' && !Array.isArray(line)) {
Object.keys(line)
.filter(function(column) { return csv.readOptions.columns.indexOf(column) === -1; })
.forEach(function(column) { csv.readOptions.columns.push(column); });
}

transforming = false;
}else{
line = state.line;
}
if(state.count === 0 && csv.writeOptions.header === true){
write(csv.writeOptions.columns || csv.readOptions.columns);
}
write(line);
state.count++;
state.line = [];
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "csv",
"version": "0.0.13",
"version": "0.0.14",
"description": "CSV parser with simple api, full of options and tested against large datasets.",
"author": "David Worms <david@adaltas.com>",
"contributors": [
Expand Down
5 changes: 5 additions & 0 deletions readme.md
Expand Up @@ -156,6 +156,11 @@ Options are:
- *end*
Prevent calling `end` on the destination, so that destination is no longer writable, similar to passing `{end: false}` option in `stream.pipe()`.

- *newColumns*
If the `columns` option is not specified (which means columns will be taken from the reader
options, will automatically append new columns if they are added during `transform()`.


Transforming data
-----------------

Expand Down
3 changes: 3 additions & 0 deletions samples/new-columns.in
@@ -0,0 +1,3 @@
id,lastname,firstname
82,Preisner,Zbigniew
94,Gainsbourg,Serge
22 changes: 22 additions & 0 deletions samples/new-columns.js
@@ -0,0 +1,22 @@

// CSV sample - Copyright David Worms <open@adaltas.com> (BSD Licensed)

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

csv()
.fromPath(__dirname+'/columns.in',{
columns: true
})
.toStream(process.stdout, {
newColumns: true,
end: false
})
.transform(function(data){
data.name = data.firstname + ' ' + data.lastname
return data;
});

// Will print sth like:
// 82,Preisner,Zbigniew,Zbigniew Preisner
// 94,Gainsbourg,Serge,Serge Gainsbourg
16 changes: 15 additions & 1 deletion test/columns.coffee
Expand Up @@ -90,7 +90,21 @@ describe 'columns', ->
result.should.eql expect
fs.unlink "#{__dirname}/columns/out_named.tmp"
next()

it 'should emit new columns in output', (next) ->
csv()
.fromPath("#{__dirname}/columns/out_new.in", columns: true)
.toPath("#{__dirname}/columns/out_new.tmp", newColumns: true, header: true)
.transform (data) ->
data.should.be.an.a 'object'
data.FIELD_7 = 'new_field'
data
.on 'end', (count) ->
count.should.eql 2
expect = fs.readFileSync("#{__dirname}/columns/out_new.out").toString()
result = fs.readFileSync("#{__dirname}/columns/out_new.tmp").toString()
result.should.eql expect
fs.unlink "#{__dirname}/columns/out_new.tmp"
next()



Expand Down
3 changes: 3 additions & 0 deletions test/columns/out_new.in
@@ -0,0 +1,3 @@
FIELD_1,FIELD_2,FIELD_3,FIELD_4,FIELD_5,FIELD_6
20322051544,1979,8.8017226E7,ABC,45,2000-01-01
28392898392,1974,8.8392926E7,DEF,23,2050-11-27
3 changes: 3 additions & 0 deletions test/columns/out_new.out
@@ -0,0 +1,3 @@
FIELD_1,FIELD_2,FIELD_3,FIELD_4,FIELD_5,FIELD_6,FIELD_7
20322051544,1979,8.8017226E7,ABC,45,2000-01-01,new_field
28392898392,1974,8.8392926E7,DEF,23,2050-11-27,new_field