Skip to content

Commit a3f772a

Browse files
committed
Merge pull request #39 from doug-martin/master
v0.3.1
2 parents 6118ebb + f755691 commit a3f772a

File tree

10 files changed

+542
-128
lines changed

10 files changed

+542
-128
lines changed

Gruntfile.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@ module.exports = function (grunt) {
44
grunt.initConfig({
55
pkg: grunt.file.readJSON('package.json'),
66

7+
exec: {
8+
removeDocs: "rm -rf docs/*",
9+
createDocs: 'coddoc -f multi-html -d ./lib --dir ./docs'
10+
},
11+
712
jshint: {
8-
file: "./lib/index.js",
13+
file: "./lib/*.js",
914
options: {
1015
jshintrc: '.jshintrc'
1116
}
@@ -19,12 +24,13 @@ module.exports = function (grunt) {
1924
reporter: 'spec'
2025
}
2126
}
22-
},
27+
}
2328
});
2429

2530
// Default task.
26-
grunt.registerTask('default', ['jshint', 'it']);
31+
grunt.registerTask('default', ['jshint', 'it', "exec"]);
2732
grunt.loadNpmTasks('grunt-it');
2833
grunt.loadNpmTasks('grunt-contrib-jshint');
34+
grunt.loadNpmTasks('grunt-exec');
2935

3036
};

History.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# v0.3.1
2+
3+
* Added transform support to formatters
4+
* When using `createWriteStream` you can now you the `transform` method to specify a row transformer.
5+
* When using other transform methods you can specify a `transform` option.
6+
17
# v0.3.0
28

39
* You can now specify `objectMode` when parsing a csv which will cause `data` events to have an object emitted.

Makefile

Lines changed: 0 additions & 32 deletions
This file was deleted.

README.md

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,9 @@ csv
212212
213213
`fast-csv` also allows to you to create create a `CSV` from data.
214214
215-
Formatting accepts the same options as parsing.
215+
Formatting accepts the same options as parsing with an additional `transform` option.
216+
217+
* `transform(row)`: A function that accepts a row and returns a transformed one to be written.
216218
217219
**`createWriteStream(options)`**
218220
@@ -235,6 +237,32 @@ csvStream.write({a: "a3", b: "b4"});
235237
csvStream.write(null);
236238
```
237239
240+
If you wish to transform rows as writing then you can use the `.transform` method.
241+
242+
```javascript
243+
var csvStream = csv
244+
.createWriteStream({headers: true})
245+
.transform(function(row){
246+
return {
247+
A: row.a,
248+
B: row.b
249+
};
250+
}),
251+
writableStream = fs.createWriteStream("my.csv");
252+
253+
writableStream.on("finish", function(){
254+
console.log("DONE!");
255+
});
256+
257+
csvSream.pipe(writableStream);
258+
csvStream.write({a: "a0", b: "b0"});
259+
csvStream.write({a: "a1", b: "b1"});
260+
csvStream.write({a: "a2", b: "b2"});
261+
csvStream.write({a: "a3", b: "b4"});
262+
csvStream.write({a: "a3", b: "b4"});
263+
csvStream.write(null);
264+
```
265+
238266
**Writing Data**
239267
240268
Each of the following methods accept an array of values to be written, however each value must be an `array` of `array`s or `object`s.
@@ -264,6 +292,26 @@ csv
264292
.pipe(ws);
265293
```
266294
295+
```javascript
296+
var ws = fs.createWriteStream("my.csv");
297+
csv
298+
.write([
299+
{a: "a1", b: "b1"},
300+
{a: "a2", b: "b2"}
301+
], {
302+
headers: true
303+
transform: function(row){
304+
return {
305+
A: row.a,
306+
B: row.b
307+
};
308+
}
309+
})
310+
.pipe(ws);
311+
```
312+
313+
314+
267315
**`writeToStream(stream,arr[, options])`**
268316
269317
Write an array of values to a `WritableStream`
@@ -286,6 +334,23 @@ csv
286334
.pipe(ws);
287335
```
288336
337+
```javascript
338+
csv
339+
.writeToStream(fs.createWriteStream("my.csv"), [
340+
{a: "a1", b: "b1"},
341+
{a: "a2", b: "b2"}
342+
], {
343+
headers: true,
344+
transform: function(row){
345+
return {
346+
A: row.a,
347+
B: row.b
348+
};
349+
}
350+
})
351+
.pipe(ws);
352+
```
353+
289354
**`writeToPath(arr[, options])`**
290355
291356
Write an array of values to the specified path
@@ -313,6 +378,25 @@ csv
313378
});
314379
```
315380
381+
```javascript
382+
csv
383+
.writeToStream("my.csv", [
384+
{a: "a1", b: "b1"},
385+
{a: "a2", b: "b2"}
386+
], {
387+
headers: true,
388+
transform: function(row){
389+
return {
390+
A: row.a,
391+
B: row.b
392+
};
393+
}
394+
})
395+
.on("finish", function(){
396+
console.log("done!");
397+
});
398+
```
399+
316400
**`writeToString(arr[, options])`**
317401
318402
```javascript
@@ -330,6 +414,21 @@ csv.writeToString([
330414
], {headers: true}); //"a,b\na1,b1\na2,b2\n"
331415
```
332416
417+
```javascript
418+
csv.writeToString([
419+
{a: "a1", b: "b1"},
420+
{a: "a2", b: "b2"}
421+
], {
422+
headers: true,
423+
transform: function(row){
424+
return {
425+
A: row.a,
426+
B: row.b
427+
};
428+
}
429+
}); //"a,b\na1,b1\na2,b2\n"
430+
```
431+
333432
## Piping from Parser to Writer
334433
335434
You can use `fast-csv` to pipe the output from a parsed CSV to a transformed CSV by setting the parser to `objectMode` and using `createWriteStream`.
@@ -361,6 +460,27 @@ csv
361460
362461
The output will contain formatted result from the transform function.
363462
463+
If you want to tranform on the formatting side
464+
465+
466+
```javascript
467+
var formatStream = csv
468+
.createWriteStream({headers: true})
469+
.transform(function(){
470+
return {
471+
name: obj.Name,
472+
address: obj.Address,
473+
emailAddress: obj.Email_Address,
474+
verified: obj.Verified
475+
};
476+
});
477+
csv
478+
.fromPath("in.csv", {headers: true})
479+
.pipe(formatStream)
480+
.pipe(fs.createWriteStream("out.csv", {encoding: "utf8"}));
481+
```
482+
483+
364484
## Benchmarks
365485
366486
`Parsing 20000 records AVG over 3 runs`

docs/History.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,14 @@
176176

177177

178178

179+
<h1>v0.3.1</h1>
180+
<ul>
181+
<li>Added transform support to formatters<ul>
182+
<li>When using <code>createWriteStream</code> you can now you the <code>transform</code> method to specify a row transformer.</li>
183+
<li>When using other transform methods you can specify a <code>transform</code> option.</li>
184+
</ul>
185+
</li>
186+
</ul>
179187
<h1>v0.3.0</h1>
180188
<ul>
181189
<li>You can now specify <code>objectMode</code> when parsing a csv which will cause <code>data</code> events to have an object emitted.</li>

0 commit comments

Comments
 (0)