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

C2FO / fast-csv / 12025003900

26 Nov 2024 06:20AM CUT coverage: 95.889%. Remained the same
12025003900

push

github

web-flow
chore(deps): update dependency @types/node to v22.10.0 (#1067)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

321 of 349 branches covered (91.98%)

Branch coverage included in aggregate %.

752 of 770 relevant lines covered (97.66%)

2179022.73 hits per line

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

84.78
/packages/format/src/CsvFormatterStream.ts
1
import { Transform, TransformCallback } from 'stream';
60✔
2
import { FormatterOptions } from './FormatterOptions';
3
import { Row, RowTransformFunction } from './types';
4
import { RowFormatter } from './formatter';
60✔
5

6
export class CsvFormatterStream<I extends Row, O extends Row> extends Transform {
60✔
7
    private formatterOptions: FormatterOptions<I, O>;
8

9
    private rowFormatter: RowFormatter<I, O>;
10

11
    private hasWrittenBOM = false;
738✔
12

13
    public constructor(formatterOptions: FormatterOptions<I, O>) {
14
        super({ writableObjectMode: formatterOptions.objectMode });
738✔
15
        this.formatterOptions = formatterOptions;
738✔
16
        this.rowFormatter = new RowFormatter(formatterOptions);
738✔
17
        // if writeBOM is false then set to true
18
        // if writeBOM is true then set to false by default so it is written out
19
        this.hasWrittenBOM = !formatterOptions.writeBOM;
738✔
20
    }
21

22
    public transform(transformFunction: RowTransformFunction<I, O>): CsvFormatterStream<I, O> {
23
        this.rowFormatter.rowTransform = transformFunction;
24✔
24
        return this;
24✔
25
    }
26

27
    public _transform(row: I, encoding: string, cb: TransformCallback): void {
28
        let cbCalled = false;
1,584✔
29
        try {
1,584✔
30
            if (!this.hasWrittenBOM) {
1,584✔
31
                this.push(this.formatterOptions.BOM);
6✔
32
                this.hasWrittenBOM = true;
6✔
33
            }
34
            this.rowFormatter.format(row, (err, rows): void => {
1,584✔
35
                if (err) {
1,578!
36
                    cbCalled = true;
×
37
                    return cb(err);
×
38
                }
39
                if (rows) {
1,578✔
40
                    rows.forEach((r): void => {
1,578✔
41
                        this.push(Buffer.from(r, 'utf8'));
2,058✔
42
                    });
43
                }
44
                cbCalled = true;
1,578✔
45
                return cb();
1,578✔
46
            });
47
        } catch (e) {
48
            if (cbCalled) {
6!
49
                throw e;
×
50
            }
51
            cb(e);
6✔
52
        }
53
    }
54

55
    public _flush(cb: TransformCallback): void {
56
        this.rowFormatter.finish((err, rows): void => {
732✔
57
            if (err) {
732✔
58
                return cb(err);
6✔
59
            }
60
            if (rows) {
726✔
61
                rows.forEach((r): void => {
726✔
62
                    this.push(Buffer.from(r, 'utf8'));
60✔
63
                });
64
            }
65
            return cb();
726✔
66
        });
67
    }
68
}
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