Skip to content

Commit

Permalink
Add support for dsv.parseRow and dsv.parseValue
Browse files Browse the repository at this point in the history
  • Loading branch information
rdmurphy authored and mbostock committed Nov 16, 2019
1 parent 81a9a8f commit 437da9a
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 3 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ Equivalent to [dsvFormat](#dsvFormat)(",").[formatBody](#dsv_formatBody).

Equivalent to [dsvFormat](#dsvFormat)(",").[formatRows](#dsv_formatRows).

<a name="csvFormatRow" href="#csvFormatRow">#</a> d3.<b>csvFormatRow</b>(<i>row</i>) [<>](https://github.com/d3/d3-dsv/blob/master/src/csv.js "Source")

Equivalent to [dsvFormat](#dsvFormat)(",").[formatRow](#dsv_formatRow).

<a name="csvFormatValue" href="#csvFormatValue">#</a> d3.<b>csvFormatValue</b>(<i>value</i>) [<>](https://github.com/d3/d3-dsv/blob/master/src/csv.js "Source")

Equivalent to [dsvFormat](#dsvFormat)(",").[formatValue](#dsv_formatValue).

<a name="tsvParse" href="#tsvParse">#</a> d3.<b>tsvParse</b>(<i>string</i>[, <i>row</i>]) [<>](https://github.com/d3/d3-dsv/blob/master/src/tsv.js "Source")

Equivalent to [dsvFormat](#dsvFormat)("\t").[parse](#dsv_parse).
Expand All @@ -83,6 +91,14 @@ Equivalent to [dsvFormat](#dsvFormat)("\t").[formatBody](#dsv_formatBody).

Equivalent to [dsvFormat](#dsvFormat)("\t").[formatRows](#dsv_formatRows).

<a name="tsvFormatRow" href="#tsvFormatRow">#</a> d3.<b>tsvFormatRow</b>(<i>row</i>) [<>](https://github.com/d3/d3-dsv/blob/master/src/tsv.js "Source")

Equivalent to [dsvFormat](#dsvFormat)("\t").[formatRow](#dsv_formatRow).

<a name="tsvFormatValue" href="#tsvFormatValue">#</a> d3.<b>tsvFormatValue</b>(<i>value</i>) [<>](https://github.com/d3/d3-dsv/blob/master/src/tsv.js "Source")

Equivalent to [dsvFormat](#dsvFormat)("\t").[formatValue](#dsv_formatValue).

<a name="dsvFormat" href="#dsvFormat">#</a> d3.<b>dsvFormat</b>(<i>delimiter</i>) [<>](https://github.com/d3/d3-dsv/blob/master/src/dsv.js)

Constructs a new DSV parser and formatter for the specified *delimiter*. The *delimiter* must be a single character (*i.e.*, a single 16-bit code unit); so, ASCII delimiters are fine, but emoji delimiters are not.
Expand Down Expand Up @@ -219,6 +235,14 @@ var string = d3.csvFormatRows([[
})));
```

<a name="dsv_formatRow" href="#dsv_formatRow">#</a> <i>dsv</i>.<b>formatRow</b>(<i>row</i>) [<>](https://github.com/d3/d3-dsv/blob/master/src/dsv.js "Source")

Formats a single array *row* of strings as delimiter-separated values, returning a string. Each column within the row will be separated by the delimiter (such as a comma, `,`). Values that contain either the delimiter, a double-quote (") or a newline will be escaped using double-quotes.

<a name="dsv_formatValue" href="#dsv_formatValue">#</a> <i>dsv</i>.<b>formatValue</b>(<i>value</i>) [<>](https://github.com/d3/d3-dsv/blob/master/src/dsv.js "Source")

Format a single *value* or string as a delimiter-separated value, returning a string. A value that contains either the delimiter, a double-quote (") or a newline will be escaped using double-quotes.

<a name="autoType" href="#autoType">#</a> d3.<b>autoType</b>(<i>object</i>) [<>](https://github.com/d3/d3-dsv/blob/master/src/autoType.js "Source")

Given an *object* (or array) representing a parsed row, infers the types of values on the *object* and coerces them accordingly, returning the mutated *object*. This function is intended to be used as a *row* accessor function in conjunction with [*dsv*.parse](#dsv_parse) and [*dsv*.parseRows](#dsv_parseRow). For example, consider the following CSV file:
Expand Down
2 changes: 2 additions & 0 deletions src/csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ export var csvParseRows = csv.parseRows;
export var csvFormat = csv.format;
export var csvFormatBody = csv.formatBody;
export var csvFormatRows = csv.formatRows;
export var csvFormatRow = csv.formatRow;
export var csvFormatValue = csv.formatValue;
4 changes: 3 additions & 1 deletion src/dsv.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ export default function(delimiter) {
parseRows: parseRows,
format: format,
formatBody: formatBody,
formatRows: formatRows
formatRows: formatRows,
formatRow: formatRow,
formatValue: formatValue
};
}
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export {default as dsvFormat} from "./dsv.js";
export {csvParse, csvParseRows, csvFormat, csvFormatBody, csvFormatRows} from "./csv.js";
export {tsvParse, tsvParseRows, tsvFormat, tsvFormatBody, tsvFormatRows} from "./tsv.js";
export {csvParse, csvParseRows, csvFormat, csvFormatBody, csvFormatRows, csvFormatRow, csvFormatValue} from "./csv.js";
export {tsvParse, tsvParseRows, tsvFormat, tsvFormatBody, tsvFormatRows, tsvFormatRow, tsvFormatValue} from "./tsv.js";
export {default as autoType} from "./autoType.js";
2 changes: 2 additions & 0 deletions src/tsv.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ export var tsvParseRows = tsv.parseRows;
export var tsvFormat = tsv.format;
export var tsvFormatBody = tsv.formatBody;
export var tsvFormatRows = tsv.formatRows;
export var tsvFormatRow = tsv.formatRow;
export var tsvFormatValue = tsv.formatValue;
5 changes: 5 additions & 0 deletions test/csv-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,8 @@ tape("csvFormatRows(array) escapes values containing delimiters", function(test)
test.deepEqual(dsv.csvFormatRows([["oxford,comma"]]), "\"oxford,comma\"");
test.end();
});

tape("csvFormatRow(array) takes a single array of string as input", function(test) {
test.deepEqual(dsv.csvFormatRow(["a", "b", "c"]), "a,b,c");
test.end();
});
5 changes: 5 additions & 0 deletions test/dsv-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,8 @@ tape("dsv(\"|\").formatRows(array) escapes values containing delimiters", functi
test.deepEqual(psv.formatRows([["oxford|tab"]]), "\"oxford|tab\"");
test.end();
});

tape("dsv(\"|\").formatRow(array) takes a single array of string as input", function(test) {
test.deepEqual(psv.formatRow(["a", "b", "c"]), "a|b|c");
test.end();
});
5 changes: 5 additions & 0 deletions test/tsv-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,8 @@ tape("tsvFormatRows(array) escapes values containing delimiters", function(test)
test.deepEqual(dsv.tsvFormatRows([["oxford\ttab"]]), "\"oxford\ttab\"");
test.end();
});

tape("tsvFormatRow(array) takes a single array of string as input", function(test) {
test.deepEqual(dsv.tsvFormatRow(["a", "b", "c"]), "a\tb\tc");
test.end();
});

0 comments on commit 437da9a

Please sign in to comment.