Support for mapHeaders to filter columns and edit the printed headers#28
Support for mapHeaders to filter columns and edit the printed headers#28hmalphettes wants to merge 1 commit intomax-mapper:masterfrom sutoiku:master
Conversation
Setting the mapHeaders function in the options will skip some columns
and edit the printed headers line.
For example:
```
var writer = csv({
mapHeaders: function (header) {
return header === 'skipthisone' ? null
: header.substring(0,1).toUpperCase() + header.substring(1)
}
})
```
This the reverse of csv-parser's `mapHeaders`
|
Having a function to edit the headers seems overly complex... you could just pipe the stream of objects into a transform that rewrites the keys or removes keys you don't want before sending them to csv-write-stream. |
|
@slang800 ok, that works too. I would argue that no matter what that transform stream will cause some overhead. If one wants to optimise such a transform stream one would need to redo the same type of work than what the csv-write-stream compiler is doing:
So at this point all that is left is the csv formatting and one would end-up with csv-write-stream Hence I think there is value in handling this transform in the csv-write-stream itself. |
|
It's not complex and you shouldn't need to extract the headers or redo any work. It should look something like this: var map = require('through2')
var transform = map({objectMode: true}, function (obj, enc, cb) {
var i, key, len, newKey, keys
delete obj.skipthisone
keys = Object.keys(obj)
for (i = 0, len = keys.length; i < len; i++) {
key = keys[i]
newKey = key.substring(0, 1).toUpperCase() + key.substring(1)
obj[newKey] = obj[key]
delete obj[key]
}
cb(null, obj)
})...Unless I'm totally misunderstanding what you want to do. |
|
Thanks @slang800 The csv-write-stream could have been written in the same fashion than what you suggest: Instead it produces a block of javascript code in the That is benchmarked to be much faster than making V8 discover the object it transforms with For example this is what it generates for So I am trying to make sure that transforming the headers and skipping columns wont be a perf bottleneck by incorporating that into the writer. This PR does that for an extra O(1) and adds zero overhead to the serialisation itself. |
Setting the mapHeaders function in the options will skip some columns
and edit the printed headers line.
For example:
This the reverse of csv-parser's
mapHeaders:mafintosh/csv-parser#54