Skip to content

Commit

Permalink
Merge pull request #45 from mdoelker/master
Browse files Browse the repository at this point in the history
Added discardUnmappedColumns option that throws away extra columns that ...
  • Loading branch information
doug-martin committed Aug 3, 2014
2 parents 850fb75 + f6de9ac commit 9273b9b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ All methods accept the following `options`
* `objectMode=true`: Ensure that `data` events have an object emitted rather than the stringified version set to false to have a stringified buffer.
* `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.
* `ignoreEmpty=false`: If you wish to ignore empty rows.
* `discardUnmappedColumns=false`: If you want to discard columns that do not map to a header.
* `delimiter=','`: If your data uses an alternate delimiter such as `;` or `\t`.
* **NOTE** When specifying an alternate `delimiter` you may only pass in a single character delimiter
* `quote='"'`: The character to use to escape values that contain a delimiter.
Expand Down Expand Up @@ -473,7 +474,7 @@ var formatStream = csv
emailAddress: obj.Email_Address,
verified: obj.Verified
};
});
});
csv
.fromPath("in.csv", {headers: true})
.pipe(formatStream)
Expand Down
10 changes: 8 additions & 2 deletions lib/parser_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function ParserStream(options) {
this.parser = createParser(options);
this._headers = options.headers;
this._ignoreEmpty = options.ignoreEmpty;
this._discardUnmappedColumns = options.discardUnmappedColumns;
this.__objectMode = options.objectMode;
this.__buffered = [];
return this;
Expand Down Expand Up @@ -111,6 +112,7 @@ extended(ParserStream).extend({
rows = data.rows;
if (!this._parsedHeaders) {
var headers = this._headers;
var discardUnmappedColumns = this._discardUnmappedColumns;
if (extended.isBoolean(headers) && headers) {
headers = this.__handleLine(rows.shift(), 0, true);
}
Expand All @@ -120,7 +122,11 @@ extended(ParserStream).extend({
this.__transform = function (data, index) {
var ret = {}, i = -1, val;
if (data.length > headersLength) {
self.emit("error", new Error("Unexpected Error: column header mismatch expected: " + headersLength + " columns got: " + data.length));
if (discardUnmappedColumns) {
data.splice(headersLength);
} else {
self.emit("error", new Error("Unexpected Error: column header mismatch expected: " + headersLength + " columns got: " + data.length));
}
}
while (++i < headersLength) {
val = data[i];
Expand Down Expand Up @@ -268,4 +274,4 @@ extended(ParserStream).extend({

});

module.exports = ParserStream;
module.exports = ParserStream;
4 changes: 4 additions & 0 deletions test/assets/test23.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
first_name,last_name,email_address
First1,Last1,[email protected]
First2,Last2,[email protected],Unmapped1
First3,Last3,[email protected],Unmapped1,Unmapped2
27 changes: 24 additions & 3 deletions test/fast-csv.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,14 @@ var expected14 = [

var expected21 = [
{"first_name": "First\n1", "last_name": "Last\n1", "email_address": "[email protected]", address: "1 Street St,\nState ST, 88888"},
{"first_name": "First\n2", "last_name": "Last\n2", "email_address": "[email protected]", address: "2 Street St,\nState ST, 88888"},
]
{"first_name": "First\n2", "last_name": "Last\n2", "email_address": "[email protected]", address: "2 Street St,\nState ST, 88888"}
];

var expected23 = [
{"first_name": "First1", "last_name": "Last1", "email_address": "[email protected]"},
{"first_name": "First2", "last_name": "Last2", "email_address": "[email protected]"},
{"first_name": "First3", "last_name": "Last3", "email_address": "[email protected]"}
];

it.describe("fast-csv", function (it) {

Expand Down Expand Up @@ -461,6 +467,21 @@ it.describe("fast-csv", function (it) {
});
});

it.should("discard extra columns that do not map to a header with discardUnmappedColumns option", function (next) {
var actual = [];
csv
.fromPath(path.resolve(__dirname, "./assets/test23.csv"), {headers: true, discardUnmappedColumns: true})
.on("record", function (data, index) {
actual.push(data);
})
.on("error", next)
.on("end", function (count) {
assert.deepEqual(actual, expected23);
assert.equal(count, actual.length);
next();
});
});

it.describe("alternate delimiters", function (it) {
it.should("support tab delimiters", function (next) {
var actual = [];
Expand Down Expand Up @@ -1000,4 +1021,4 @@ it.describe("fast-csv", function (it) {
});
});
});
});
});

0 comments on commit 9273b9b

Please sign in to comment.