Skip to content

Commit 4bbd39f

Browse files
committed
fix: Simplify empty row check by removing complex regex
1 parent 1d18b89 commit 4bbd39f

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

Diff for: packages/parse/__tests__/issues/issue540.spec.ts

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { EOL } from 'os';
2+
import { parseString, RowMap, RowArray } from '../../src';
3+
4+
describe('Issue #540 - https://github.com/C2FO/fast-csv/issues/540', () => {
5+
const CSV_CONTENT = [
6+
' , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , -',
7+
].join(EOL);
8+
9+
const expectedRows = [
10+
[
11+
' , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,' +
12+
' , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,' +
13+
' , , , , , , , , , , , , , , -',
14+
],
15+
];
16+
17+
it('allow transforming to any object shape', () => {
18+
return new Promise((res, rej) => {
19+
const invalid: RowArray[] = [];
20+
const rows: RowMap[] = [];
21+
parseString(CSV_CONTENT, { ignoreEmpty: true, delimiter: '\t' })
22+
.on('data-invalid', (row: RowArray) => invalid.push(row))
23+
.on('data', (r) => rows.push(r))
24+
.on('error', rej)
25+
.on('end', (count: number) => {
26+
expect(rows).toEqual(expectedRows);
27+
expect(invalid).toHaveLength(0);
28+
expect(count).toBe(expectedRows.length + invalid.length);
29+
res();
30+
});
31+
});
32+
});
33+
});

Diff for: packages/parse/src/parser/Parser.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import { ParserOptions } from '../ParserOptions';
44
import { RowArray } from '../types';
55
import { Token } from './Token';
66

7-
const EMPTY_ROW_REGEXP = /^\s*(?:''|"")?\s*(?:,\s*(?:''|"")?\s*)*$/;
8-
97
export interface ParseResult {
108
line: string;
119
rows: string[][];
@@ -79,7 +77,7 @@ export class Parser {
7977
if (row === null) {
8078
return false;
8179
}
82-
if (this.parserOptions.ignoreEmpty && EMPTY_ROW_REGEXP.test(row.join(''))) {
80+
if (this.parserOptions.ignoreEmpty && RowParser.isEmptyRow(row)) {
8381
return true;
8482
}
8583
rows.push(row);

Diff for: packages/parse/src/parser/RowParser.ts

+6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import { ParserOptions } from '../ParserOptions';
44
import { RowArray } from '../types';
55
import { MaybeToken, Token } from './Token';
66

7+
const EMPTY_STRING = '';
8+
79
export class RowParser {
10+
static isEmptyRow(row: RowArray): boolean {
11+
return row.join(EMPTY_STRING).replace(/\s+/g, EMPTY_STRING) === EMPTY_STRING;
12+
}
13+
814
private readonly parserOptions: ParserOptions;
915

1016
private readonly columnParser: ColumnParser;

0 commit comments

Comments
 (0)