Skip to content

Commit

Permalink
fix(dsv): strip bom from csv/tsv files if necessary (#1745)
Browse files Browse the repository at this point in the history
* fix(dsv): strip bom from csv/tsv files if necessary

* resolve conflicts in pnpm-lock.yaml

* update pnpm-lock.yaml

---------

Co-authored-by: shellscape <[email protected]>
  • Loading branch information
CrazyOrr and shellscape authored Sep 23, 2024
1 parent a100887 commit 8c24fb6
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/dsv/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@rollup/pluginutils": "^5.0.1",
"@types/d3-dsv": "^3.0.0",
"d3-dsv": "2.0.0",
"strip-bom": "^4.0.0",
"tosource": "^2.0.0-alpha.3"
},
"devDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion packages/dsv/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { extname } from 'path';
import { csvParse, tsvParse } from 'd3-dsv';
import toSource from 'tosource';
import { createFilter } from '@rollup/pluginutils';
import stripBom from 'strip-bom';

const parsers = { '.csv': csvParse, '.tsv': tsvParse };

Expand All @@ -18,7 +19,7 @@ export default function dsv(options = {}) {
const ext = extname(id);
if (!(ext in parsers)) return null;

let rows = parsers[ext](code);
let rows = parsers[ext](stripBom(code));

if (options.processRow) {
rows = rows.map((row) => options.processRow(row, id) || row);
Expand Down
4 changes: 4 additions & 0 deletions packages/dsv/test/fixtures/csv-with-bom/fruit.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type,count
apples,7
pears,4
bananas,5
7 changes: 7 additions & 0 deletions packages/dsv/test/fixtures/csv-with-bom/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import fruit from './fruit.csv';

t.deepEqual(fruit, [
{ type: 'apples', count: '7' },
{ type: 'pears', count: '4' },
{ type: 'bananas', count: '5' }
]);
4 changes: 4 additions & 0 deletions packages/dsv/test/fixtures/tsv-with-bom/fruit.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type count
apples 7
pears 4
bananas 5
7 changes: 7 additions & 0 deletions packages/dsv/test/fixtures/tsv-with-bom/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import fruit from './fruit.tsv';

t.deepEqual(fruit, [
{ type: 'apples', count: '7' },
{ type: 'pears', count: '4' },
{ type: 'bananas', count: '5' }
]);
18 changes: 18 additions & 0 deletions packages/dsv/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ test('converts a csv file', async (t) => {
return testBundle(t, bundle);
});

test('converts a csv file with bom', async (t) => {
const bundle = await rollup({
input: 'fixtures/csv-with-bom/main.js',
plugins: [dsv()]
});
t.plan(1);
return testBundle(t, bundle);
});

test('converts a tsv file', async (t) => {
const bundle = await rollup({
input: 'fixtures/basic-tsv/main.js',
Expand All @@ -31,6 +40,15 @@ test('converts a tsv file', async (t) => {
return testBundle(t, bundle);
});

test('converts a tsv file with bom', async (t) => {
const bundle = await rollup({
input: 'fixtures/tsv-with-bom/main.js',
plugins: [dsv()]
});
t.plan(1);
return testBundle(t, bundle);
});

test('uses a custom processor', async (t) => {
const parse = (value) => (isNaN(+value) ? value : +value);

Expand Down
11 changes: 10 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8c24fb6

Please sign in to comment.