Skip to content

Commit

Permalink
Merge pull request #1 from cbrittingham/maxLines-#275
Browse files Browse the repository at this point in the history
Feature: add maxRows for C2FO#275
  • Loading branch information
cbrittingham authored Aug 27, 2019
2 parents 3deb16b + 9589054 commit 0cc34c8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/parsing.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
* `rtrim: {boolean} = false`: Set to `true` to right trim all fields.
* `ltrim: {boolean} = false`: Set to `true` to left trim all fields.
* `encoding: {string} = 'utf8'`: Passed to [StringDecoder](https://nodejs.org/api/string_decoder.html#string_decoder_new_stringdecoder_encoding) when decoding incoming buffers. Change if incoming content is not 'utf8' encoded.
- `maxRows: {number}`: Up to the given number of rows will be returned if set to a number greater than 0 (e.g., `100` would return the first 100 rows of data).

<a name="parsing-events"></a>
## Events
Expand Down
9 changes: 9 additions & 0 deletions src/parser/ParserOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface ParserOptionsArgs{
ltrim?: boolean;
rtrim?: boolean;
encoding?: string;
maxRows?: number;
}

export class ParserOptions {
Expand Down Expand Up @@ -56,6 +57,10 @@ export class ParserOptions {

public readonly encoding: string = 'utf8';

public readonly limitRows: boolean = false;

public readonly maxRows: number = -1;

public constructor(opts?: ParserOptionsArgs) {
Object.assign(this, opts || {});
if (this.delimiter.length > 1) {
Expand All @@ -65,5 +70,9 @@ export class ParserOptions {
this.escapeChar = isString(this.escape) ? this.escape : this.quote;
this.supportsComments = !isNil(this.comment);
this.NEXT_TOKEN_REGEXP = new RegExp(`([^\\s]|\\r\\n|\\n|\\r|${this.escapedDelimiter})`);

if (this.maxRows > 0) {
this.limitRows = true;
}
}
}
5 changes: 4 additions & 1 deletion src/parser/parser/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ export default class Parser {
return { line: scanner.line, rows };
}

private rowCount: number = 0;

private parseRow(scanner: Scanner, rows: RowArray[]): boolean {
const nextToken = scanner.nextNonSpaceToken;
if (!nextToken) {
if (!nextToken || (this.parserOptions.limitRows && this.rowCount >= this.parserOptions.maxRows)) {
return false;
}
const row = this.rowParser.parse(scanner);
Expand All @@ -82,6 +84,7 @@ export default class Parser {
return true;
}
rows.push(row);
this.rowCount += 1;
return true;
}
}

0 comments on commit 0cc34c8

Please sign in to comment.