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

C2FO / fast-csv / 12025003900

26 Nov 2024 06:20AM UTC 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

94.74
/packages/format/src/index.ts
1
import { promisify } from 'util';
60✔
2
import { Writable } from 'stream';
60✔
3
import * as fs from 'fs';
60✔
4
import { Row } from './types';
5
import { FormatterOptions, FormatterOptionsArgs } from './FormatterOptions';
60✔
6
import { CsvFormatterStream } from './CsvFormatterStream';
60✔
7

8
export * from './types';
60✔
9
export { CsvFormatterStream } from './CsvFormatterStream';
366✔
10
export { FormatterOptions, FormatterOptionsArgs } from './FormatterOptions';
762✔
11

12
export const format = <I extends Row, O extends Row>(
60✔
13
    options?: FormatterOptionsArgs<I, O>,
14
): CsvFormatterStream<I, O> => {
15
    return new CsvFormatterStream(new FormatterOptions(options));
432✔
16
};
17

18
export const write = <I extends Row, O extends Row>(
60✔
19
    rows: I[],
20
    options?: FormatterOptionsArgs<I, O>,
21
): CsvFormatterStream<I, O> => {
22
    const csvStream = format(options);
420✔
23
    const promiseWrite = promisify((row: I, cb: (error?: Error | null) => void): void => {
420✔
24
        csvStream.write(row, undefined, cb);
930✔
25
    });
26
    rows.reduce((prev: Promise<void>, row: I): Promise<void> => {
420✔
27
        return prev.then((): Promise<void> => {
930✔
28
            return promiseWrite(row);
930✔
29
        });
30
    }, Promise.resolve())
31
        .then((): void => {
32
            csvStream.end();
420✔
33
        })
34
        .catch((err): void => {
35
            csvStream.emit('error', err);
×
36
        });
37
    return csvStream;
420✔
38
};
39

40
export const writeToStream = <T extends NodeJS.WritableStream, I extends Row, O extends Row>(
60✔
41
    ws: T,
42
    rows: I[],
43
    options?: FormatterOptionsArgs<I, O>,
44
): T => {
45
    return write(rows, options).pipe(ws);
54✔
46
};
47

48
export const writeToBuffer = <I extends Row, O extends Row>(
60✔
49
    rows: I[],
50
    opts: FormatterOptionsArgs<I, O> = {},
×
51
): Promise<Buffer> => {
52
    const buffers: Buffer[] = [];
168✔
53
    const ws = new Writable({
168✔
54
        write(data, enc, writeCb): void {
55
            buffers.push(data);
480✔
56
            writeCb();
480✔
57
        },
58
    });
59
    return new Promise((res, rej): void => {
168✔
60
        ws.on('error', rej).on('finish', (): void => {
168✔
61
            return res(Buffer.concat(buffers));
168✔
62
        });
63
        write(rows, opts).pipe(ws);
168✔
64
    });
65
};
66

67
export const writeToString = <I extends Row, O extends Row>(
60✔
68
    rows: I[],
69
    options?: FormatterOptionsArgs<I, O>,
70
): Promise<string> => {
71
    return writeToBuffer(rows, options).then((buffer): string => {
84✔
72
        return buffer.toString();
84✔
73
    });
74
};
75

76
export const writeToPath = <I extends Row, O extends Row>(
60✔
77
    path: string,
78
    rows: I[],
79
    options?: FormatterOptionsArgs<I, O>,
80
): fs.WriteStream => {
81
    const stream = fs.createWriteStream(path, { encoding: 'utf8' });
54✔
82
    return write(rows, options).pipe(stream);
54✔
83
};
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