add lisp packages

This commit is contained in:
2020-12-05 21:29:49 +01:00
parent 85e20365ae
commit a6e2395755
7272 changed files with 1363243 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
Copyright 2013-2016 Mike Bostock
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the author nor the names of contributors may be used to
endorse or promote products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@@ -0,0 +1,473 @@
# d3-dsv
This module provides a parser and formatter for delimiter-separated values, most commonly [comma-](https://en.wikipedia.org/wiki/Comma-separated_values) (CSV) or tab-separated values (TSV). These tabular formats are popular with spreadsheet programs such as Microsoft Excel, and are often more space-efficient than JSON. This implementation is based on [RFC 4180](http://tools.ietf.org/html/rfc4180).
Comma (CSV) and tab (TSV) delimiters are built-in. For example, to parse:
```js
d3.csvParse("foo,bar\n1,2"); // [{foo: "1", bar: "2"}, columns: ["foo", "bar"]]
d3.tsvParse("foo\tbar\n1\t2"); // [{foo: "1", bar: "2"}, columns: ["foo", "bar"]]
```
Or to format:
```js
d3.csvFormat([{foo: "1", bar: "2"}]); // "foo,bar\n1,2"
d3.tsvFormat([{foo: "1", bar: "2"}]); // "foo\tbar\n1\t2"
```
To use a different delimiter, such as “|” for pipe-separated values, use [d3.dsvFormat](#dsvFormat):
```js
var psv = d3.dsvFormat("|");
console.log(psv.parse("foo|bar\n1|2")); // [{foo: "1", bar: "2"}, columns: ["foo", "bar"]]
```
For easy loading of DSV files in a browser, see [d3-fetch](https://github.com/d3/d3-fetch)s [d3.csv](https://github.com/d3/d3-fetch/blob/master/README.md#csv) and [d3.tsv](https://github.com/d3/d3-fetch/blob/master/README.md#tsv) methods.
## Installing
If you use NPM, `npm install d3-dsv`. Otherwise, download the [latest release](https://github.com/d3/d3-dsv/releases/latest). You can also load directly from [d3js.org](https://d3js.org), either as a [standalone library](https://d3js.org/d3-dsv.v1.min.js) or as part of [D3](https://github.com/d3/d3). AMD, CommonJS, and vanilla environments are supported. In vanilla, a `d3` global is exported:
```html
<script src="https://d3js.org/d3-dsv.v1.min.js"></script>
<script>
var data = d3.csvParse(string);
</script>
```
[Try d3-dsv in your browser.](https://tonicdev.com/npm/d3-dsv)
## API Reference
<a name="csvParse" href="#csvParse">#</a> d3.<b>csvParse</b>(<i>string</i>[, <i>row</i>]) [<>](https://github.com/d3/d3-dsv/blob/master/src/csv.js "Source")
Equivalent to [dsvFormat](#dsvFormat)(",").[parse](#dsv_parse).
<a name="csvParseRows" href="#csvParseRows">#</a> d3.<b>csvParseRows</b>(<i>string</i>[, <i>row</i>]) [<>](https://github.com/d3/d3-dsv/blob/master/src/csv.js "Source")
Equivalent to [dsvFormat](#dsvFormat)(",").[parseRows](#dsv_parseRows).
<a name="csvFormat" href="#csvFormat">#</a> d3.<b>csvFormat</b>(<i>rows</i>[, <i>columns</i>]) [<>](https://github.com/d3/d3-dsv/blob/master/src/csv.js "Source")
Equivalent to [dsvFormat](#dsvFormat)(",").[format](#dsv_format).
<a name="csvFormatBody" href="#csvFormatBody">#</a> d3.<b>csvFormatBody</b>(<i>rows</i>[, <i>columns</i>]) [<>](https://github.com/d3/d3-dsv/blob/master/src/csv.js "Source")
Equivalent to [dsvFormat](#dsvFormat)(",").[formatBody](#dsv_formatBody).
<a name="csvFormatRows" href="#csvFormatRows">#</a> d3.<b>csvFormatRows</b>(<i>rows</i>) [<>](https://github.com/d3/d3-dsv/blob/master/src/csv.js "Source")
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).
<a name="tsvParseRows" href="#tsvParseRows">#</a> d3.<b>tsvParseRows</b>(<i>string</i>[, <i>row</i>]) [<>](https://github.com/d3/d3-dsv/blob/master/src/tsv.js "Source")
Equivalent to [dsvFormat](#dsvFormat)("\t").[parseRows](#dsv_parseRows).
<a name="tsvFormat" href="#tsvFormat">#</a> d3.<b>tsvFormat</b>(<i>rows</i>[, <i>columns</i>]) [<>](https://github.com/d3/d3-dsv/blob/master/src/tsv.js "Source")
Equivalent to [dsvFormat](#dsvFormat)("\t").[format](#dsv_format).
<a name="tsvFormatBody" href="#tsvFormatBody">#</a> d3.<b>tsvFormatBody</b>(<i>rows</i>[, <i>columns</i>]) [<>](https://github.com/d3/d3-dsv/blob/master/src/tsv.js "Source")
Equivalent to [dsvFormat](#dsvFormat)("\t").[formatBody](#dsv_formatBody).
<a name="tsvFormatRows" href="#tsvFormatRows">#</a> d3.<b>tsvFormatRows</b>(<i>rows</i>) [<>](https://github.com/d3/d3-dsv/blob/master/src/tsv.js "Source")
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.
<a name="dsv_parse" href="#dsv_parse">#</a> *dsv*.<b>parse</b>(<i>string</i>[, <i>row</i>]) [<>](https://github.com/d3/d3-dsv/blob/master/src/dsv.js "Source")
Parses the specified *string*, which must be in the delimiter-separated values format with the appropriate delimiter, returning an array of objects representing the parsed rows.
Unlike [*dsv*.parseRows](#dsv_parseRows), this method requires that the first line of the DSV content contains a delimiter-separated list of column names; these column names become the attributes on the returned objects. For example, consider the following CSV file:
```
Year,Make,Model,Length
1997,Ford,E350,2.34
2000,Mercury,Cougar,2.38
```
The resulting JavaScript array is:
```js
[
{"Year": "1997", "Make": "Ford", "Model": "E350", "Length": "2.34"},
{"Year": "2000", "Make": "Mercury", "Model": "Cougar", "Length": "2.38"}
]
```
The returned array also exposes a `columns` property containing the column names in input order (in contrast to Object.keys, whose iteration order is arbitrary). For example:
```js
data.columns; // ["Year", "Make", "Model", "Length"]
```
If a *row* conversion function is not specified, field values are strings. For safety, there is no automatic conversion to numbers, dates, or other types. In some cases, JavaScript may coerce strings to numbers for you automatically (for example, using the `+` operator), but better is to specify a *row* conversion function. See [d3.autoType](#autoType) for a convenient *row* conversion function that infers and coerces common types like numbers and strings.
If a *row* conversion function is specified, the specified function is invoked for each row, being passed an object representing the current row (`d`), the index (`i`) starting at zero for the first non-header row, and the array of column names. If the returned value is null or undefined, the row is skipped and will be omitted from the array returned by *dsv*.parse; otherwise, the returned value defines the corresponding row object. For example:
```js
var data = d3.csvParse(string, function(d) {
return {
year: new Date(+d.Year, 0, 1), // lowercase and convert "Year" to Date
make: d.Make, // lowercase
model: d.Model, // lowercase
length: +d.Length // lowercase and convert "Length" to number
};
});
```
Note: using `+` rather than [parseInt](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseInt) or [parseFloat](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseFloat) is typically faster, though more restrictive. For example, `"30px"` when coerced using `+` returns `NaN`, while parseInt and parseFloat return `30`.
<a name="dsv_parseRows" href="#dsv_parseRows">#</a> <i>dsv</i>.<b>parseRows</b>(<i>string</i>[, <i>row</i>]) [<>](https://github.com/d3/d3-dsv/blob/master/src/dsv.js "Source")
Parses the specified *string*, which must be in the delimiter-separated values format with the appropriate delimiter, returning an array of arrays representing the parsed rows.
Unlike [*dsv*.parse](#dsv_parse), this method treats the header line as a standard row, and should be used whenever DSV content does not contain a header. Each row is represented as an array rather than an object. Rows may have variable length. For example, consider the following CSV file, which notably lacks a header line:
```
1997,Ford,E350,2.34
2000,Mercury,Cougar,2.38
```
The resulting JavaScript array is:
```js
[
["1997", "Ford", "E350", "2.34"],
["2000", "Mercury", "Cougar", "2.38"]
]
```
If a *row* conversion function is not specified, field values are strings. For safety, there is no automatic conversion to numbers, dates, or other types. In some cases, JavaScript may coerce strings to numbers for you automatically (for example, using the `+` operator), but better is to specify a *row* conversion function. See [d3.autoType](#autoType) for a convenient *row* conversion function that infers and coerces common types like numbers and strings.
If a *row* conversion function is specified, the specified function is invoked for each row, being passed an array representing the current row (`d`), the index (`i`) starting at zero for the first row, and the array of column names. If the returned value is null or undefined, the row is skipped and will be omitted from the array returned by *dsv*.parse; otherwise, the returned value defines the corresponding row object. For example:
```js
var data = d3.csvParseRows(string, function(d, i) {
return {
year: new Date(+d[0], 0, 1), // convert first colum column to Date
make: d[1],
model: d[2],
length: +d[3] // convert fourth column to number
};
});
```
In effect, *row* is similar to applying a [map](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map) and [filter](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter) operator to the returned rows.
<a name="dsv_format" href="#dsv_format">#</a> <i>dsv</i>.<b>format</b>(<i>rows</i>[, <i>columns</i>]) [<>](https://github.com/d3/d3-dsv/blob/master/src/dsv.js "Source")
Formats the specified array of object *rows* as delimiter-separated values, returning a string. This operation is the inverse of [*dsv*.parse](#dsv_parse). Each row will be separated by a newline (`\n`), and each column within each 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.
If *columns* is not specified, the list of column names that forms the header row is determined by the union of all properties on all objects in *rows*; the order of columns is nondeterministic. If *columns* is specified, it is an array of strings representing the column names. For example:
```js
var string = d3.csvFormat(data, ["year", "make", "model", "length"]);
```
All fields on each row object will be coerced to strings. If the field value is null or undefined, the empty string is used. If the field value is a Date, the [ECMAScript date-time string format](https://www.ecma-international.org/ecma-262/9.0/index.html#sec-date-time-string-format) (a subset of ISO 8601) is used: for example, dates at UTC midnight are formatted as `YYYY-MM-DD`. For more control over which and how fields are formatted, first map *rows* to an array of array of string, and then use [*dsv*.formatRows](#dsv_formatRows).
<a name="dsv_formatBody" href="#dsv_formatBody">#</a> <i>dsv</i>.<b>formatBody</b>(<i>rows</i>[, <i>columns</i>]) [<>](https://github.com/d3/d3-dsv/blob/master/src/dsv.js "Source")
Equivalent to [*dsv*.format](#dsv_format), but omits the header row. This is useful, for example, when appending rows to an existing file.
<a name="dsv_formatRows" href="#dsv_formatRows">#</a> <i>dsv</i>.<b>formatRows</b>(<i>rows</i>) [<>](https://github.com/d3/d3-dsv/blob/master/src/dsv.js "Source")
Formats the specified array of array of string *rows* as delimiter-separated values, returning a string. This operation is the reverse of [*dsv*.parseRows](#dsv_parseRows). Each row will be separated by a newline (`\n`), and each column within each 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.
To convert an array of objects to an array of arrays while explicitly specifying the columns, use [*array*.map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map). For example:
```js
var string = d3.csvFormatRows(data.map(function(d, i) {
return [
d.year.getFullYear(), // Assuming d.year is a Date object.
d.make,
d.model,
d.length
];
}));
```
If you like, you can also [*array*.concat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat) this result with an array of column names to generate the first row:
```js
var string = d3.csvFormatRows([[
"year",
"make",
"model",
"length"
]].concat(data.map(function(d, i) {
return [
d.year.getFullYear(), // Assuming d.year is a Date object.
d.make,
d.model,
d.length
];
})));
```
<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:
```
Year,Make,Model,Length
1997,Ford,E350,2.34
2000,Mercury,Cougar,2.38
```
When used with [d3.csvParse](#csvParse),
```js
d3.csvParse(string, d3.autoType)
```
the resulting JavaScript array is:
```js
[
{"Year": 1997, "Make": "Ford", "Model": "E350", "Length": 2.34},
{"Year": 2000, "Make": "Mercury", "Model": "Cougar", "Length": 2.38}
]
```
Type inference works as follows. For each *value* in the given *object*, the [trimmed](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim) value is computed; the value is then re-assigned as follows:
1. If empty, then `null`.
1. If exactly `"true"`, then `true`.
1. If exactly `"false"`, then `false`.
1. If exactly `"NaN"`, then `NaN`.
1. Otherwise, if [coercible to a number](https://www.ecma-international.org/ecma-262/9.0/index.html#sec-tonumber-applied-to-the-string-type), then a number.
1. Otherwise, if a [date-only or date-time string](https://www.ecma-international.org/ecma-262/9.0/index.html#sec-date-time-string-format), then a Date.
1. Otherwise, a string (the original untrimmed value).
Values with leading zeroes may be coerced to numbers; for example `"08904"` coerces to `8904`. However, extra characters such as commas or units (*e.g.*, `"$1.00"`, `"(123)"`, `"1,234"` or `"32px"`) will prevent number coercion, resulting in a string.
Date strings must be in ECMAScripts subset of the [ISO 8601 format](https://en.wikipedia.org/wiki/ISO_8601). When a date-only string such as YYYY-MM-DD is specified, the inferred time is midnight UTC; however, if a date-time string such as YYYY-MM-DDTHH:MM is specified without a time zone, it is assumed to be local time.
Automatic type inference is primarily intended to provide safe, predictable behavior in conjunction with [*dsv*.format](#dsv_format) and [*dsv*.formatRows](#dsv_formatRows) for common JavaScript types. If you need different behavior, you should implement your own row accessor function.
For more, see [the d3.autoType notebook](https://observablehq.com/@d3/d3-autotype).
### Content Security Policy
If a [content security policy](http://www.w3.org/TR/CSP/) is in place, note that [*dsv*.parse](#dsv_parse) requires `unsafe-eval` in the `script-src` directive, due to the (safe) use of dynamic code generation for fast parsing. (See [source](https://github.com/d3/d3-dsv/blob/master/src/dsv.js).) Alternatively, use [*dsv*.parseRows](#dsv_parseRows).
### Byte-Order Marks
DSV files sometimes begin with a [byte order mark (BOM)](https://en.wikipedia.org/wiki/Byte_order_mark); saving a spreadsheet in CSV UTF-8 format from Microsoft Excel, for example, will include a BOM. On the web this is not usually a problem because the [UTF-8 decode algorithm](https://encoding.spec.whatwg.org/#utf-8-decode) specified in the Encoding standard removes the BOM. Node.js, on the other hand, [does not remove the BOM](https://github.com/nodejs/node-v0.x-archive/issues/1918) when decoding UTF-8.
If the BOM is not removed, the first character of the text is a zero-width non-breaking space. So if a CSV file with a BOM is parsed by [d3.csvParse](#csvParse), the first columns name will begin with a zero-width non-breaking space. This can be hard to spot since this character is usually invisible when printed.
To remove the BOM before parsing, consider using [strip-bom](https://www.npmjs.com/package/strip-bom).
## Command Line Reference
### dsv2dsv
<a name="dsv2dsv" href="#dsv2dsv">#</a> <b>dsv2dsv</b> [<i>options…</i>] [<i>file</i>]
Converts the specified DSV input *file* to DSV (typically with a different delimiter or encoding). If *file* is not specified, defaults to reading from stdin. For example, to convert to CSV to TSV:
```
csv2tsv < example.csv > example.tsv
```
To convert windows-1252 CSV to utf-8 CSV:
```
dsv2dsv --input-encoding windows-1252 < latin1.csv > utf8.csv
```
<a name="dsv2dsv_help" href="dsv2dsv_help">#</a> dsv2dsv <b>-h</b>
<br><a href="dsv2dsv_help">#</a> dsv2dsv <b>--help</b>
Output usage information.
<a name="dsv2dsv_version" href="dsv2dsv_version">#</a> dsv2dsv <b>-V</b>
<br><a href="dsv2dsv_version">#</a> dsv2dsv <b>--version</b>
Output the version number.
<a name="dsv2dsv_out" href="dsv2dsv_out">#</a> dsv2dsv <b>-o</b> <i>file</i>
<br><a href="dsv2dsv_out">#</a> dsv2dsv <b>--out</b> <i>file</i>
Specify the output file name. Defaults to “-” for stdout.
<a name="dsv2dsv_input_delimiter" href="dsv2dsv_input_delimiter">#</a> dsv2dsv <b>-r</b> <i>delimiter</i>
<br><a href="dsv2dsv_input_delimiter">#</a> dsv2dsv <b>--input-delimiter</b> <i>delimiter</i>
Specify the input delimiter character. Defaults to “,” for reading CSV. (You can enter a tab on the command line by typing ⌃V.)
<a name="dsv2dsv_input_encoding" href="dsv2dsv_input_encoding">#</a> dsv2dsv <b>--input-encoding</b> <i>encoding</i>
Specify the input character encoding. Defaults to “utf8”.
<a name="dsv2dsv_output_delimiter" href="dsv2dsv_output_delimiter">#</a> dsv2dsv <b>-w</b> <i>delimiter</i>
<br><a href="dsv2dsv_output_delimiter">#</a> dsv2dsv <b>--output-delimiter</b> <i>delimiter</i>
Specify the output delimiter character. Defaults to “,” for writing CSV. (You can enter a tab on the command line by typing ⌃V.)
<a name="dsv2dsv_output_encoding" href="dsv2dsv_output_encoding">#</a> dsv2dsv <b>--output-encoding</b> <i>encoding</i>
Specify the output character encoding. Defaults to “utf8”.
<a name="csv2tsv" href="#csv2tsv">#</a> <b>csv2tsv</b> [<i>options…</i>] [<i>file</i>]
Equivalent to [dsv2dsv](#dsv2dsv), but the [output delimiter](#dsv2dsv_output_delimiter) defaults to the tab character (\t).
<a name="tsv2csv" href="#tsv2csv">#</a> <b>tsv2csv</b> [<i>options…</i>] [<i>file</i>]
Equivalent to [dsv2dsv](#dsv2dsv), but the [input delimiter](#dsv2dsv_output_delimiter) defaults to the tab character (\t).
### dsv2json
<a name="dsv2json" href="#dsv2json">#</a> <b>dsv2json</b> [<i>options…</i>] [<i>file</i>]
Converts the specified DSV input *file* to JSON. If *file* is not specified, defaults to reading from stdin. For example, to convert to CSV to JSON:
```
csv2json < example.csv > example.json
```
Or to convert CSV to a newline-delimited JSON stream:
```
csv2json -n < example.csv > example.ndjson
```
<a name="dsv2json_help" href="dsv2json_help">#</a> dsv2json <b>-h</b>
<br><a href="dsv2json_help">#</a> dsv2json <b>--help</b>
Output usage information.
<a name="dsv2json_version" href="dsv2json_version">#</a> dsv2json <b>-V</b>
<br><a href="dsv2json_version">#</a> dsv2json <b>--version</b>
Output the version number.
<a name="dsv2json_out" href="dsv2json_out">#</a> dsv2json <b>-o</b> <i>file</i>
<br><a href="dsv2json_out">#</a> dsv2json <b>--out</b> <i>file</i>
Specify the output file name. Defaults to “-” for stdout.
<a name="dsv2json_input_delimiter" href="dsv2json_input_delimiter">#</a> dsv2json <b>-r</b> <i>delimiter</i>
<br><a href="dsv2json_input_delimiter">#</a> dsv2json <b>--input-delimiter</b> <i>delimiter</i>
Specify the input delimiter character. Defaults to “,” for reading CSV. (You can enter a tab on the command line by typing ⌃V.)
<a name="dsv2json_input_encoding" href="dsv2json_input_encoding">#</a> dsv2json <b>--input-encoding</b> <i>encoding</i>
Specify the input character encoding. Defaults to “utf8”.
<a name="dsv2json_output_encoding" href="dsv2json_output_encoding">#</a> dsv2json <b>-r</b> <i>encoding</i>
<br><a href="dsv2json_output_encoding">#</a> dsv2json <b>--output-encoding</b> <i>encoding</i>
Specify the output character encoding. Defaults to “utf8”.
<a name="dsv2json_newline_delimited" href="dsv2json_newline_delimited">#</a> dsv2json <b>-n</b>
<br><a href="dsv2json_newline_delimited">#</a> dsv2json <b>--newline-delimited</b>
Output [newline-delimited JSON](https://github.com/mbostock/ndjson-cli) instead of a single JSON array.
<a name="csv2json" href="#csv2json">#</a> <b>csv2json</b> [<i>options…</i>] [<i>file</i>]
Equivalent to [dsv2json](#dsv2json).
<a name="tsv2json" href="#csv2json">#</a> <b>tsv2json</b> [<i>options…</i>] [<i>file</i>]
Equivalent to [dsv2json](#dsv2json), but the [input delimiter](#dsv2json_input_delimiter) defaults to the tab character (\t).
### json2dsv
<a name="json2dsv" href="#json2dsv">#</a> <b>json2dsv</b> [<i>options…</i>] [<i>file</i>]
Converts the specified JSON input *file* to DSV. If *file* is not specified, defaults to reading from stdin. For example, to convert to JSON to CSV:
```
json2csv < example.json > example.csv
```
Or to convert a newline-delimited JSON stream to CSV:
```
json2csv -n < example.ndjson > example.csv
```
<a name="json2dsv_help" href="json2dsv_help">#</a> json2dsv <b>-h</b>
<br><a href="json2dsv_help">#</a> json2dsv <b>--help</b>
Output usage information.
<a name="json2dsv_version" href="json2dsv_version">#</a> json2dsv <b>-V</b>
<br><a href="json2dsv_version">#</a> json2dsv <b>--version</b>
Output the version number.
<a name="json2dsv_out" href="json2dsv_out">#</a> json2dsv <b>-o</b> <i>file</i>
<br><a href="json2dsv_out">#</a> json2dsv <b>--out</b> <i>file</i>
Specify the output file name. Defaults to “-” for stdout.
<a name="json2dsv_input_encoding" href="json2dsv_input_encoding">#</a> json2dsv <b>--input-encoding</b> <i>encoding</i>
Specify the input character encoding. Defaults to “utf8”.
<a name="json2dsv_output_delimiter" href="json2dsv_output_delimiter">#</a> json2dsv <b>-w</b> <i>delimiter</i>
<br><a href="json2dsv_output_delimiter">#</a> json2dsv <b>--output-delimiter</b> <i>delimiter</i>
Specify the output delimiter character. Defaults to “,” for writing CSV. (You can enter a tab on the command line by typing ⌃V.)
<a name="json2dsv_output_encoding" href="json2dsv_output_encoding">#</a> json2dsv <b>--output-encoding</b> <i>encoding</i>
Specify the output character encoding. Defaults to “utf8”.
<a name="json2dsv_newline_delimited" href="json2dsv_newline_delimited">#</a> json2dsv <b>-n</b>
<br><a href="json2dsv_newline_delimited">#</a> json2dsv <b>--newline-delimited</b>
Read [newline-delimited JSON](https://github.com/mbostock/ndjson-cli) instead of a single JSON array.
<a name="csv2json" href="#csv2json">#</a> <b>csv2json</b> [<i>options…</i>] [<i>file</i>]
Equivalent to [json2dsv](#json2dsv).
<a name="tsv2json" href="#csv2json">#</a> <b>tsv2json</b> [<i>options…</i>] [<i>file</i>]
Equivalent to [json2dsv](#json2dsv), but the [output delimiter](#json2dsv_output_delimiter) defaults to the tab character (\t).

View File

@@ -0,0 +1,32 @@
#!/usr/bin/env node
var os = require("os"),
rw = require("rw").dash,
path = require("path"),
iconv = require("iconv-lite"),
commander = require("commander"),
dsv = require("../");
var program = path.basename(process.argv[1]),
defaultInDelimiter = program.slice(0, 3) === "tsv" ? "\t" : ",",
defaultOutDelimiter = program.slice(-3) === "tsv" ? "\t" : ",";
commander
.version(require("../package.json").version)
.usage("[options] [file]")
.option("-o, --out <file>", "output file name; defaults to “-” for stdout", "-")
.option("-r, --input-delimiter <character>", "input delimiter character", defaultInDelimiter)
.option("-w, --output-delimiter <character>", "output delimiter character", defaultOutDelimiter)
.option("--input-encoding <encoding>", "input character encoding; defaults to “utf8”", "utf8")
.option("--output-encoding <encoding>", "output character encoding; defaults to “utf8”", "utf8")
.parse(process.argv);
var inFormat = dsv.dsvFormat(commander.inputDelimiter),
outFormat = dsv.dsvFormat(commander.outputDelimiter);
rw.readFile(commander.args[0] || "-", function(error, text) {
if (error) throw error;
rw.writeFile("-", iconv.encode(outFormat.format(inFormat.parse(iconv.decode(text, commander.inputEncoding))) + os.EOL, commander.outputEncoding), function(error) {
if (error) throw error;
});
});

View File

@@ -0,0 +1,33 @@
#!/usr/bin/env node
var os = require("os"),
rw = require("rw").dash,
path = require("path"),
iconv = require("iconv-lite"),
commander = require("commander"),
dsv = require("../");
var program = path.basename(process.argv[1]),
defaultInDelimiter = program.slice(0, 3) === "tsv" ? "\t" : ",";
commander
.version(require("../package.json").version)
.usage("[options] [file]")
.option("-o, --out <file>", "output file name; defaults to “-” for stdout", "-")
.option("-r, --input-delimiter <character>", "input delimiter character", defaultInDelimiter)
.option("-n, --newline-delimited", "output newline-delimited JSON")
.option("--input-encoding <encoding>", "input character encoding; defaults to “utf8”", "utf8")
.option("--output-encoding <encoding>", "output character encoding; defaults to “utf8”", "utf8")
.parse(process.argv);
var inFormat = dsv.dsvFormat(commander.inputDelimiter);
rw.readFile(commander.args[0] || "-", function(error, text) {
if (error) throw error;
var rows = inFormat.parse(iconv.decode(text, commander.inputEncoding));
rw.writeFile(commander.out, iconv.encode(commander.newlineDelimited
? rows.map(function(row) { return JSON.stringify(row); }).join("\n") + "\n"
: JSON.stringify(rows) + os.EOL, commander.outputEncoding), function(error) {
if (error) throw error;
});
});

View File

@@ -0,0 +1,33 @@
#!/usr/bin/env node
var os = require("os"),
rw = require("rw").dash,
path = require("path"),
iconv = require("iconv-lite"),
commander = require("commander"),
dsv = require("../");
var program = path.basename(process.argv[1]),
defaultOutDelimiter = program.slice(-3) === "tsv" ? "\t" : ",";
commander
.version(require("../package.json").version)
.usage("[options] [file]")
.option("-o, --out <file>", "output file name; defaults to “-” for stdout", "-")
.option("-w, --output-delimiter <character>", "output delimiter character", defaultOutDelimiter)
.option("-n, --newline-delimited", "accept newline-delimited JSON")
.option("--input-encoding <encoding>", "input character encoding; defaults to “utf8”", "utf8")
.option("--output-encoding <encoding>", "output character encoding; defaults to “utf8”", "utf8")
.parse(process.argv);
var outFormat = dsv.dsvFormat(commander.outputDelimiter);
rw.readFile(commander.args[0] || "-", function(error, text) {
if (error) throw error;
text = iconv.decode(text, commander.inputEncoding);
rw.writeFile(commander.out, iconv.encode(outFormat.format(commander.newlineDelimited
? text.trim().split(/\r?\n/g).map(function(line) { return JSON.parse(line); })
: JSON.parse(text)) + os.EOL, commander.outputEncoding), function(error) {
if (error) throw error;
});
});

View File

@@ -0,0 +1,233 @@
// https://d3js.org/d3-dsv/ v1.2.0 Copyright 2019 Mike Bostock
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global.d3 = global.d3 || {}));
}(this, function (exports) { 'use strict';
var EOL = {},
EOF = {},
QUOTE = 34,
NEWLINE = 10,
RETURN = 13;
function objectConverter(columns) {
return new Function("d", "return {" + columns.map(function(name, i) {
return JSON.stringify(name) + ": d[" + i + "] || \"\"";
}).join(",") + "}");
}
function customConverter(columns, f) {
var object = objectConverter(columns);
return function(row, i) {
return f(object(row), i, columns);
};
}
// Compute unique columns in order of discovery.
function inferColumns(rows) {
var columnSet = Object.create(null),
columns = [];
rows.forEach(function(row) {
for (var column in row) {
if (!(column in columnSet)) {
columns.push(columnSet[column] = column);
}
}
});
return columns;
}
function pad(value, width) {
var s = value + "", length = s.length;
return length < width ? new Array(width - length + 1).join(0) + s : s;
}
function formatYear(year) {
return year < 0 ? "-" + pad(-year, 6)
: year > 9999 ? "+" + pad(year, 6)
: pad(year, 4);
}
function formatDate(date) {
var hours = date.getUTCHours(),
minutes = date.getUTCMinutes(),
seconds = date.getUTCSeconds(),
milliseconds = date.getUTCMilliseconds();
return isNaN(date) ? "Invalid Date"
: formatYear(date.getUTCFullYear()) + "-" + pad(date.getUTCMonth() + 1, 2) + "-" + pad(date.getUTCDate(), 2)
+ (milliseconds ? "T" + pad(hours, 2) + ":" + pad(minutes, 2) + ":" + pad(seconds, 2) + "." + pad(milliseconds, 3) + "Z"
: seconds ? "T" + pad(hours, 2) + ":" + pad(minutes, 2) + ":" + pad(seconds, 2) + "Z"
: minutes || hours ? "T" + pad(hours, 2) + ":" + pad(minutes, 2) + "Z"
: "");
}
function dsv(delimiter) {
var reFormat = new RegExp("[\"" + delimiter + "\n\r]"),
DELIMITER = delimiter.charCodeAt(0);
function parse(text, f) {
var convert, columns, rows = parseRows(text, function(row, i) {
if (convert) return convert(row, i - 1);
columns = row, convert = f ? customConverter(row, f) : objectConverter(row);
});
rows.columns = columns || [];
return rows;
}
function parseRows(text, f) {
var rows = [], // output rows
N = text.length,
I = 0, // current character index
n = 0, // current line number
t, // current token
eof = N <= 0, // current token followed by EOF?
eol = false; // current token followed by EOL?
// Strip the trailing newline.
if (text.charCodeAt(N - 1) === NEWLINE) --N;
if (text.charCodeAt(N - 1) === RETURN) --N;
function token() {
if (eof) return EOF;
if (eol) return eol = false, EOL;
// Unescape quotes.
var i, j = I, c;
if (text.charCodeAt(j) === QUOTE) {
while (I++ < N && text.charCodeAt(I) !== QUOTE || text.charCodeAt(++I) === QUOTE);
if ((i = I) >= N) eof = true;
else if ((c = text.charCodeAt(I++)) === NEWLINE) eol = true;
else if (c === RETURN) { eol = true; if (text.charCodeAt(I) === NEWLINE) ++I; }
return text.slice(j + 1, i - 1).replace(/""/g, "\"");
}
// Find next delimiter or newline.
while (I < N) {
if ((c = text.charCodeAt(i = I++)) === NEWLINE) eol = true;
else if (c === RETURN) { eol = true; if (text.charCodeAt(I) === NEWLINE) ++I; }
else if (c !== DELIMITER) continue;
return text.slice(j, i);
}
// Return last token before EOF.
return eof = true, text.slice(j, N);
}
while ((t = token()) !== EOF) {
var row = [];
while (t !== EOL && t !== EOF) row.push(t), t = token();
if (f && (row = f(row, n++)) == null) continue;
rows.push(row);
}
return rows;
}
function preformatBody(rows, columns) {
return rows.map(function(row) {
return columns.map(function(column) {
return formatValue(row[column]);
}).join(delimiter);
});
}
function format(rows, columns) {
if (columns == null) columns = inferColumns(rows);
return [columns.map(formatValue).join(delimiter)].concat(preformatBody(rows, columns)).join("\n");
}
function formatBody(rows, columns) {
if (columns == null) columns = inferColumns(rows);
return preformatBody(rows, columns).join("\n");
}
function formatRows(rows) {
return rows.map(formatRow).join("\n");
}
function formatRow(row) {
return row.map(formatValue).join(delimiter);
}
function formatValue(value) {
return value == null ? ""
: value instanceof Date ? formatDate(value)
: reFormat.test(value += "") ? "\"" + value.replace(/"/g, "\"\"") + "\""
: value;
}
return {
parse: parse,
parseRows: parseRows,
format: format,
formatBody: formatBody,
formatRows: formatRows,
formatRow: formatRow,
formatValue: formatValue
};
}
var csv = dsv(",");
var csvParse = csv.parse;
var csvParseRows = csv.parseRows;
var csvFormat = csv.format;
var csvFormatBody = csv.formatBody;
var csvFormatRows = csv.formatRows;
var csvFormatRow = csv.formatRow;
var csvFormatValue = csv.formatValue;
var tsv = dsv("\t");
var tsvParse = tsv.parse;
var tsvParseRows = tsv.parseRows;
var tsvFormat = tsv.format;
var tsvFormatBody = tsv.formatBody;
var tsvFormatRows = tsv.formatRows;
var tsvFormatRow = tsv.formatRow;
var tsvFormatValue = tsv.formatValue;
function autoType(object) {
for (var key in object) {
var value = object[key].trim(), number, m;
if (!value) value = null;
else if (value === "true") value = true;
else if (value === "false") value = false;
else if (value === "NaN") value = NaN;
else if (!isNaN(number = +value)) value = number;
else if (m = value.match(/^([-+]\d{2})?\d{4}(-\d{2}(-\d{2})?)?(T\d{2}:\d{2}(:\d{2}(\.\d{3})?)?(Z|[-+]\d{2}:\d{2})?)?$/)) {
if (fixtz && !!m[4] && !m[7]) value = value.replace(/-/g, "/").replace(/T/, " ");
value = new Date(value);
}
else continue;
object[key] = value;
}
return object;
}
// https://github.com/d3/d3-dsv/issues/45
var fixtz = new Date("2019-01-01T00:00").getHours() || new Date("2019-07-01T00:00").getHours();
exports.autoType = autoType;
exports.csvFormat = csvFormat;
exports.csvFormatBody = csvFormatBody;
exports.csvFormatRow = csvFormatRow;
exports.csvFormatRows = csvFormatRows;
exports.csvFormatValue = csvFormatValue;
exports.csvParse = csvParse;
exports.csvParseRows = csvParseRows;
exports.dsvFormat = dsv;
exports.tsvFormat = tsvFormat;
exports.tsvFormatBody = tsvFormatBody;
exports.tsvFormatRow = tsvFormatRow;
exports.tsvFormatRows = tsvFormatRows;
exports.tsvFormatValue = tsvFormatValue;
exports.tsvParse = tsvParse;
exports.tsvParseRows = tsvParseRows;
Object.defineProperty(exports, '__esModule', { value: true });
}));

View File

@@ -0,0 +1,2 @@
// https://d3js.org/d3-dsv/ v1.2.0 Copyright 2019 Mike Bostock
!function(r,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((r=r||self).d3=r.d3||{})}(this,function(r){"use strict";var t={},e={},n=34,o=10,a=13;function u(r){return new Function("d","return {"+r.map(function(r,t){return JSON.stringify(r)+": d["+t+'] || ""'}).join(",")+"}")}function f(r){var t=Object.create(null),e=[];return r.forEach(function(r){for(var n in r)n in t||e.push(t[n]=n)}),e}function i(r,t){var e=r+"",n=e.length;return n<t?new Array(t-n+1).join(0)+e:e}function s(r){var t,e=r.getUTCHours(),n=r.getUTCMinutes(),o=r.getUTCSeconds(),a=r.getUTCMilliseconds();return isNaN(r)?"Invalid Date":((t=r.getUTCFullYear())<0?"-"+i(-t,6):t>9999?"+"+i(t,6):i(t,4))+"-"+i(r.getUTCMonth()+1,2)+"-"+i(r.getUTCDate(),2)+(a?"T"+i(e,2)+":"+i(n,2)+":"+i(o,2)+"."+i(a,3)+"Z":o?"T"+i(e,2)+":"+i(n,2)+":"+i(o,2)+"Z":n||e?"T"+i(e,2)+":"+i(n,2)+"Z":"")}function c(r){var i=new RegExp('["'+r+"\n\r]"),c=r.charCodeAt(0);function l(r,u){var f,i=[],s=r.length,l=0,d=0,m=s<=0,v=!1;function p(){if(m)return e;if(v)return v=!1,t;var u,f,i=l;if(r.charCodeAt(i)===n){for(;l++<s&&r.charCodeAt(l)!==n||r.charCodeAt(++l)===n;);return(u=l)>=s?m=!0:(f=r.charCodeAt(l++))===o?v=!0:f===a&&(v=!0,r.charCodeAt(l)===o&&++l),r.slice(i+1,u-1).replace(/""/g,'"')}for(;l<s;){if((f=r.charCodeAt(u=l++))===o)v=!0;else if(f===a)v=!0,r.charCodeAt(l)===o&&++l;else if(f!==c)continue;return r.slice(i,u)}return m=!0,r.slice(i,s)}for(r.charCodeAt(s-1)===o&&--s,r.charCodeAt(s-1)===a&&--s;(f=p())!==e;){for(var w=[];f!==t&&f!==e;)w.push(f),f=p();u&&null==(w=u(w,d++))||i.push(w)}return i}function d(t,e){return t.map(function(t){return e.map(function(r){return v(t[r])}).join(r)})}function m(t){return t.map(v).join(r)}function v(r){return null==r?"":r instanceof Date?s(r):i.test(r+="")?'"'+r.replace(/"/g,'""')+'"':r}return{parse:function(r,t){var e,n,o=l(r,function(r,o){if(e)return e(r,o-1);n=r,e=t?function(r,t){var e=u(r);return function(n,o){return t(e(n),o,r)}}(r,t):u(r)});return o.columns=n||[],o},parseRows:l,format:function(t,e){return null==e&&(e=f(t)),[e.map(v).join(r)].concat(d(t,e)).join("\n")},formatBody:function(r,t){return null==t&&(t=f(r)),d(r,t).join("\n")},formatRows:function(r){return r.map(m).join("\n")},formatRow:m,formatValue:v}}var l=c(","),d=l.parse,m=l.parseRows,v=l.format,p=l.formatBody,w=l.formatRows,h=l.formatRow,C=l.formatValue,g=c("\t"),R=g.parse,T=g.parseRows,F=g.format,y=g.formatBody,j=g.formatRows,A=g.formatRow,N=g.formatValue;var U=new Date("2019-01-01T00:00").getHours()||new Date("2019-07-01T00:00").getHours();r.autoType=function(r){for(var t in r){var e,n,o=r[t].trim();if(o)if("true"===o)o=!0;else if("false"===o)o=!1;else if("NaN"===o)o=NaN;else if(isNaN(e=+o)){if(!(n=o.match(/^([-+]\d{2})?\d{4}(-\d{2}(-\d{2})?)?(T\d{2}:\d{2}(:\d{2}(\.\d{3})?)?(Z|[-+]\d{2}:\d{2})?)?$/)))continue;U&&n[4]&&!n[7]&&(o=o.replace(/-/g,"/").replace(/T/," ")),o=new Date(o)}else o=e;else o=null;r[t]=o}return r},r.csvFormat=v,r.csvFormatBody=p,r.csvFormatRow=h,r.csvFormatRows=w,r.csvFormatValue=C,r.csvParse=d,r.csvParseRows=m,r.dsvFormat=c,r.tsvFormat=F,r.tsvFormatBody=y,r.tsvFormatRow=A,r.tsvFormatRows=j,r.tsvFormatValue=N,r.tsvParse=R,r.tsvParseRows=T,Object.defineProperty(r,"__esModule",{value:!0})});

View File

@@ -0,0 +1,61 @@
{
"name": "d3-dsv",
"version": "1.2.0",
"description": "A parser and formatter for delimiter-separated values, such as CSV and TSV",
"keywords": [
"d3",
"d3-module",
"dsv",
"csv",
"tsv"
],
"homepage": "https://d3js.org/d3-dsv/",
"license": "BSD-3-Clause",
"author": {
"name": "Mike Bostock",
"url": "http://bost.ocks.org/mike"
},
"main": "dist/d3-dsv.js",
"unpkg": "dist/d3-dsv.min.js",
"jsdelivr": "dist/d3-dsv.min.js",
"module": "src/index.js",
"bin": {
"csv2json": "bin/dsv2json",
"csv2tsv": "bin/dsv2dsv",
"dsv2dsv": "bin/dsv2dsv",
"dsv2json": "bin/dsv2json",
"json2csv": "bin/json2dsv",
"json2dsv": "bin/json2dsv",
"json2tsv": "bin/json2dsv",
"tsv2csv": "bin/dsv2dsv",
"tsv2json": "bin/dsv2json"
},
"repository": {
"type": "git",
"url": "https://github.com/d3/d3-dsv.git"
},
"files": [
"bin/*",
"dist/**/*.js",
"src/**/*.js"
],
"scripts": {
"pretest": "rollup -c",
"test": "TZ=America/Los_Angeles tape 'test/**/*-test.js' && eslint src test",
"prepublishOnly": "rm -rf dist && yarn test",
"postpublish": "git push && git push --tags && cd ../d3.github.com && git pull && cp ../${npm_package_name}/dist/${npm_package_name}.js ${npm_package_name}.v${npm_package_version%%.*}.js && cp ../${npm_package_name}/dist/${npm_package_name}.min.js ${npm_package_name}.v${npm_package_version%%.*}.min.js && git add ${npm_package_name}.v${npm_package_version%%.*}.js ${npm_package_name}.v${npm_package_version%%.*}.min.js && git commit -m \"${npm_package_name} ${npm_package_version}\" && git push && cd - && zip -j dist/${npm_package_name}.zip -- LICENSE README.md dist/${npm_package_name}.js dist/${npm_package_name}.min.js"
},
"dependencies": {
"commander": "2",
"iconv-lite": "0.4",
"rw": "1"
},
"sideEffects": false,
"devDependencies": {
"csv-spectrum": "1",
"eslint": "6",
"rollup": "1",
"rollup-plugin-terser": "5",
"tape": "4"
}
}

View File

@@ -0,0 +1,20 @@
export default function autoType(object) {
for (var key in object) {
var value = object[key].trim(), number, m;
if (!value) value = null;
else if (value === "true") value = true;
else if (value === "false") value = false;
else if (value === "NaN") value = NaN;
else if (!isNaN(number = +value)) value = number;
else if (m = value.match(/^([-+]\d{2})?\d{4}(-\d{2}(-\d{2})?)?(T\d{2}:\d{2}(:\d{2}(\.\d{3})?)?(Z|[-+]\d{2}:\d{2})?)?$/)) {
if (fixtz && !!m[4] && !m[7]) value = value.replace(/-/g, "/").replace(/T/, " ");
value = new Date(value);
}
else continue;
object[key] = value;
}
return object;
}
// https://github.com/d3/d3-dsv/issues/45
var fixtz = new Date("2019-01-01T00:00").getHours() || new Date("2019-07-01T00:00").getHours();

View File

@@ -0,0 +1,11 @@
import dsv from "./dsv.js";
var csv = dsv(",");
export var csvParse = csv.parse;
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;

View File

@@ -0,0 +1,164 @@
var EOL = {},
EOF = {},
QUOTE = 34,
NEWLINE = 10,
RETURN = 13;
function objectConverter(columns) {
return new Function("d", "return {" + columns.map(function(name, i) {
return JSON.stringify(name) + ": d[" + i + "] || \"\"";
}).join(",") + "}");
}
function customConverter(columns, f) {
var object = objectConverter(columns);
return function(row, i) {
return f(object(row), i, columns);
};
}
// Compute unique columns in order of discovery.
function inferColumns(rows) {
var columnSet = Object.create(null),
columns = [];
rows.forEach(function(row) {
for (var column in row) {
if (!(column in columnSet)) {
columns.push(columnSet[column] = column);
}
}
});
return columns;
}
function pad(value, width) {
var s = value + "", length = s.length;
return length < width ? new Array(width - length + 1).join(0) + s : s;
}
function formatYear(year) {
return year < 0 ? "-" + pad(-year, 6)
: year > 9999 ? "+" + pad(year, 6)
: pad(year, 4);
}
function formatDate(date) {
var hours = date.getUTCHours(),
minutes = date.getUTCMinutes(),
seconds = date.getUTCSeconds(),
milliseconds = date.getUTCMilliseconds();
return isNaN(date) ? "Invalid Date"
: formatYear(date.getUTCFullYear(), 4) + "-" + pad(date.getUTCMonth() + 1, 2) + "-" + pad(date.getUTCDate(), 2)
+ (milliseconds ? "T" + pad(hours, 2) + ":" + pad(minutes, 2) + ":" + pad(seconds, 2) + "." + pad(milliseconds, 3) + "Z"
: seconds ? "T" + pad(hours, 2) + ":" + pad(minutes, 2) + ":" + pad(seconds, 2) + "Z"
: minutes || hours ? "T" + pad(hours, 2) + ":" + pad(minutes, 2) + "Z"
: "");
}
export default function(delimiter) {
var reFormat = new RegExp("[\"" + delimiter + "\n\r]"),
DELIMITER = delimiter.charCodeAt(0);
function parse(text, f) {
var convert, columns, rows = parseRows(text, function(row, i) {
if (convert) return convert(row, i - 1);
columns = row, convert = f ? customConverter(row, f) : objectConverter(row);
});
rows.columns = columns || [];
return rows;
}
function parseRows(text, f) {
var rows = [], // output rows
N = text.length,
I = 0, // current character index
n = 0, // current line number
t, // current token
eof = N <= 0, // current token followed by EOF?
eol = false; // current token followed by EOL?
// Strip the trailing newline.
if (text.charCodeAt(N - 1) === NEWLINE) --N;
if (text.charCodeAt(N - 1) === RETURN) --N;
function token() {
if (eof) return EOF;
if (eol) return eol = false, EOL;
// Unescape quotes.
var i, j = I, c;
if (text.charCodeAt(j) === QUOTE) {
while (I++ < N && text.charCodeAt(I) !== QUOTE || text.charCodeAt(++I) === QUOTE);
if ((i = I) >= N) eof = true;
else if ((c = text.charCodeAt(I++)) === NEWLINE) eol = true;
else if (c === RETURN) { eol = true; if (text.charCodeAt(I) === NEWLINE) ++I; }
return text.slice(j + 1, i - 1).replace(/""/g, "\"");
}
// Find next delimiter or newline.
while (I < N) {
if ((c = text.charCodeAt(i = I++)) === NEWLINE) eol = true;
else if (c === RETURN) { eol = true; if (text.charCodeAt(I) === NEWLINE) ++I; }
else if (c !== DELIMITER) continue;
return text.slice(j, i);
}
// Return last token before EOF.
return eof = true, text.slice(j, N);
}
while ((t = token()) !== EOF) {
var row = [];
while (t !== EOL && t !== EOF) row.push(t), t = token();
if (f && (row = f(row, n++)) == null) continue;
rows.push(row);
}
return rows;
}
function preformatBody(rows, columns) {
return rows.map(function(row) {
return columns.map(function(column) {
return formatValue(row[column]);
}).join(delimiter);
});
}
function format(rows, columns) {
if (columns == null) columns = inferColumns(rows);
return [columns.map(formatValue).join(delimiter)].concat(preformatBody(rows, columns)).join("\n");
}
function formatBody(rows, columns) {
if (columns == null) columns = inferColumns(rows);
return preformatBody(rows, columns).join("\n");
}
function formatRows(rows) {
return rows.map(formatRow).join("\n");
}
function formatRow(row) {
return row.map(formatValue).join(delimiter);
}
function formatValue(value) {
return value == null ? ""
: value instanceof Date ? formatDate(value)
: reFormat.test(value += "") ? "\"" + value.replace(/"/g, "\"\"") + "\""
: value;
}
return {
parse: parse,
parseRows: parseRows,
format: format,
formatBody: formatBody,
formatRows: formatRows,
formatRow: formatRow,
formatValue: formatValue
};
}

View File

@@ -0,0 +1,4 @@
export {default as dsvFormat} from "./dsv.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";

View File

@@ -0,0 +1,11 @@
import dsv from "./dsv.js";
var tsv = dsv("\t");
export var tsvParse = tsv.parse;
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;