Skip to content

Commit

Permalink
fix: Simplify empty row check by removing complex regex
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-martin committed Dec 4, 2020
1 parent 1d18b89 commit 4bbd39f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
33 changes: 33 additions & 0 deletions packages/parse/__tests__/issues/issue540.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { EOL } from 'os';
import { parseString, RowMap, RowArray } from '../../src';

describe('Issue #540 - https://github.com/C2FO/fast-csv/issues/540', () => {
const CSV_CONTENT = [
' , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , -',
].join(EOL);

const expectedRows = [
[
' , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,' +
' , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,' +
' , , , , , , , , , , , , , , -',
],
];

it('allow transforming to any object shape', () => {
return new Promise((res, rej) => {
const invalid: RowArray[] = [];
const rows: RowMap[] = [];
parseString(CSV_CONTENT, { ignoreEmpty: true, delimiter: '\t' })
.on('data-invalid', (row: RowArray) => invalid.push(row))
.on('data', (r) => rows.push(r))
.on('error', rej)
.on('end', (count: number) => {
expect(rows).toEqual(expectedRows);
expect(invalid).toHaveLength(0);
expect(count).toBe(expectedRows.length + invalid.length);
res();
});
});
});
});
4 changes: 1 addition & 3 deletions packages/parse/src/parser/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { ParserOptions } from '../ParserOptions';
import { RowArray } from '../types';
import { Token } from './Token';

const EMPTY_ROW_REGEXP = /^\s*(?:''|"")?\s*(?:,\s*(?:''|"")?\s*)*$/;

export interface ParseResult {
line: string;
rows: string[][];
Expand Down Expand Up @@ -79,7 +77,7 @@ export class Parser {
if (row === null) {
return false;
}
if (this.parserOptions.ignoreEmpty && EMPTY_ROW_REGEXP.test(row.join(''))) {
if (this.parserOptions.ignoreEmpty && RowParser.isEmptyRow(row)) {
return true;
}
rows.push(row);
Expand Down
6 changes: 6 additions & 0 deletions packages/parse/src/parser/RowParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { ParserOptions } from '../ParserOptions';
import { RowArray } from '../types';
import { MaybeToken, Token } from './Token';

const EMPTY_STRING = '';

export class RowParser {
static isEmptyRow(row: RowArray): boolean {
return row.join(EMPTY_STRING).replace(/\s+/g, EMPTY_STRING) === EMPTY_STRING;
}

private readonly parserOptions: ParserOptions;

private readonly columnParser: ColumnParser;
Expand Down

0 comments on commit 4bbd39f

Please sign in to comment.