Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@
* 2.0.
*/

// @ts-expect-error untyped lib
import { functionWrapper } from '../../../test_helpers/function_wrapper';
import { getFunctionErrors } from '../../../i18n';
import { csv } from './csv';
import { Datatable } from 'src/plugins/expressions';

const errors = getFunctionErrors().csv;

describe('csv', () => {
const fn = functionWrapper(csv);
const expected = {
const expected: Datatable = {
type: 'datatable',
columns: [
{ name: 'name', type: 'string' },
{ name: 'number', type: 'string' },
{ id: 'name', name: 'name', meta: { type: 'string' } },
{ id: 'number', name: 'number', meta: { type: 'string' } },
],
rows: [
{ name: 'one', number: '1' },
Expand Down Expand Up @@ -69,43 +71,47 @@ fourty two%SPLIT%42`,
});

it('should trim column names', () => {
expect(
fn(null, {
data: `foo," bar ", baz, " buz "
1,2,3,4`,
})
).toEqual({
const expectedResult: Datatable = {
type: 'datatable',
columns: [
{ name: 'foo', type: 'string' },
{ name: 'bar', type: 'string' },
{ name: 'baz', type: 'string' },
{ name: 'buz', type: 'string' },
{ id: 'foo', name: 'foo', meta: { type: 'string' } },
{ id: 'bar', name: 'bar', meta: { type: 'string' } },
{ id: 'baz', name: 'baz', meta: { type: 'string' } },
{ id: 'buz', name: 'buz', meta: { type: 'string' } },
],
rows: [{ foo: '1', bar: '2', baz: '3', buz: '4' }],
});
});
};

it('should handle odd spaces correctly', () => {
expect(
fn(null, {
data: `foo," bar ", baz, " buz "
1," best ",3, " ok"
" good", bad, better , " worst " `,
1,2,3,4`,
})
).toEqual({
).toEqual(expectedResult);
});

it('should handle odd spaces correctly', () => {
const expectedResult: Datatable = {
type: 'datatable',
columns: [
{ name: 'foo', type: 'string' },
{ name: 'bar', type: 'string' },
{ name: 'baz', type: 'string' },
{ name: 'buz', type: 'string' },
{ id: 'foo', name: 'foo', meta: { type: 'string' } },
{ id: 'bar', name: 'bar', meta: { type: 'string' } },
{ id: 'baz', name: 'baz', meta: { type: 'string' } },
{ id: 'buz', name: 'buz', meta: { type: 'string' } },
],
rows: [
{ foo: '1', bar: ' best ', baz: '3', buz: ' ok' },
{ foo: ' good', bar: ' bad', baz: ' better ', buz: ' worst ' },
],
});
};

expect(
fn(null, {
data: `foo," bar ", baz, " buz "
1," best ",3, " ok"
" good", bad, better , " worst " `,
})
).toEqual(expectedResult);
});

it('throws when given invalid csv', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ export function csv(): ExpressionFunctionDefinition<'csv', null, Arguments, Data
if (i === 0) {
// first row, assume header values
row.forEach((colName: string) =>
acc.columns.push({ name: colName.trim(), type: 'string' })
acc.columns.push({
id: colName.trim(),
name: colName.trim(),
meta: { type: 'string' },
})
);
} else {
// any other row is a data row
Expand Down