Skip to content

Commit

Permalink
Return empty string for missing columns, instead of undefined
Browse files Browse the repository at this point in the history
Reverses one test, so technically we can say that it breaks the API. However README is silent about this situation, so I don't know.

Fixes #48
  • Loading branch information
Fil committed Jul 22, 2019
1 parent a9354a7 commit 525627a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/dsv.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var EOL = {},

function objectConverter(columns) {
return new Function("d", "return {" + columns.map(function(name, i) {
return JSON.stringify(name) + ": d[" + i + "]";
return JSON.stringify(name) + ": d[" + i + "] || \"\"";
}).join(",") + "}");
}

Expand Down
8 changes: 4 additions & 4 deletions test/csv-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ tape("csvParse(string) ignores a blank last line", function(test) {
});

tape("csvParse(string) treats a blank non-last line as a single-column empty string", function(test) {
test.deepEqual(dsv.csvParse("a,b,c\n1,2,3\n\n"), table([{a: "1", b: "2", c: "3"}, {a: "", b: undefined, c: undefined}], ["a", "b", "c"]));
test.deepEqual(dsv.csvParse("a,b,c\n1,2,3\n\n"), table([{a: "1", b: "2", c: "3"}, {a: "", b: "", c: ""}], ["a", "b", "c"]));
test.end();
});

tape("csvParse(string) returns undefined values for missing columns", function(test) {
test.deepEqual(dsv.csvParse("a,b,c\n1\n1,2"), table([{a: "1", b: undefined, c: undefined}, {a: "1", b: "2", c: undefined}], ["a", "b", "c"]));
tape("csvParse(string) returns empty strings for missing columns", function(test) {
test.deepEqual(dsv.csvParse("a,b,c\n1\n1,2"), table([{a: "1", b: "", c: ""}, {a: "1", b: "2", c: ""}], ["a", "b", "c"]));
test.end();
});

tape("csvParse(string) does not ignore a whitespace-only last line", function(test) {
test.deepEqual(dsv.csvParse("a,b,c\n1,2,3\n "), table([{a: "1", b: "2", c: "3"}, {a: " ", b: undefined, c: undefined}], ["a", "b", "c"]));
test.deepEqual(dsv.csvParse("a,b,c\n1,2,3\n "), table([{a: "1", b: "2", c: "3"}, {a: " ", b: "", c: ""}], ["a", "b", "c"]));
test.end();
});

Expand Down

0 comments on commit 525627a

Please sign in to comment.