Skip to content

Commit d489412

Browse files
committed
Add CSV and TSV parsing.
1 parent dd504c0 commit d489412

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ const url = await FileAttachment("file.txt").url();
303303
Returns a promise to the file’s contents as a JavaScript string.
304304

305305
```js
306-
const data = d3.csvParse(await FileAttachment("cars.csv").text());
306+
const hello = await FileAttachment("hello.txt").text();
307307
```
308308

309309
<a href="#attachment_json" name="attachment_json">#</a> *attachment*.<b>json</b>() [<>](https://github.com/observablehq/stdlib/blob/master/src/fileAttachment.js "Source")
@@ -314,6 +314,26 @@ Returns a promise to the file’s contents, parsed as JSON into JavaScript value
314314
const logs = await FileAttachment("weekend-logs.json").json();
315315
```
316316

317+
<a href="#attachment_csv" name="attachment_csv">#</a> *attachment*.<b>csv</b>({<i>array</i> = false, <i>typed</i> = false} = {}) [<>](https://github.com/observablehq/stdlib/blob/master/src/fileAttachment.js "Source")
318+
319+
Returns a promise to the file’s contents, parsed as comma-separated values (CSV) into an array.
320+
321+
```js
322+
const data = await FileAttachment("cars.csv").csv();
323+
```
324+
325+
If <i>array</i> is true, an array of arrays is returned; otherwise, the first row is assumed to be the header row and an array of objects is returned, and the returned array has a <i>data</i>.columns property that is an array of column names. (See <a href="https://github.com/d3/d3-dsv/blob/master/README.md#dsv_parseRows">d3.csvParseRows</a>.) If <i>typed</i> is true, [automatic type inference](https://observablehq.com/@d3/d3-autotype) is applied; only use this feature if you know your data is compatible.
326+
327+
<a href="#attachment_csv" name="attachment_tsv">#</a> *attachment*.<b>tsv</b>({<i>array</i> = false, <i>typed</i> = false} = {}) [<>](https://github.com/observablehq/stdlib/blob/master/src/fileAttachment.js "Source")
328+
329+
Returns a promise to the file’s contents, parsed as tab-separated values (TSV) into an array.
330+
331+
```js
332+
const data = await FileAttachment("cars.tsv").tsv();
333+
```
334+
335+
If <i>array</i> is true, an array of arrays is returned; otherwise, the first row is assumed to be the header row and an array of objects is returned, and the returned array has a <i>data</i>.columns property that is an array of column names. (See <a href="https://github.com/d3/d3-dsv/blob/master/README.md#dsv_parseRows">d3.tsvParseRows</a>.) If <i>typed</i> is true, [automatic type inference](https://observablehq.com/@d3/d3-autotype) is applied; only use this feature if you know your data is compatible.
336+
317337
<a href="#attachment_image" name="attachment_image">#</a> *attachment*.<b>image</b>() [<>](https://github.com/observablehq/stdlib/blob/master/src/fileAttachment.js "Source")
318338

319339
Returns a promise to a file loaded as an [Image](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/Image). The promise resolves when the image has finished loading, making this useful for reading the image pixels in Canvas, or for loading the image into a WebGL texture. Consider [*attachment*.url](#attachment_url) if you want to embed an image in HTML or Markdown.

src/fileAttachment.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1+
import {require as requireDefault} from "d3-require";
2+
13
async function remote_fetch(file) {
24
const response = await fetch(await file.url());
35
if (!response.ok) throw new Error(`Unable to load file: ${file.name}`);
46
return response;
57
}
68

9+
async function dsv(file, delimiter, {array = false, typed = false} = {}) {
10+
const [text, d3] = await Promise.all([file.text(), requireDefault("[email protected]/dist/d3-dsv.min.js")]);
11+
return (delimiter === "\t"
12+
? (array ? d3.tsvParseRows : d3.tsvParse)
13+
: (array ? d3.csvParseRows : d3.csvParse))(text, typed && d3.autoType);
14+
}
15+
716
class FileAttachment {
817
constructor(url, name) {
918
Object.defineProperties(this, {
@@ -29,6 +38,12 @@ class FileAttachment {
2938
async stream() {
3039
return (await remote_fetch(this)).body;
3140
}
41+
async csv(options) {
42+
return dsv(this, ",", options);
43+
}
44+
async tsv(options) {
45+
return dsv(this, "\t", options);
46+
}
3247
async image() {
3348
const url = await this.url();
3449
return new Promise((resolve, reject) => {

0 commit comments

Comments
 (0)