Skip to content

Commit

Permalink
Merge pull request #55 from doug-martin/master
Browse files Browse the repository at this point in the history
v0.4.3
  • Loading branch information
doug-martin committed Aug 14, 2014
2 parents c84294f + 64fb355 commit 5b9af7a
Show file tree
Hide file tree
Showing 7 changed files with 486 additions and 72 deletions.
6 changes: 6 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# v0.4.3

* Added ability to include a `rowDelimiter` at the end of a csv with the `includeEndRowDelimiter` optioin [#54](https://github.com/C2FO/fast-csv/issues/54)
* Added escaping for values that include a row delimiter
* Added more tests for new feature and escaping row delimiter values.

# v0.4.2

* Added ability to specify a rowDelimiter when creating a csv.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ Formatting accepts the same options as parsing with an additional `transform` op
* `transform(row)`: A function that accepts a row and returns a transformed one to be written.
* `rowDelimiter='\n'`: Specify an alternate row delimiter (i.e `\r\n`)
* `includeEndRowDelimiter=false`: Set to `true` to include a row delimiter at the end of the csv.
**`createWriteStream(options)`**
Expand Down
6 changes: 6 additions & 0 deletions docs/History.html
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@



<h1>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> optioin <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>
<ul>
<li>Added ability to specify a rowDelimiter when creating a csv.</li>
Expand Down
1 change: 1 addition & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ <h3>Formatting</h3>
<ul>
<li><code>transform(row)</code>: A function that accepts a row and returns a transformed one to be written.</li>
<li><code>rowDelimiter=&#39;\n&#39;</code>: Specify an alternate row delimiter (i.e <code>\r\n</code>)</li>
<li><code>includeEndRowDelimiter=false</code>: Set to <code>true</code> to include a row delimiter at the end of the csv.</li>
</ul>
<p><strong><code>createWriteStream(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>
Expand Down
20 changes: 15 additions & 5 deletions lib/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ var fs = require("fs"),
util = require("util"),
extended = require("./extended"),
isUndefinedOrNull = extended.isUndefinedOrNull,
escape = extended.escape,
hash = extended.hash,
has = extended.has,
stream = require("stream"),
Transform = stream.Transform,
LINE_BREAK = extended.LINE_BREAK;

function createFormatter(options) {
options = options || {};
var delimiter = options.delimiter || ",",
ESCAPE_REGEXP = new RegExp("[" + delimiter + "\\r\\n']"),
ESCAPE_REGEXP = new RegExp("[" + delimiter + escape(options.rowDelimiter || LINE_BREAK) + "']"),
QUOTE = options.quote || '"',
ESCAPE = options.escape || '"',
REPLACE_REGEXP = new RegExp(QUOTE, "g");
Expand Down Expand Up @@ -50,9 +52,10 @@ function defaultTransform(row) {
function __write(writer, arr, options) {
options = options || {};
var formatter = createFormatter(options),
transformer = extended.has(options, "transform") ? options.transform : defaultTransform,
hasHeaders = extended.has(options, "headers") ? options.headers : true,
transformer = has(options, "transform") ? options.transform : defaultTransform,
hasHeaders = has(options, "headers") ? options.headers : true,
rowDelimiter = options.rowDelimiter || LINE_BREAK,
includeEndRowDelimiter = !!options.includeEndRowDelimiter,
headersLength = 0,
i = -1,
j = -1,
Expand Down Expand Up @@ -89,6 +92,9 @@ function __write(writer, arr, options) {
ret.push(formatter(vals));
}
writer.push(ret.join(rowDelimiter));
if (includeEndRowDelimiter) {
writer.push(rowDelimiter);
}
}
}

Expand All @@ -99,11 +105,12 @@ function CsvTransformStream(options) {
Transform.call(this, options);
this.formatter = createFormatter(options);
this.rowDelimiter = options.rowDelimiter || "\n";
var hasHeaders = this.hasHeaders = extended.has(options, "headers") ? options.headers : true;
var hasHeaders = this.hasHeaders = has(options, "headers") ? options.headers : true;
this.parsedHeaders = hasHeaders ? false : true;
this.buffer = [];
this.maxBufferSize = options.maxBuffer || 100000;
extended.has(options, "transform") && this.transform(options.transform);
this.includeEndRowDelimiter = !!options.includeEndRowDelimiter,
has(options, "transform") && this.transform(options.transform);
}

util.inherits(CsvTransformStream, Transform);
Expand Down Expand Up @@ -154,6 +161,9 @@ extended(CsvTransformStream).extend({
}
} else {
if (buffer.length) {
if (this.includeEndRowDelimiter) {
buffer.push(this.rowDelimiter);
}
transformStreamWrite.call(this, new Buffer(buffer.join("")).toString("utf8"));
buffer.length = 0;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fast-csv",
"version": "0.4.2",
"version": "0.4.3",
"description": "CSV parser and writer",
"main": "index.js",
"scripts": {
Expand Down
Loading

0 comments on commit 5b9af7a

Please sign in to comment.