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

IgniteUI / igniteui-angular / 13331632524

14 Feb 2025 02:51PM CUT coverage: 22.015% (-69.6%) from 91.622%
13331632524

Pull #15372

github

web-flow
Merge d52d57714 into bcb78ae0a
Pull Request #15372: chore(*): test ci passing

1990 of 15592 branches covered (12.76%)

431 of 964 new or added lines in 18 files covered. (44.71%)

19956 existing lines in 307 files now uncovered.

6452 of 29307 relevant lines covered (22.02%)

249.17 hits per line

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

0.0
/projects/igniteui-angular/src/lib/services/csv/char-separated-value-data.ts
1
import { ExportUtilities } from '../exporter-common/export-utilities';
2
import { yieldingLoop } from '../../core/utils';
3
import { IColumnInfo } from '../exporter-common/base-export-service';
4

5
/**
6
 * @hidden
7
 */
8
export class CharSeparatedValueData {
UNCOV
9
    private _headerRecord = '';
×
UNCOV
10
    private _dataRecords = '';
×
UNCOV
11
    private _eor = '\r\n';
×
12
    private _delimiter;
UNCOV
13
    private _escapeCharacters = ['\r', '\n', '\r\n'];
×
UNCOV
14
    private _delimiterLength = 1;
×
UNCOV
15
    private _isSpecialData = false;
×
16

UNCOV
17
    constructor(private _data: any[], valueDelimiter: string, private columns: IColumnInfo[] = [])  {
×
UNCOV
18
        this.setDelimiter(valueDelimiter);
×
19
    }
20

21
    public prepareData(key?: any[]) {
UNCOV
22
        if (!this._data || this._data.length === 0) {
×
UNCOV
23
            return '';
×
24
        }
UNCOV
25
        let keys = [];
×
UNCOV
26
        if (key){
×
27
            keys = key;
×
28
        }else {
UNCOV
29
            keys = ExportUtilities.getKeysFromData(this._data);
×
30
        }
31

UNCOV
32
        if (keys.length === 0) {
×
33
            return '';
×
34
        }
35

UNCOV
36
        this._isSpecialData = ExportUtilities.isSpecialData(this._data[0]);
×
UNCOV
37
        this._escapeCharacters.push(this._delimiter);
×
38

UNCOV
39
        this._headerRecord = this.processHeaderRecord(keys, this._data.length);
×
UNCOV
40
        this._dataRecords = this.processDataRecords(this._data, keys);
×
41

UNCOV
42
        return this._headerRecord + this._dataRecords;
×
43
    }
44

45
    public prepareDataAsync(done: (result: string) => void) {
UNCOV
46
        const columns = this.columns?.filter(c => !c.skip)
×
UNCOV
47
                        .sort((a, b) => a.startIndex - b.startIndex)
×
UNCOV
48
                        .sort((a, b) => a.pinnedIndex - b.pinnedIndex);
×
UNCOV
49
        const keys = columns && columns.length ? columns.map(c => c.field) : ExportUtilities.getKeysFromData(this._data);
×
50

UNCOV
51
        this._isSpecialData = ExportUtilities.isSpecialData(this._data[0]);
×
UNCOV
52
        this._escapeCharacters.push(this._delimiter);
×
53

UNCOV
54
        const headers = columns && columns.length ?
×
UNCOV
55
                        columns.map(c => c.header ?? c.field) :
×
56
                        keys;
57

UNCOV
58
        this._headerRecord = this.processHeaderRecord(headers, this._data.length);
×
UNCOV
59
        if (keys.length === 0 || ((!this._data || this._data.length === 0) && keys.length === 0)) {
×
UNCOV
60
            done('');
×
61
        } else {
UNCOV
62
            this.processDataRecordsAsync(this._data, keys, (dr) => {
×
UNCOV
63
                done(this._headerRecord + dr);
×
64
            });
65
        }
66
    }
67

68
    private processField(value, escapeChars): string {
UNCOV
69
        let safeValue = ExportUtilities.hasValue(value) ? String(value) : '';
×
UNCOV
70
        if (escapeChars.some((v) => safeValue.includes(v))) {
×
UNCOV
71
            safeValue = `"${safeValue}"`;
×
72
        }
UNCOV
73
        return safeValue + this._delimiter;
×
74
    }
75

76
    private processHeaderRecord(keys, dataLength): string {
UNCOV
77
        let recordData = '';
×
UNCOV
78
        for (const keyName of keys) {
×
UNCOV
79
            recordData += this.processField(keyName, this._escapeCharacters);
×
80
        }
81

UNCOV
82
        const result = recordData.slice(0, -this._delimiterLength);
×
83

UNCOV
84
        return dataLength > 0 ? result + this._eor : result;
×
85
    }
86

87
    private processRecord(record, keys): string {
UNCOV
88
        const recordData = new Array(keys.length);
×
UNCOV
89
        for (let index = 0; index < keys.length; index++) {
×
UNCOV
90
            const value = (record[keys[index]] !== undefined) ? record[keys[index]] : this._isSpecialData ? record : '';
×
UNCOV
91
            recordData[index] = this.processField(value, this._escapeCharacters);
×
92
        }
93

UNCOV
94
        return recordData.join('').slice(0, -this._delimiterLength) + this._eor;
×
95
    }
96

97
    private processDataRecords(currentData, keys) {
UNCOV
98
        const dataRecords = new Array(currentData.length);
×
99

UNCOV
100
        for (let i = 0; i < currentData.length; i++) {
×
UNCOV
101
            const row = currentData[i];
×
UNCOV
102
            dataRecords[i] = this.processRecord(row, keys);
×
103
        }
104

UNCOV
105
        return dataRecords.join('');
×
106
    }
107

108
    private processDataRecordsAsync(currentData, keys, done: (result: string) => void) {
UNCOV
109
        const dataRecords = new Array(currentData.length);
×
110

UNCOV
111
        yieldingLoop(currentData.length, 1000,
×
112
            (i) => {
UNCOV
113
                const row = currentData[i];
×
UNCOV
114
                dataRecords[i] = this.processRecord(row, keys);
×
115
            },
116
            () => {
UNCOV
117
                done(dataRecords.join(''));
×
118
            });
119
    }
120

121
    private setDelimiter(value) {
UNCOV
122
        this._delimiter = value;
×
UNCOV
123
        this._delimiterLength = value.length;
×
124
    }
125
}
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