Skip to content

Commit ebb6881

Browse files
committed
Merge pull request #19 from doug-martin/master
v0.1.0
2 parents 45897fa + 4401b0a commit ebb6881

27 files changed

+1171631
-1454
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
language: node_js
22
node_js:
3-
- 0.8
43
- 0.1
54
before_script:
65
- npm install -g grunt-cli

README.md

Lines changed: 237 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,93 @@ edge cases such as multi line rows. However it does support escaped values, embe
1313

1414
## Usage
1515

16-
To parse a file.
16+
### Parsing
17+
18+
All methods accept the following `options`
19+
20+
* `headers=false`: Ste to true if you expect the first line of your `CSV` to contain headers, alternatly you can specify an array of headers to use.
21+
* `ignoreEmpty=false`: If you wish to ignore empty rows.
22+
* `delimiter=','`: If your data uses an alternate delimiter such as `;` or `\t`.
23+
* **NOTE** When specifying an alternate `delimiter` you may only pass in a single character delimeter
24+
25+
**events**
26+
27+
`parse-error`: Emitted if there was an error parsing a row.
28+
`record`: Emitted when a record is parsed.
29+
`data-invalid`: Emitted if there was invalid row encounted, **only emitted if the `validate` function is used**.
30+
`data`: Emitted with the `stringified` version of a record.
31+
32+
**([options])**
33+
34+
If you use `fast-csv` as a function it returns a transform stream that can be piped into.
35+
36+
```javascript
37+
var stream = fs.createReadStream("my.csv");
38+
39+
var csvStream = csv()
40+
.on("record", function(data){
41+
console.log(data):
42+
})
43+
.on("end", function(){
44+
console.log("done");
45+
});
46+
47+
stream.pipe(csvStream);
48+
```
49+
50+
**`.fromPath(path[, options])**
51+
52+
This method parses a file from the specified path.
1753

1854
```javascript
1955
var csv = require("fast-csv");
2056

21-
csv("my.csv")
22-
.on("data", function(data){
57+
csv
58+
.fromPath("my.csv")
59+
.on("record", function(data){
2360
console.log(data):
2461
})
2562
.on("end", function(){
2663
console.log("done");
64+
});
65+
```
66+
67+
**`.fromString(string[, options])**
68+
69+
This method parses a string
70+
71+
```javascript
72+
var csv = require("fast-csv");
73+
74+
var CSV_STRING = 'a,b\n' +
75+
'a1,b1\n' +
76+
'a2,b2\n';
77+
78+
csv
79+
.fromPath(CSV_STRING, {headers: true})
80+
.on("record", function(data){
81+
console.log(data):
2782
})
28-
.parse();
83+
.on("end", function(){
84+
console.log("done");
85+
});
2986
```
3087

31-
You may also parse a stream.
88+
**`.fromStream(stream[, options])**
89+
90+
This accepted a readable stream to parse data from.
3291

3392
```javascript
3493
var stream = fs.createReadStream("my.csv");
3594

36-
csv(stream)
37-
.on("data", function(data){
95+
csv()
96+
.fromStream(stream)
97+
.on("record", function(data){
3898
console.log(data):
3999
})
40100
.on("end", function(){
41101
console.log("done");
42-
})
43-
.parse();
44-
102+
});
45103
```
46104

47105
If you expect the first line your csv to headers you may pass a headers option in. Setting the headers option will
@@ -50,14 +108,14 @@ cause change each row to an object rather than an array.
50108
```javascript
51109
var stream = fs.createReadStream("my.csv");
52110

53-
csv(stream, {headers : true})
54-
.on("data", function(data){
111+
csv()
112+
.fromStream(stream, {headers : true})
113+
.on("record", function(data){
55114
console.log(data):
56115
})
57116
.on("end", function(){
58117
console.log("done");
59-
})
60-
.parse();
118+
});
61119

62120
```
63121

@@ -67,14 +125,14 @@ the data columns will not match.
67125
```javascript
68126
var stream = fs.createReadStream("my.csv");
69127

70-
csv(stream, {headers : ["firstName", "lastName", "address"]})
71-
.on("data", function(data){
128+
csv
129+
.fromStream(stream, {headers : ["firstName", "lastName", "address"]})
130+
.on("record", function(data){
72131
console.log(data):
73132
})
74133
.on("end", function(){
75134
console.log("done");
76-
})
77-
.parse();
135+
});
78136

79137
```
80138

@@ -86,14 +144,14 @@ Any rows consisting of nothing but empty strings and/or commas will be skipped,
86144
```javascript
87145
var stream = fs.createReadStream("my.csv");
88146

89-
csv(stream, {ignoreEmpty: true})
90-
.on("data", function(data){
147+
csv
148+
.fromStream(stream, {ignoreEmpty: true})
149+
.on("record", function(data){
91150
console.log(data):
92151
})
93152
.on("end", function(){
94153
console.log("done");
95-
})
96-
.parse();
154+
});
97155

98156
```
99157

@@ -105,20 +163,20 @@ will be emitted with the row and the index.
105163
```javascript
106164
var stream = fs.createReadStream("my.csv");
107165

108-
csv(stream, {headers : true})
166+
csv(
167+
.fromStream(stream, {headers : true})
109168
.validate(function(data){
110169
return data.age < 50; //all persons must be under the age of 50
111170
})
112171
.on("data-invalid", function(data){
113172
//do something with invalid row
114173
})
115-
.on("data", function(data){
174+
.on("record", function(data){
116175
console.log(data):
117176
})
118177
.on("end", function(){
119178
console.log("done");
120-
})
121-
.parse();
179+
});
122180

123181
```
124182
@@ -130,18 +188,153 @@ be provided to validate and emitted as a row.
130188
```javascript
131189
var stream = fs.createReadStream("my.csv");
132190

133-
csv(stream)
191+
csv
192+
.fromStream(stream)
134193
.transform(function(data){
135194
return data.reverse(); //reverse each row.
136195
})
137-
.on("data", function(data){
196+
.on("record", function(data){
138197
console.log(data):
139198
})
140199
.on("end", function(){
141200
console.log("done");
142-
})
143-
.parse();
201+
});
202+
203+
```
144204
205+
### Formatting
206+
207+
`fast-csv` also allows to you to create create a `CSV` from data.
208+
209+
In addition to the options for parsing you can specify the following additional options.
210+
211+
* `quote='"'`: The character to use to escape values that contain a delimeter.
212+
* `escape='"'`: The character to use when escaping a value that is `quoted` and constains a `quote` character.
213+
* `i.e`: 'First,"Name"' => '"First,""name"""'
214+
215+
**Writing Data**
216+
217+
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.
218+
219+
**`write(arr[, options])`**
220+
221+
Create a readable stream to read data from.
222+
223+
```javascript
224+
var ws = fs.createWritableStream("my.csv");
225+
csv
226+
.write([
227+
["a", "b"],
228+
["a1", "b1"],
229+
["a2", "b2"]
230+
], {headers: true})
231+
.pipe(ws);
232+
```
233+
234+
```javascript
235+
var ws = fs.createWritableStream("my.csv");
236+
csv
237+
.write([
238+
{a: "a1", b: "b1"},
239+
{a: "a2", b: "b2"}
240+
], {headers: true})
241+
.pipe(ws);
242+
```
243+
244+
**`writeToStream(stream,arr[, options])`**
245+
246+
Write an array of values to a `WritableStream`
247+
248+
```javascript
249+
csv
250+
.writeToStream(fs.createWritableStream("my.csv"), [
251+
["a", "b"],
252+
["a1", "b1"],
253+
["a2", "b2"]
254+
], {headers: true});
255+
```
256+
257+
```javascript
258+
csv
259+
.writeToStream(fs.createWritableStream("my.csv"), [
260+
{a: "a1", b: "b1"},
261+
{a: "a2", b: "b2"}
262+
], {headers: true})
263+
.pipe(ws);
264+
```
265+
266+
**`writeToPath(arr[, options])`**
267+
268+
Write an array of values to the specified path
269+
270+
```javascript
271+
csv
272+
.writeToPath("my.csv", [
273+
["a", "b"],
274+
["a1", "b1"],
275+
["a2", "b2"]
276+
], {headers: true})
277+
.on("finish", function(){
278+
console.log("done!");
279+
});
280+
```
281+
282+
```javascript
283+
csv
284+
.writeToStream("my.csv", [
285+
{a: "a1", b: "b1"},
286+
{a: "a2", b: "b2"}
287+
], {headers: true})
288+
.on("finish", function(){
289+
console.log("done!");
290+
});
291+
```
292+
293+
**`writeToString(arr[, options])`**
294+
295+
```javascript
296+
csv.writeToString([
297+
["a", "b"],
298+
["a1", "b1"],
299+
["a2", "b2"]
300+
], {headers: true}); //"a,b\na1,b1\na2,b2\n"
301+
```
302+
303+
```javascript
304+
csv.writeToString([
305+
{a: "a1", b: "b1"},
306+
{a: "a2", b: "b2"}
307+
], {headers: true}); //"a,b\na1,b1\na2,b2\n"
308+
```
309+
310+
## Benchmarks
311+
312+
`Parsing 20000 records AVG over 3 runs`
313+
314+
```
315+
fast-csv: 198.67ms
316+
csv: 525.33ms
317+
```
318+
319+
`Parsing 50000 records AVG over 3 runs`
320+
321+
```
322+
fast-csv: 441.33ms
323+
csv: 1291ms
324+
```
325+
326+
`Parsing 100000 records AVG over 3 runs`
327+
328+
```
329+
fast-csv: 866ms
330+
csv: 2773.33ms
331+
```
332+
333+
`Parsing 1000000 records AVG over 3 runs`
334+
335+
```
336+
fast-csv: 8562.67ms
337+
csv: 30030.67ms
145338
```
146339
147340
## License
@@ -153,5 +346,19 @@ MIT <https://github.com/C2FO/fast-csv/raw/master/LICENSE>
153346
* Website: <http://c2fo.com>
154347
* Twitter: [http://twitter.com/c2fo](http://twitter.com/c2fo) - 877.465.4045
155348

349+
##Namespaces
350+
351+
352+
353+
354+
355+
##Classes
356+
357+
358+
359+
360+
361+
362+
156363

157364

0 commit comments

Comments
 (0)