|
| 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 | +[](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) |
0 commit comments