Skip to content

Commit

Permalink
feat: add exclude unknown columns with options
Browse files Browse the repository at this point in the history
  • Loading branch information
Th1nkK1D committed May 4, 2024
1 parent 6e8ebfc commit 0b158d4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
28 changes: 16 additions & 12 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import type { TColumnsDefinition } from './table';

export interface CSVParserOptions {
trim?: boolean;
excludeUnknownColumns?: boolean;
}

const defaultCSVParserOptions: CSVParserOptions = {
const defaultCSVParserOptions: Record<keyof CSVParserOptions, boolean> = {
trim: true,
excludeUnknownColumns: true,
};

export async function parseCSVFromUrl<C extends TObject<TColumnsDefinition>>(
Expand Down Expand Up @@ -41,21 +43,23 @@ export function parseCSVFromString<C extends TObject<TColumnsDefinition>>(
csvParse(csvString, processRow(mergedOptions.trim)),
);

if (mergedOptions.excludeUnknownColumns) {
Value.Clean(outputSchema, rows);
}

if (!Value.Check(outputSchema, rows)) {
throw [...Value.Errors(outputSchema, rows)];
}

return rows;
}

const processRow =
(trim = true) =>
(obj: Record<any, string>) =>
Object.entries(obj).reduce<Record<any, string | null>>(
(newObj, [key, value]) => {
const newValue = trim ? value.trim() : value;
newObj[key] = newValue.length > 0 ? newValue : null;
return newObj;
},
{},
);
const processRow = (trim: boolean) => (obj: Record<any, string>) =>
Object.entries(obj).reduce<Record<any, string | null>>(
(newObj, [key, value]) => {
const newValue = trim ? value.trim() : value;
newObj[key] = newValue.length > 0 ? newValue : null;
return newObj;
},
{},
);
36 changes: 19 additions & 17 deletions tests/parser/parse-csv-from-string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,29 +202,31 @@ describe('parseCSVFromString', () => {
});

describe('options', () => {
it('should trim value before parsing by default', async () => {
const input = 'value\n hi ';
const table = Table({
value: Column.String(),
});

const res = parseCSVFromString(
input,
Table({
value: Column.String(),
}),
);
it('should trim value before parsing by default', async () => {
const res = parseCSVFromString('value\n hi ', table);
expect(res).toEqual([{ value: 'hi' }]);
});

it('should not trim if option is set to false', async () => {
const input = 'value\n hi ';

const res = parseCSVFromString(
input,
Table({
value: Column.String(),
}),
{ trim: false },
);
const res = parseCSVFromString('value\n hi ', table, { trim: false });
expect(res).toEqual([{ value: ' hi ' }]);
});

it('should exclude unknown columns by default', async () => {
const res = parseCSVFromString('value,unknown\na,b', table);
expect(res).toEqual([{ value: 'a' }]);
});

it('should not exclude unknown columns if set to false', async () => {
const res = parseCSVFromString('value,unknown\na,b', table, {
excludeUnknownColumns: false,
});
// @ts-ignore
expect(res).toEqual([{ value: 'a', unknown: 'b' }]);
});
});
});

0 comments on commit 0b158d4

Please sign in to comment.