• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

C2FO / fast-csv / 13550863665

26 Feb 2025 06:30PM CUT coverage: 95.907%. Remained the same
13550863665

Pull #1101

github

web-flow
Merge 78434839f into 947bf442a
Pull Request #1101: chore(deps): update node.js to v22

325 of 353 branches covered (92.07%)

Branch coverage included in aggregate %.

753 of 771 relevant lines covered (97.67%)

2176197.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.36
/packages/parse/src/parser/Parser.ts
1
import { Scanner } from './Scanner';
144✔
2
import { RowParser } from './RowParser';
144✔
3
import { ParserOptions } from '../ParserOptions';
4
import { RowArray } from '../types';
5
import { Token } from './Token';
144✔
6

7
export interface ParseResult {
8
    line: string;
9
    rows: string[][];
10
}
11
export class Parser {
144✔
12
    private static removeBOM(line: string): string {
13
        // Catches EFBBBF (UTF-8 BOM) because the buffer-to-string
14
        // conversion translates it to FEFF (UTF-16 BOM)
15
        if (line && line.charCodeAt(0) === 0xfeff) {
2,718✔
16
            return line.slice(1);
12✔
17
        }
18
        return line;
2,706✔
19
    }
20

21
    private readonly parserOptions: ParserOptions;
22

23
    private readonly rowParser: RowParser;
24

25
    public constructor(parserOptions: ParserOptions) {
26
        this.parserOptions = parserOptions;
966✔
27
        this.rowParser = new RowParser(this.parserOptions);
966✔
28
    }
29

30
    public parse(line: string, hasMoreData: boolean): ParseResult {
31
        const scanner = new Scanner({
2,718✔
32
            line: Parser.removeBOM(line),
33
            parserOptions: this.parserOptions,
34
            hasMoreData,
35
        });
36
        if (this.parserOptions.supportsComments) {
2,718✔
37
            return this.parseWithComments(scanner);
54✔
38
        }
39
        return this.parseWithoutComments(scanner);
2,664✔
40
    }
41

42
    private parseWithoutComments(scanner: Scanner): ParseResult {
43
        const rows: RowArray[] = [];
2,664✔
44
        let shouldContinue = true;
2,664✔
45
        while (shouldContinue) {
2,664✔
46
            shouldContinue = this.parseRow(scanner, rows);
735,486✔
47
        }
48
        return { line: scanner.line, rows };
2,634✔
49
    }
50

51
    private parseWithComments(scanner: Scanner): ParseResult {
52
        const { parserOptions } = this;
54✔
53
        const rows: RowArray[] = [];
54✔
54
        for (let nextToken = scanner.nextCharacterToken; nextToken !== null; nextToken = scanner.nextCharacterToken) {
54✔
55
            if (Token.isTokenComment(nextToken, parserOptions)) {
102✔
56
                const cursor = scanner.advancePastLine();
54✔
57
                if (cursor === null) {
54✔
58
                    return { line: scanner.lineFromCursor, rows };
18✔
59
                }
60
                if (!scanner.hasMoreCharacters) {
36✔
61
                    return { line: scanner.lineFromCursor, rows };
12✔
62
                }
63
                scanner.truncateToCursor();
24✔
64
            } else if (!this.parseRow(scanner, rows)) {
48!
65
                break;
×
66
            }
67
        }
68
        return { line: scanner.line, rows };
24✔
69
    }
70

71
    private parseRow(scanner: Scanner, rows: RowArray[]): boolean {
72
        const nextToken = scanner.nextNonSpaceToken;
735,534✔
73
        if (!nextToken) {
735,534✔
74
            return false;
750✔
75
        }
76
        const row = this.rowParser.parse(scanner);
734,784✔
77
        if (row === null) {
734,754✔
78
            return false;
1,884✔
79
        }
80
        if (this.parserOptions.ignoreEmpty && RowParser.isEmptyRow(row)) {
732,870✔
81
            return true;
36✔
82
        }
83
        rows.push(row);
732,834✔
84
        return true;
732,834✔
85
    }
86
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc