• 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

98.11
/packages/format/src/formatter/FieldFormatter.ts
1
import isBoolean from 'lodash.isboolean';
60✔
2
import isNil from 'lodash.isnil';
60✔
3
import escapeRegExp from 'lodash.escaperegexp';
60✔
4
import { FormatterOptions } from '../FormatterOptions';
5
import { Row } from '../types';
6

7
export class FieldFormatter<I extends Row, O extends Row> {
60✔
8
    private readonly formatterOptions: FormatterOptions<I, O>;
9

10
    private _headers: string[] | null = null;
1,134✔
11

12
    private readonly REPLACE_REGEXP: RegExp;
13

14
    private readonly ESCAPE_REGEXP: RegExp;
15

16
    public constructor(formatterOptions: FormatterOptions<I, O>) {
17
        this.formatterOptions = formatterOptions;
1,134✔
18
        if (formatterOptions.headers !== null) {
1,134✔
19
            this.headers = formatterOptions.headers;
156✔
20
        }
21
        this.REPLACE_REGEXP = new RegExp(formatterOptions.quote, 'g');
1,134✔
22
        const escapePattern = `[${formatterOptions.delimiter}${escapeRegExp(formatterOptions.rowDelimiter)}|\r|\n]`;
1,134✔
23
        this.ESCAPE_REGEXP = new RegExp(escapePattern);
1,134✔
24
    }
25

26
    public set headers(headers: string[]) {
27
        this._headers = headers;
1,092✔
28
    }
29

30
    private shouldQuote(fieldIndex: number, isHeader: boolean): boolean {
31
        const quoteConfig = isHeader ? this.formatterOptions.quoteHeaders : this.formatterOptions.quoteColumns;
4,866✔
32
        if (isBoolean(quoteConfig)) {
4,866✔
33
            return quoteConfig;
4,338✔
34
        }
35
        if (Array.isArray(quoteConfig)) {
528✔
36
            return quoteConfig[fieldIndex];
168✔
37
        }
38
        if (this._headers !== null) {
360✔
39
            return quoteConfig[this._headers[fieldIndex]];
360✔
40
        }
41
        return false;
×
42
    }
43

44
    public format(field: string, fieldIndex: number, isHeader: boolean): string {
45
        const preparedField = `${isNil(field) ? '' : field}`.replace(/\0/g, '');
4,998✔
46
        const { formatterOptions } = this;
4,998✔
47
        if (formatterOptions.quote !== '') {
4,998✔
48
            const shouldEscape = preparedField.indexOf(formatterOptions.quote) !== -1;
4,932✔
49
            if (shouldEscape) {
4,932✔
50
                return this.quoteField(preparedField.replace(this.REPLACE_REGEXP, formatterOptions.escapedQuote));
18✔
51
            }
52
        }
53
        const hasEscapeCharacters = preparedField.search(this.ESCAPE_REGEXP) !== -1;
4,980✔
54
        if (hasEscapeCharacters || this.shouldQuote(fieldIndex, isHeader)) {
4,980✔
55
            return this.quoteField(preparedField);
600✔
56
        }
57
        return preparedField;
4,380✔
58
    }
59

60
    private quoteField(field: string): string {
61
        const { quote } = this.formatterOptions;
618✔
62
        return `${quote}${field}${quote}`;
618✔
63
    }
64
}
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