Skip to content

Commit 9bc503a

Browse files
authored
chore: add rollup-plugin-dsv (rollup#22)
1 parent 8121cd1 commit 9bc503a

17 files changed

+376
-1
lines changed

packages/dsv/CHANGELOG.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# rollup-plugin-dsv changelog
2+
3+
## 1.2.0
4+
5+
* Pass `id` to `processRow`
6+
7+
## 1.1.2
8+
9+
* Return a `name`
10+
11+
## 1.1.1
12+
13+
* Add missing dependencies
14+
15+
## 1.1.0
16+
17+
* Support `options.processRow`
18+
19+
## 1.0.1
20+
21+
* Include correct files in package
22+
23+
## 1.0.0
24+
25+
* First release

packages/dsv/README.md

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
[cover]: https://codecov.io/gh/rollup/plugins/replace/branch/master/graph/badge.svg
2+
[cover-url]: https://codecov.io/gh/rollup/plugins
3+
[size]: https://packagephobia.now.sh/badge?p=@rollup/plugin-dsv
4+
[size-url]: https://packagephobia.now.sh/result?p=@rollup/plugin-dsv
5+
[tests]: https://img.shields.io/circleci/project/github/rollup/plugins.svg
6+
[tests-url]: https://circleci.com/gh/rollup/plugins
7+
8+
[![tests][tests]][tests-url]
9+
[![cover][cover]][cover-url]
10+
[![size][size]][size-url]
11+
[![libera manifesto](https://img.shields.io/badge/libera-manifesto-lightgrey.svg)](https://liberamanifesto.com)
12+
13+
# @rollup/plugin-dsv
14+
15+
🍣 A Rollup plugin which converts `.csv` and `.tsv` files into JavaScript modules with [d3-dsv](https://github.com/d3/d3-dsv).
16+
17+
WebAssembly Modules are imported asynchronous as base64 strings. Small modules [can be imported synchronously](#synchronous-modules).
18+
19+
## Requirements
20+
21+
This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v8.0.0+) and Rollup v1.20.0+.
22+
23+
## Install
24+
25+
Using npm:
26+
27+
```console
28+
npm install @rollup/plugin-dsv --save-dev
29+
```
30+
31+
## Usage
32+
33+
Create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files) and import the plugin:
34+
35+
```js
36+
import dsv from '@rollup/plugin-dsv';
37+
38+
export default {
39+
input: 'src/index.js',
40+
output: {
41+
dir: 'output',
42+
format: 'cjs'
43+
},
44+
plugins: [dsv()]
45+
};
46+
```
47+
48+
Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#command-line-reference) or the [API](https://www.rollupjs.org/guide/en/#javascript-api).
49+
50+
## Practical Example
51+
52+
Suppose that you have a CSV (or TSV!) file which contains some information on delicious fruits:
53+
54+
```csv
55+
type,count
56+
apples,7
57+
pears,4
58+
bananas,5
59+
```
60+
61+
And suppose you'd like to import that CSV as an `Array` within some part of your code. After adding the plugin (as shown above), you may `import` (or `require`) the CSV file directly. The import will provide an `Array` of `Objects` representing rows from the CSV file:
62+
63+
```js
64+
import fruit from './fruit.csv';
65+
66+
console.log(fruit);
67+
// [
68+
// { type: 'apples', count: '7' },
69+
// { type: 'pears', count: '4' },
70+
// { type: 'bananas', count: '5' }
71+
// ]
72+
```
73+
74+
## Options
75+
76+
### `processRow`
77+
78+
Type: `Function`<br>
79+
Default: `null`
80+
81+
Specifies a function which processes each row in the parsed array. The function can either manipulate the passed `row`, or return an entirely new row object.
82+
83+
This option could be used for converting numeric `string` values into `Number` values. – for example turning numeric values into numbers, e.g.
84+
85+
```js
86+
dsv({
87+
processRow: (row, id) => {
88+
Object.keys(row).forEach(key => {
89+
var value = row[key];
90+
row[key] = isNaN(+value) ? value : +value;
91+
});
92+
}
93+
});
94+
```
95+
96+
## Meta
97+
98+
[CONTRIBUTING](./.github/CONTRIBUTING.md)
99+
100+
[LICENSE (MIT)](./LICENSE)

packages/dsv/package.json

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"name": "@rollup/plugin-dsv",
3+
"version": "2.0.0",
4+
"description": "Convert .csv and .tsv files into JavaScript modules with d3-dsv",
5+
"license": "MIT",
6+
"repository": "rollup/plugins",
7+
"author": "Rich Harris",
8+
"homepage": "https://github.com/rollup/plugins",
9+
"bugs": "https://github.com/rollup/plugins/issues",
10+
"main": "dist/index.js",
11+
"scripts": {
12+
"build": "rollup -c",
13+
"ci:coverage": "nyc pnpm run test && nyc report --reporter=text-lcov > coverage.lcov",
14+
"ci:coverage:submit": "curl -s https://codecov.io/bash | bash -s - -F dsv",
15+
"ci:lint": "pnpm run build && pnpm run lint && pnpm run security",
16+
"ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}",
17+
"ci:test": "pnpm run test -- --verbose",
18+
"lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:package",
19+
"lint:docs": "prettier --single-quote --write README.md",
20+
"lint:js": "eslint --fix --cache src test",
21+
"lint:package": "prettier --write package.json --plugin=prettier-plugin-package",
22+
"prebuild": "del-cli dist",
23+
"prepare": "npm run build",
24+
"prepublishOnly": "npm run lint",
25+
"pretest": "npm run build",
26+
"security": "echo 'pnpm needs `npm audit` support'",
27+
"test": "ava"
28+
},
29+
"files": [
30+
"dist",
31+
"README.md",
32+
"LICENSE"
33+
],
34+
"dependencies": {
35+
"d3-dsv": "^0.1.14",
36+
"rollup-pluginutils": "^2.8.2",
37+
"tosource": "^1.0.0"
38+
},
39+
"devDependencies": {
40+
"del-cli": "^3.0.0",
41+
"rollup": "^1.20.0"
42+
},
43+
"ava": {
44+
"files": [
45+
"!**/fixtures/**",
46+
"!**/helpers/**",
47+
"!**/recipes/**",
48+
"!**/types.ts"
49+
]
50+
},
51+
"jsnext:main": "dist/index.es.js",
52+
"module": "dist/index.es.js"
53+
}

packages/dsv/rollup.config.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import pkg from './package.json';
2+
3+
export default {
4+
input: 'src/index.js',
5+
output: [{ file: pkg.main, format: 'cjs' }, { file: pkg.module, format: 'es' }]
6+
};

packages/dsv/src/index.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { extname } from 'path';
2+
3+
import { csv, tsv } from 'd3-dsv';
4+
import toSource from 'tosource';
5+
import { createFilter } from 'rollup-pluginutils';
6+
7+
const parsers = { '.csv': csv, '.tsv': tsv };
8+
9+
export default function dsv(options = {}) {
10+
const filter = createFilter(options.include, options.exclude);
11+
12+
return {
13+
name: 'dsv',
14+
15+
transform(code, id) {
16+
if (!filter(id)) return null;
17+
18+
const ext = extname(id);
19+
if (!(ext in parsers)) return null;
20+
21+
let rows = parsers[ext].parse(code);
22+
23+
if (options.processRow) {
24+
rows = rows.map((row) => options.processRow(row, id) || row);
25+
}
26+
27+
return {
28+
code: `export default ${toSource(rows)};`,
29+
map: { mappings: '' }
30+
};
31+
}
32+
};
33+
}

packages/dsv/test/fixtures/.eslintrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"globals": {
3+
"t": "readonly"
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
type,count
2+
apples,7
3+
pears,4
4+
bananas,5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import fruit from './fruit.csv';
2+
3+
t.deepEqual(fruit, [
4+
{ type: 'apples', count: '7' },
5+
{ type: 'pears', count: '4' },
6+
{ type: 'bananas', count: '5' }
7+
]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
type count
2+
apples 7
3+
pears 4
4+
bananas 5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import fruit from './fruit.tsv';
2+
3+
t.deepEqual(fruit, [
4+
{ type: 'apples', count: '7' },
5+
{ type: 'pears', count: '4' },
6+
{ type: 'bananas', count: '5' }
7+
]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
type,count
2+
Apples,7
3+
Pears,4
4+
Bananas,5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import lower from './lower.csv';
2+
import upper from './upper.csv';
3+
4+
t.deepEqual(lower, [
5+
{ type: 'apples', count: 7 },
6+
{ type: 'pears', count: 4 },
7+
{ type: 'bananas', count: 5 }
8+
]);
9+
10+
t.deepEqual(upper, [
11+
{ type: 'APPLES', count: 7 },
12+
{ type: 'PEARS', count: 4 },
13+
{ type: 'BANANAS', count: 5 }
14+
]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
type,count
2+
Apples,7
3+
Pears,4
4+
Bananas,5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
type,count
2+
apples,7
3+
pears,4
4+
bananas,5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import fruit from './fruit.csv';
2+
3+
t.deepEqual(fruit, [
4+
{ type: 'apples', count: 7 },
5+
{ type: 'pears', count: 4 },
6+
{ type: 'bananas', count: 5 }
7+
]);

packages/dsv/test/test.js

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
const test = require('ava');
2+
const { rollup } = require('rollup');
3+
4+
const dsv = require('..');
5+
6+
process.chdir(__dirname);
7+
8+
const testBundle = async (t, bundle) => {
9+
const { output } = await bundle.generate({ format: 'cjs' });
10+
const [{ code }] = output;
11+
const func = new Function('t', code); // eslint-disable-line no-new-func
12+
13+
return func(t);
14+
};
15+
16+
test('converts a csv file', async (t) => {
17+
const bundle = await rollup({
18+
input: 'fixtures/basic-csv/main.js',
19+
plugins: [dsv()]
20+
});
21+
t.plan(1);
22+
return testBundle(t, bundle);
23+
});
24+
25+
test('converts a tsv file', async (t) => {
26+
const bundle = await rollup({
27+
input: 'fixtures/basic-tsv/main.js',
28+
plugins: [dsv()]
29+
});
30+
t.plan(1);
31+
return testBundle(t, bundle);
32+
});
33+
34+
test('uses a custom processor', async (t) => {
35+
const parse = (value) => (isNaN(+value) ? value : +value);
36+
37+
const bundle = await rollup({
38+
input: 'fixtures/process/main.js',
39+
plugins: [
40+
dsv({
41+
processRow(row) {
42+
Object.keys(row).forEach((key) => {
43+
row[key] = parse(row[key]); // eslint-disable-line no-param-reassign
44+
});
45+
}
46+
})
47+
]
48+
});
49+
t.plan(1);
50+
return testBundle(t, bundle);
51+
});
52+
53+
test('uses a custom processor with id', async (t) => {
54+
const bundle = await rollup({
55+
input: 'fixtures/process-id/main.js',
56+
plugins: [
57+
dsv({
58+
processRow(row, id) {
59+
return {
60+
type: row.type[/lower/.test(id) ? 'toLowerCase' : 'toUpperCase'](),
61+
count: +row.count
62+
};
63+
}
64+
})
65+
]
66+
});
67+
t.plan(2);
68+
return testBundle(t, bundle);
69+
});

0 commit comments

Comments
 (0)