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

IgniteUI / igniteui-angular / 6797384210

08 Nov 2023 11:09AM UTC coverage: 91.853% (-0.3%) from 92.123%
6797384210

push

github

web-flow
Merge pull request #13613 from IgniteUI/mkirova/fix-empty-pivot

fix(igxPivotGrid): Add check in case data is empty due to removing al…

12459 of 14514 branches covered (0.0%)

1 of 1 new or added line in 1 file covered. (100.0%)

2127 existing lines in 217 files now uncovered.

25413 of 27667 relevant lines covered (91.85%)

31122.38 hits per line

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

96.67
/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 {
9
    private _headerRecord = '';
83✔
10
    private _dataRecords = '';
83✔
11
    private _eor = '\r\n';
83✔
12
    private _delimiter;
13
    private _escapeCharacters = ['\r', '\n', '\r\n'];
83✔
14
    private _delimiterLength = 1;
83✔
15
    private _isSpecialData = false;
83✔
16

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

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

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

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

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

42
        return this._headerRecord + this._dataRecords;
8✔
43
    }
44

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

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

54
        const headers = columns && columns.length ?
74✔
55
                        columns.map(c => c.header ?? c.field) :
188!
56
                        keys;
57

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

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

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

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

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

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

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

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

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

105
        return dataRecords.join('');
8✔
106
    }
107

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

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

121
    private setDelimiter(value) {
122
        this._delimiter = value;
83✔
123
        this._delimiterLength = value.length;
83✔
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

© 2026 Coveralls, Inc