Skip to content

Commit e6e4774

Browse files
authored
FileAttachment.dsv (#1172)
* FileAttachment.dsv * document FileAttachment.dsv
1 parent 0dc0fe4 commit e6e4774

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

docs/lib/csv.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Comma-separated values
22

3-
To load a [comma-separated values](https://en.wikipedia.org/wiki/Comma-separated_values) (CSV) file, use [`FileAttachment`](../javascript/files). The `file.csv` and `file.tsv` methods are implemented using [D3](https://d3js.org/d3-dsv) and are based on [RFC 4180](https://datatracker.ietf.org/doc/html/rfc4180).
3+
To load a [comma-separated values](https://en.wikipedia.org/wiki/Comma-separated_values) (CSV) file, use [`FileAttachment`](../javascript/files)`.csv`. The `csv`, `tsv`, and `dsv` method implementations are based on [RFC 4180](https://datatracker.ietf.org/doc/html/rfc4180).
44

55
```js echo
66
const gistemp = FileAttachment("gistemp.csv").csv({typed: true});
@@ -18,7 +18,7 @@ The column names are listed in the `columns` property:
1818
gistemp.columns
1919
```
2020

21-
You can also load a tab-separated values (TSV) file using [`FileAttachment`](../javascript/files):
21+
You can also load a tab-separated values (TSV) file using `FileAttachment.tsv`:
2222

2323
```js echo
2424
const capitals = FileAttachment("us-state-capitals.tsv").tsv({typed: true});
@@ -28,6 +28,12 @@ const capitals = FileAttachment("us-state-capitals.tsv").tsv({typed: true});
2828
Inputs.table(capitals)
2929
```
3030

31+
For a different delimiter, use `FileAttachment.dsv`. <a href="https://github.com/observablehq/framework/pull/1172" class="observablehq-version-badge" data-version="prerelease" title="Added in #1172"></a> For example, for semicolon separated values:
32+
33+
```js run=false
34+
const capitals = FileAttachment("us-state-capitals.csv").dsv({delimiter: ";", typed: true});
35+
```
36+
3137
## Type coercion
3238

3339
A common pitfall with CSV is that it is untyped: numbers, dates, booleans, and every other possible value are represented as text and there is no universal way to automatically determine the correct type.

src/client/stdlib/fileAttachment.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ async function remote_fetch(file) {
2121
return response;
2222
}
2323

24-
async function dsv(file, delimiter, {array = false, typed = false} = {}) {
25-
const [text, d3] = await Promise.all([file.text(), import("npm:d3-dsv")]);
26-
const parse = delimiter === "\t" ? (array ? d3.tsvParseRows : d3.tsvParse) : array ? d3.csvParseRows : d3.csvParse;
27-
return parse(text, typed && d3.autoType);
28-
}
29-
3024
export class AbstractFile {
3125
constructor(name, mimeType = "application/octet-stream", lastModified) {
3226
Object.defineProperty(this, "name", {value: `${name}`, enumerable: true});
@@ -50,11 +44,17 @@ export class AbstractFile {
5044
async stream() {
5145
return (await remote_fetch(this)).body;
5246
}
47+
async dsv({delimiter = ",", array = false, typed = false} = {}) {
48+
const [text, d3] = await Promise.all([this.text(), import("npm:d3-dsv")]);
49+
const format = d3.dsvFormat(delimiter);
50+
const parse = array ? format.parseRows : format.parse;
51+
return parse(text, typed && d3.autoType);
52+
}
5353
async csv(options) {
54-
return dsv(this, ",", options);
54+
return this.dsv({...options, delimiter: ","});
5555
}
5656
async tsv(options) {
57-
return dsv(this, "\t", options);
57+
return this.dsv({...options, delimiter: "\t"});
5858
}
5959
async image(props) {
6060
const url = await this.url();

0 commit comments

Comments
 (0)