Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dsv): strip bom from csv/tsv files if necessary #1745

Merged
merged 4 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading