Skip to content

Commit

Permalink
Parse autoType query parameter (#2)
Browse files Browse the repository at this point in the history
* Parse autoType query parameter

* Add usage notes
  • Loading branch information
erxclau authored Oct 14, 2022
1 parent 95fea80 commit d0a1e30
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# parcel-transformer-csv

CSV transformer for Parcel 2
CSV transformer for Parcel 2 using [`d3-dsv`](https://github.com/d3/d3-dsv)

## Usage

Expand All @@ -17,3 +17,15 @@ CSV transformer for Parcel 2
}
}
```

Now, you can import a CSV file like so:

```javascript
import data from "../data.csv"
```

By default, this transformer will use [`d3.autoType`](https://github.com/d3/d3-dsv#autoType) to infer data types. If you want to disable automatic typing, import with a `autoType=false` query parameter:

```javascript
import data from "../data.csv?autoType=false"
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@michigandaily/parcel-transformer-csv",
"version": "1.0.1",
"version": "1.0.2",
"description": "CSV transformer for Parcel 2",
"repository": "https://github.com/MichiganDaily/parcel-transformer-csv",
"author": "erxclau <ericlau41802@gmail.com>",
Expand Down
9 changes: 8 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ module.exports = new Transformer({
async transform({ asset }) {
const code = await asset.getCode();
asset.type = "js";
let parseFunction = `csvParse(\`${code}\`, autoType)`;
if (
asset.query.has("autoType") &&
asset.query.get("autoType") === "false"
) {
parseFunction = `csvParse(\`${code}\`)`;
}
asset.setCode(`
import { csvParse, autoType } from "d3-dsv";
export default csvParse(\`${code}\`, autoType);
export default ${parseFunction};
`);
return [asset];
},
Expand Down

0 comments on commit d0a1e30

Please sign in to comment.