Skip to content

Commit

Permalink
feat(parsing): Less restrictive row parsing type #356
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-martin committed May 19, 2020
1 parent 357bcf7 commit 87d74ec
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
5 changes: 1 addition & 4 deletions packages/format/src/types.ts
@@ -1,9 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

export interface RowMap {
[key: string]: any;
}

export type RowMap = Record<string, any>;
export type RowHashArray = [string, any][];
export type RowArray = string[];
export type Row = RowArray | RowHashArray | RowMap;
Expand Down
37 changes: 37 additions & 0 deletions packages/parse/__tests__/issues/issue356.spec.ts
@@ -0,0 +1,37 @@
import { EOL } from 'os';
import { parseString, RowMap, RowArray } from '../../src';

describe('Issue #356 - https://github.com/C2FO/fast-csv/issues/356', () => {
interface InputRow {
clicks: string;
}

interface ClicksRow {
clicks?: number;
}

const CSV_CONTENT = ['clicks', `1`, '2', 'a'].join(EOL);
const expectedRows = [{ clicks: 1 }, { clicks: 2 }, {}];

it('allow transforming to any object shape', (done) => {
const invalid: RowArray[] = [];
const rows: RowMap[] = [];
parseString<InputRow, ClicksRow>(CSV_CONTENT, { headers: true, escape: "'" })
.transform((row: InputRow) => {
const clicks = parseInt(row.clicks, 10);
if (Number.isInteger(clicks)) {
return { clicks };
}
return {};
})
.on('data-invalid', (row: RowArray) => invalid.push(row))
.on('data', (r: ClicksRow) => rows.push(r))
.on('error', done)
.on('end', (count: number) => {
expect(rows).toEqual(expectedRows);
expect(invalid).toHaveLength(0);
expect(count).toBe(expectedRows.length + invalid.length);
done();
});
});
});
8 changes: 4 additions & 4 deletions packages/parse/src/types.ts
@@ -1,7 +1,7 @@
export interface RowMap {
[s: string]: string | undefined | null;
}
export type RowArray = string[];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type RowMap = Record<string, any>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type RowArray = any[];
export type Row = RowMap | RowArray;

export interface RowValidationResult<R extends Row> {
Expand Down

0 comments on commit 87d74ec

Please sign in to comment.