Skip to content

Commit

Permalink
Merge pull request #85 from TechnotronicOz/fixing_headers
Browse files Browse the repository at this point in the history
Fixed header ordering and csv not being created if no data supplied to stream
  • Loading branch information
doug-martin committed Jan 28, 2015
2 parents 51c3f1b + 79dfbc8 commit 4993c05
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 57 deletions.
4 changes: 4 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v0.5.5
* Fixed issues with ordering of headers when specifying headers in a write stream [#77](https://github.com/C2FO/fast-csv/pull/77)
* Fixed issue where headers were not being written if no data was supplied to write stream.

# v0.5.4

* Fixed issues with error handling and not registering an error handler on stream [#68](https://github.com/C2FO/fast-csv/issues/68)
Expand Down
47 changes: 26 additions & 21 deletions docs/History.html
Original file line number Diff line number Diff line change
Expand Up @@ -176,28 +176,33 @@



<h1>v0.5.4</h1>
<h1 id="v0-5-5">v0.5.5</h1>
<ul>
<li>Fixed issues with ordering of headers when specifying headers in a write stream <a href="https://github.com/C2FO/fast-csv/pull/77">#77</a></li>
<li>Fixed issue where headers were not being written if no data was supplied to write stream.</li>
</ul>
<h1 id="v0-5-4">v0.5.4</h1>
<ul>
<li>Fixed issues with error handling and not registering an error handler on stream <a href="https://github.com/C2FO/fast-csv/issues/68">#68</a></li>
<li>Added support for ignoring quoting while parsing <a href="https://github.com/C2FO/fast-csv/issues/75">#75</a></li>
</ul>
<h1>v0.5.3</h1>
<h1 id="v0-5-3">v0.5.3</h1>
<ul>
<li>Fixed issues with <code>v0.11</code> stream implementation <a href="https://github.com/C2FO/fast-csv/issues/73">#73</a></li>
<li>Fixed issues with <code>pause/resume</code> and data events in <code>v0.10</code> <a href="https://github.com/C2FO/fast-csv/issues/69">#69</a></li>
<li>Fixed the double invoking of done callback when parsing files <a href="https://github.com/C2FO/fast-csv/issues/68">#68</a></li>
<li>Refactored tests</li>
</ul>
<h1>v0.5.2</h1>
<h1 id="v0-5-2">v0.5.2</h1>
<ul>
<li>Fixed issue with <code>writeToString</code> and <code>writeToPath</code> examples <a href="https://github.com/C2FO/fast-csv/issues/64">#64</a></li>
<li>Fixed issue with creating a csv without headers <a href="https://github.com/C2FO/fast-csv/issues/63">#63</a></li>
</ul>
<h1>v0.5.1</h1>
<h1 id="v0-5-1">v0.5.1</h1>
<ul>
<li>Fixed issue where line data was not being passed between transforms in the parser_stream</li>
</ul>
<h1>v0.5.0</h1>
<h1 id="v0-5-0">v0.5.0</h1>
<ul>
<li>Added support for async transforms <a href="https://github.com/C2FO/fast-csv/issues/24">#24</a></li>
<li>Added support for async validation</li>
Expand All @@ -214,67 +219,67 @@ <h1>v0.5.0</h1>
<li>More tests</li>
<li>Code refactor and clean up</li>
</ul>
<h1>v0.4.4</h1>
<h1 id="v0-4-4">v0.4.4</h1>
<ul>
<li>Added support for comments. <a href="https://github.com/C2FO/fast-csv/issues/56">#56</a></li>
</ul>
<h1>v0.4.3</h1>
<h1 id="v0-4-3">v0.4.3</h1>
<ul>
<li>Added ability to include a <code>rowDelimiter</code> at the end of a csv with the <code>includeEndRowDelimiter</code> option <a href="https://github.com/C2FO/fast-csv/issues/54">#54</a></li>
<li>Added escaping for values that include a row delimiter</li>
<li>Added more tests for new feature and escaping row delimiter values.</li>
</ul>
<h1>v0.4.2</h1>
<h1 id="v0-4-2">v0.4.2</h1>
<ul>
<li>Added ability to specify a rowDelimiter when creating a csv.</li>
<li>Added discardUnmappedColumns option to allow the ignoring of extra data <a href="https://github.com/C2FO/fast-csv/pull/45">#45</a></li>
</ul>
<h1>v0.4.1</h1>
<h1 id="v0-4-1">v0.4.1</h1>
<ul>
<li>Fixed race condition that occurred if you called pause during a flush.</li>
</ul>
<h1>v0.4.0</h1>
<h1 id="v0-4-0">v0.4.0</h1>
<ul>
<li>Fixed misspelling of <code>delimiter</code> <a href="https://github.com/C2FO/fast-csv/issues/40">#40</a></li>
</ul>
<h1>v0.3.1</h1>
<h1 id="v0-3-1">v0.3.1</h1>
<ul>
<li>Added transform support to formatters<ul>
<li>When using <code>createWriteStream</code> you can now you the <code>transform</code> method to specify a row transformer.</li>
<li>When using other transform methods you can specify a <code>transform</code> option.</li>
</ul>
</li>
</ul>
<h1>v0.3.0</h1>
<h1 id="v0-3-0">v0.3.0</h1>
<ul>
<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>
<li>You can now pipe directly to the stream returned from <code>createWriteStream</code></li>
<li>You can now transform csvs by piping output from parsing into a formatter.</li>
</ul>
<h1>v0.2.5</h1>
<h1 id="v0-2-5">v0.2.5</h1>
<ul>
<li>Fixed issue where not all rows are emitted when using <code>pause</code> and <code>resume</code></li>
</ul>
<h1>v0.2.4</h1>
<h1 id="v0-2-4">v0.2.4</h1>
<ul>
<li>Added more fine grained control to <code>.pause</code> and <code>.resume</code><ul>
<li>You can now pause resume between chunks</li>
</ul>
</li>
</ul>
<h1>v0.2.3</h1>
<h1 id="v0-2-3">v0.2.3</h1>
<ul>
<li>Add new <code>createWriteStream</code> for creating a streaming csv writer</li>
</ul>
<h1>v0.2.2</h1>
<h1 id="v0-2-2">v0.2.2</h1>
<ul>
<li>Fixed issue with having line breaks containing <code>\r\n</code></li>
</ul>
<h1>v0.2.1</h1>
<h1 id="v0-2-1">v0.2.1</h1>
<ul>
<li>Fixed issue with <code>\r</code> line break in parser</li>
</ul>
<h1>v0.2.0</h1>
<h1 id="v0-2-0">v0.2.0</h1>
<ul>
<li>Added multiline value support</li>
<li>Updated escaping logic</li>
Expand All @@ -283,17 +288,17 @@ <h1>v0.2.0</h1>
<li>Removed support for having two quote types instead it just supports a single quote and escape sequence.
Source code (zip)</li>
</ul>
<h1>v0.1.2</h1>
<h1 id="v0-1-2">v0.1.2</h1>
<ul>
<li>Fixed issue with formatter handling undefined or null values.</li>
<li>Changed formatter not not include a new line at the end of a CSV.</li>
<li>Added pause and resume functionality to ParserStream</li>
</ul>
<h1>v0.1.1</h1>
<h1 id="v0-1-1">v0.1.1</h1>
<ul>
<li>Added trim, ltrim, and rtrim to parsing options</li>
</ul>
<h1>v0.1.0</h1>
<h1 id="v0-1-0">v0.1.0</h1>

<hr>

Expand Down
32 changes: 16 additions & 16 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,17 @@


<p><a href="http://travis-ci.org/C2FO/fast-csv"><img src="https://secure.travis-ci.org/C2FO/fast-csv.png" alt="build status"></a></p>
<h1>Fast-csv</h1>
<h1 id="fast-csv">Fast-csv</h1>
<p>This is a library that provides CSV parsing and formatting.</p>
<p><strong>NOTE</strong> As of v0.2.0 <code>fast-csv</code> supports multi-line values.</p>
<h2>Installation</h2>
<h2 id="installation">Installation</h2>
<p><code>npm install fast-csv</code></p>
<h2>Usage</h2>
<h3>Parsing</h3>
<h2 id="usage">Usage</h2>
<h3 id="parsing">Parsing</h3>
<p>All methods accept the following <code>options</code></p>
<ul>
<li><code>objectMode=true</code>: Ensure that <code>data</code> events have an object emitted rather than the stringified version set to false to have a stringified buffer.</li>
<li><code>headers=false</code>: Ste to true if you expect the first line of your <code>CSV</code> to contain headers, alternatly you can specify an array of headers to use.</li>
<li><code>headers=false</code>: Set to true if you expect the first line of your <code>CSV</code> to contain headers, alternatly you can specify an array of headers to use.</li>
<li><code>ignoreEmpty=false</code>: If you wish to ignore empty rows.</li>
<li><code>discardUnmappedColumns=false</code>: If you want to discard columns that do not map to a header.</li>
<li><code>delimiter=&#39;,&#39;</code>: If your data uses an alternate delimiter such as <code>;</code> or <code>\t</code>.<ul>
Expand Down Expand Up @@ -350,7 +350,7 @@ <h3>Parsing</h3>
.on(&quot;end&quot;, function(){
console.log(&quot;done&quot;);
});</code></pre>
<h3>Validating</h3>
<h3 id="validating">Validating</h3>
<p>You can validate each row in the csv by providing a validate handler. If a row is invalid then a <code>data-invalid</code> event
will be emitted with the row and the index.</p>
<pre class='prettyprint linenums lang-js'><code class="lang-javascript">var stream = fs.createReadStream(&quot;my.csv&quot;);
Expand Down Expand Up @@ -389,7 +389,7 @@ <h3>Validating</h3>
.on(&quot;end&quot;, function(){
console.log(&quot;done&quot;);
});</code></pre>
<h3>Transforming</h3>
<h3 id="transforming">Transforming</h3>
<p>You can transform data by providing a transform function. What is returned from the transform function will
be provided to validate and emitted as a row.</p>
<pre class='prettyprint linenums lang-js'><code class="lang-javascript">var stream = fs.createReadStream(&quot;my.csv&quot;);
Expand Down Expand Up @@ -419,7 +419,7 @@ <h3>Transforming</h3>
.on(&quot;end&quot;, function(){
console.log(&quot;done&quot;);
});</code></pre>
<h3>Formatting</h3>
<h3 id="formatting">Formatting</h3>
<p><code>fast-csv</code> also allows to you to create create a <code>CSV</code> from data.</p>
<p>Formatting accepts the same options as parsing with an additional <code>transform</code> option.</p>
<ul>
Expand All @@ -439,7 +439,7 @@ <h3>Formatting</h3>
</ul>
</li>
</ul>
<h3>Data Types</h3>
<h3 id="data-types">Data Types</h3>
<p>When creating a CSV <code>fast-csv</code> supports a few data formats.</p>
<p><strong><code>Objects</code></strong></p>
<p>You can pass in object to any formatter function if your csv requires headers the keys of the first object will be used as the header names.</p>
Expand Down Expand Up @@ -479,7 +479,7 @@ <h3>Data Types</h3>
//Generated CSV
//a,a,b,b,c,c
//a1,a2,b1,b2,c1,c2</code></pre>
<h3>Formatting Functions</h3>
<h3 id="formatting-functions">Formatting Functions</h3>
<p><strong><code>createWriteStream(options)</code> or <code>.format(options)</code></strong></p>
<p>This is the lowest level of the write methods, it creates a stream that can be used to create a csv of unknown size and pipe to an output csv.</p>
<pre class='prettyprint linenums lang-js'><code class="lang-javascript">var csvStream = csv.createWriteStream({headers: true}),
Expand Down Expand Up @@ -739,7 +739,7 @@ <h3>Formatting Functions</h3>
console.log(data); //&quot;A,B\na1,b1\na2,b2\n&quot;
}
);</code></pre>
<h2>Piping from Parser to Writer</h2>
<h2 id="piping-from-parser-to-writer">Piping from Parser to Writer</h2>
<p>You can use <code>fast-csv</code> to pipe the output from a parsed CSV to a transformed CSV by setting the parser to <code>objectMode</code> and using <code>createWriteStream</code>.</p>
<pre class='prettyprint linenums lang-js'><code class="lang-javascript">csv
.fromPath(&quot;in.csv&quot;, {headers: true})
Expand Down Expand Up @@ -774,10 +774,10 @@ <h2>Piping from Parser to Writer</h2>
.fromPath(&quot;in.csv&quot;, {headers: true})
.pipe(formatStream)
.pipe(fs.createWriteStream(&quot;out.csv&quot;, {encoding: &quot;utf8&quot;}));</code></pre>
<h2>Quoting Columns</h2>
<h2 id="quoting-columns">Quoting Columns</h2>
<p>Sometimes you may need to quote columns is certain ways in order meet certain requirements. <code>fast-csv</code> can quote columns and headers almost anyway you may need.</p>
<p><strong>Note</strong> in the following example we use <code>writeToString</code> but the options option are valid for any of the formatting methods.</p>
<h3><code>quoteColumns</code></h3>
<h3 id="-quotecolumns-"><code>quoteColumns</code></h3>
<pre class='prettyprint linenums lang-js'><code>//quote all columns including headers
var objectData = [{a: &quot;a1&quot;, b: &quot;b1&quot;}, {a: &quot;a2&quot;, b: &quot;b2&quot;}],
arrayData = [[&quot;a&quot;, &quot;b&quot;], [&quot;a1&quot;, &quot;b1&quot;], [&quot;a2&quot;, &quot;b2&quot;]];
Expand All @@ -800,7 +800,7 @@ <h3><code>quoteColumns</code></h3>
//a1,&quot;b1&quot;
//a2,&quot;b2&quot;
});</code></pre>
<h3><code>quoteHeaders</code></h3>
<h3 id="-quoteheaders-"><code>quoteHeaders</code></h3>
<pre class='prettyprint linenums lang-js'><code>//quote all columns including headers
var objectData = [{a: &quot;a1&quot;, b: &quot;b1&quot;}, {a: &quot;a2&quot;, b: &quot;b2&quot;}],
arrayData = [[&quot;a&quot;, &quot;b&quot;], [&quot;a1&quot;, &quot;b1&quot;], [&quot;a2&quot;, &quot;b2&quot;]];
Expand Down Expand Up @@ -832,9 +832,9 @@ <h3><code>quoteHeaders</code></h3>
//&quot;a1&quot;,&quot;b1&quot;
//&quot;a2&quot;,&quot;b2&quot;
});</code></pre>
<h2>License</h2>
<h2 id="license">License</h2>
<p>MIT <a href="https://github.com/C2FO/fast-csv/raw/master/LICENSE">https://github.com/C2FO/fast-csv/raw/master/LICENSE</a></p>
<h2>Meta</h2>
<h2 id="meta">Meta</h2>
<ul>
<li>Code: <code>git clone git://github.com/C2FO/fast-csv.git</code></li>
<li>Website: <a href="http://c2fo.com">http://c2fo.com</a></li>
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fast-csv",
"version": "0.5.4",
"version": "0.5.5",
"description": "CSV parser and writer",
"main": "index.js",
"scripts": {
Expand All @@ -26,8 +26,7 @@
"grunt-it": "~0.3.1",
"grunt": "~0.4.1",
"grunt-contrib-jshint": "~0.10.0",
"grunt-exec": "^0.4.5",
"event-stream": "^3.1.7"
"grunt-exec": "^0.4.5"
},
"engines": {
"node": ">=0.10"
Expand Down
15 changes: 0 additions & 15 deletions test/headersbug.test.js

This file was deleted.

46 changes: 44 additions & 2 deletions test/issues.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ it.describe("github issues", function (it) {
it.timeout(60000);

it.describe("#68", function (it) {

it.should("handle parse errors properly", function (next) {
var actual = [];
csv
Expand All @@ -29,4 +28,47 @@ it.describe("github issues", function (it) {
});
});
});
});

it.describe("#77", function (it) {
it.should("sort columns by order of headers defined", function (next) {
var writable = fs.createWriteStream(path.resolve(__dirname, "assets/test.csv"), {encoding: "utf8"}),
stream = csv.createWriteStream({headers: ["second", "first"]})
.on("error", next);

writable.on("finish", function () {
assert.equal(fs.readFileSync(path.resolve(__dirname, "assets/test.csv")).toString(), "second,first\n2,1");
fs.unlinkSync(path.resolve(__dirname, "assets/test.csv"));
next();
});

stream.pipe(writable);

[{first: "1", second: "2"}].forEach(function (item) {
stream.write(item);
});

stream.end();
});

it.should("write headers even with no data", function (next) {
var writable = fs.createWriteStream(path.resolve(__dirname, "assets/test.csv"), {encoding: "utf8"}),
stream = csv.createWriteStream({headers: ["first", "second"]})
.on("error", next);

writable.on("finish", function () {
assert.equal(fs.readFileSync(path.resolve(__dirname, 'assets/test.csv')).toString(), "first,second\n,");
fs.unlinkSync(path.resolve(__dirname, 'assets/test.csv'));
next();
});

stream.pipe(writable);

[{}].forEach(function (item) {
stream.write(item);
});

stream.end();

});
});
});

0 comments on commit 4993c05

Please sign in to comment.