Skip to content

Commit

Permalink
fix(core): ensure correct column order in CSV export (#2734)
Browse files Browse the repository at this point in the history
Closes #2718
  • Loading branch information
B4nan authored Nov 1, 2024
1 parent cfaa872 commit b66784f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
8 changes: 7 additions & 1 deletion packages/core/src/storages/dataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,13 @@ export class Dataset<Data extends Dictionary = Dictionary> {
const items = await this.export(options);

if (contentType === 'text/csv') {
const value = stringify([Object.keys(items[0]), ...items.map((item) => Object.values(item))]);
const keys = Object.keys(items[0]);
const value = stringify([
keys,
...items.map((item) => {
return keys.map((k) => item[k]);
}),
]);
await kvStore.setValue(key, value, { contentType });
return items;
}
Expand Down
20 changes: 15 additions & 5 deletions test/core/storages/dataset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,10 +486,16 @@ describe('dataset', () => {
describe('exportToJSON', () => {
const dataToPush = [
{
hello: 'world',
hello: 'world 1',
foo: 'bar 1',
},
{
foo: 'bar 2',
hello: 'world 2',
},
{
foo: 'bar',
hello: 'world 3',
foo: 'bar 3',
},
];

Expand Down Expand Up @@ -518,8 +524,12 @@ describe('dataset', () => {
foo: 'bar 1',
},
{
hello: 'world 2',
foo: 'bar 2',
hello: 'world 2',
},
{
hello: 'world 3',
foo: 'bar 3',
},
];

Expand All @@ -529,15 +539,15 @@ describe('dataset', () => {
await dataset.exportToCSV('HELLO-csv');

const kvData = await KeyValueStore.getValue('HELLO-csv');
expect(kvData).toEqual('hello,foo\nworld 1,bar 1\nworld 2,bar 2\n');
expect(kvData).toEqual('hello,foo\nworld 1,bar 1\nworld 2,bar 2\nworld 3,bar 3\n');
});

it('Should work as a static method for the default dataset', async () => {
await Dataset.pushData(dataToPush);
await Dataset.exportToCSV('TEST-123-123-csv');

const kvData = await KeyValueStore.getValue('TEST-123-123-csv');
expect(kvData).toEqual('hello,foo\nworld 1,bar 1\nworld 2,bar 2\n');
expect(kvData).toEqual('hello,foo\nworld 1,bar 1\nworld 2,bar 2\nworld 3,bar 3\n');
});
});
});
Expand Down

0 comments on commit b66784f

Please sign in to comment.