Skip to content

Commit b66784f

Browse files
authored
fix(core): ensure correct column order in CSV export (#2734)
Closes #2718
1 parent cfaa872 commit b66784f

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

packages/core/src/storages/dataset.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,13 @@ export class Dataset<Data extends Dictionary = Dictionary> {
347347
const items = await this.export(options);
348348

349349
if (contentType === 'text/csv') {
350-
const value = stringify([Object.keys(items[0]), ...items.map((item) => Object.values(item))]);
350+
const keys = Object.keys(items[0]);
351+
const value = stringify([
352+
keys,
353+
...items.map((item) => {
354+
return keys.map((k) => item[k]);
355+
}),
356+
]);
351357
await kvStore.setValue(key, value, { contentType });
352358
return items;
353359
}

test/core/storages/dataset.test.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -486,10 +486,16 @@ describe('dataset', () => {
486486
describe('exportToJSON', () => {
487487
const dataToPush = [
488488
{
489-
hello: 'world',
489+
hello: 'world 1',
490+
foo: 'bar 1',
491+
},
492+
{
493+
foo: 'bar 2',
494+
hello: 'world 2',
490495
},
491496
{
492-
foo: 'bar',
497+
hello: 'world 3',
498+
foo: 'bar 3',
493499
},
494500
];
495501

@@ -518,8 +524,12 @@ describe('dataset', () => {
518524
foo: 'bar 1',
519525
},
520526
{
521-
hello: 'world 2',
522527
foo: 'bar 2',
528+
hello: 'world 2',
529+
},
530+
{
531+
hello: 'world 3',
532+
foo: 'bar 3',
523533
},
524534
];
525535

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

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

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

539549
const kvData = await KeyValueStore.getValue('TEST-123-123-csv');
540-
expect(kvData).toEqual('hello,foo\nworld 1,bar 1\nworld 2,bar 2\n');
550+
expect(kvData).toEqual('hello,foo\nworld 1,bar 1\nworld 2,bar 2\nworld 3,bar 3\n');
541551
});
542552
});
543553
});

0 commit comments

Comments
 (0)