• 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

13.79
/projects/igniteui-angular/src/lib/data-operations/sorting-strategy.ts
1
import type { KeyOfOrString } from '../core/types';
2
import { IBaseEventArgs } from '../core/utils';
3
import { GridType } from '../grids/common/grid.interface';
4

5
/* mustCoerceToInt */
6
export enum SortingDirection {
2✔
7
    None = 0,
2✔
8
    Asc = 1,
2✔
9
    Desc = 2
2✔
10
}
11

12
/* marshalByValue */
13
/* tsPlainInterface */
14
export interface ISortingExpression<T = any> extends IBaseEventArgs {
15
    fieldName: KeyOfOrString<T> & string;
16
    /* mustCoerceToInt */
17
    dir: SortingDirection;
18
    ignoreCase?: boolean;
19
    strategy?: ISortingStrategy;
20
}
21

22
export interface ISortingStrategy {
23
    /* blazorSuppress */
24
    sort: (
25
        data: any[],
26
        fieldName: string,
27
        dir: SortingDirection,
28
        ignoreCase: boolean,
29
        valueResolver: (obj: any, key: string, isDate?: boolean) => any,
30
        isDate?: boolean,
31
        isTime?: boolean,
32
        grid?: GridType
33
    ) => any[];
34
}
35

36
export class DefaultSortingStrategy implements ISortingStrategy {
37
    protected static _instance: DefaultSortingStrategy = null;
2✔
38

39
    protected constructor() { }
40

41
    public static instance(): DefaultSortingStrategy {
42
        return this._instance || (this._instance = new this());
259✔
43
    }
44

45
    /* blazorSuppress */
46
    public sort(
47
        data: any[],
48
        fieldName: string,
49
        dir: SortingDirection,
50
        ignoreCase: boolean,
51
        valueResolver: (obj: any, key: string, isDate?: boolean) => any,
52
        isDate?: boolean,
53
        isTime?: boolean
54
    ) {
UNCOV
55
        const key = fieldName;
×
UNCOV
56
        const reverse = (dir === SortingDirection.Desc ? -1 : 1);
×
UNCOV
57
        const cmpFunc = (obj1: any, obj2: any) => this.compareObjects(obj1, obj2, key, reverse, ignoreCase, valueResolver, isDate, isTime);
×
UNCOV
58
        return this.arraySort(data, cmpFunc);
×
59
    }
60

61
    public compareValues(a: any, b: any): number {
UNCOV
62
        const an = (a === null || a === undefined);
×
UNCOV
63
        const bn = (b === null || b === undefined);
×
UNCOV
64
        if (an) {
×
UNCOV
65
            if (bn) {
×
UNCOV
66
                return 0;
×
67
            }
UNCOV
68
            return -1;
×
UNCOV
69
        } else if (bn) {
×
UNCOV
70
            return 1;
×
71
        }
UNCOV
72
        return a > b ? 1 : a < b ? -1 : 0;
×
73
    }
74

75
    protected compareObjects(
76
        obj1: any,
77
        obj2: any,
78
        key: string,
79
        reverse: number,
80
        ignoreCase: boolean,
81
        valueResolver: (obj: any, key: string, isDate?: boolean, isTime?: boolean) => any,
82
        isDate: boolean,
83
        isTime: boolean
84
    ) {
UNCOV
85
        let a = valueResolver.call(this, obj1, key, isDate, isTime);
×
UNCOV
86
        let b = valueResolver.call(this, obj2, key, isDate, isTime);
×
UNCOV
87
        if (ignoreCase) {
×
UNCOV
88
            a = a && a.toLowerCase ? a.toLowerCase() : a;
×
UNCOV
89
            b = b && b.toLowerCase ? b.toLowerCase() : b;
×
90
        }
UNCOV
91
        return reverse * this.compareValues(a, b);
×
92
    }
93

94
    protected arraySort(data: any[], compareFn?: (arg0: any, arg1: any) => number): any[] {
UNCOV
95
        return data.sort(compareFn);
×
96
    }
97
}
98

99
export class GroupMemberCountSortingStrategy implements ISortingStrategy {
100
    protected static _instance: GroupMemberCountSortingStrategy = null;
2✔
101

102
    protected constructor() { }
103

104
    public static instance(): GroupMemberCountSortingStrategy {
105
        return this._instance || (this._instance = new this());
×
106
    }
107

108
    public sort(data: any[], fieldName: string, dir: SortingDirection) {
109
        const groupedArray = this.groupBy(data, fieldName);
×
110
        const reverse = (dir === SortingDirection.Desc ? -1 : 1);
×
111

112
        const cmpFunc = (a, b) => {
×
113
            return this.compareObjects(a, b, groupedArray, fieldName, reverse);
×
114
        };
115

116
        return data
×
117
            .sort((a, b) => a[fieldName].localeCompare(b[fieldName]))
×
118
            .sort(cmpFunc);
119
    }
120

121
    public groupBy(data, key) {
122
        return data.reduce((acc, curr) => {
×
123
            (acc[curr[key]] = acc[curr[key]] || []).push(curr);
×
124
            return acc;
×
125
        }, {})
126
    }
127

128
    protected compareObjects(obj1: any, obj2: any, data: any[], fieldName: string, reverse: number) {
129
        const firstItemValuesLength = data[obj1[fieldName]].length;
×
130
        const secondItemValuesLength = data[obj2[fieldName]].length;
×
131

132
        return reverse * (firstItemValuesLength - secondItemValuesLength);
×
133
    }
134
}
135

136
export class FormattedValuesSortingStrategy extends DefaultSortingStrategy {
137
    protected static override _instance: FormattedValuesSortingStrategy = null;
2✔
138

139
    constructor() {
UNCOV
140
        super();
×
141
    }
142

143
    public static override instance(): FormattedValuesSortingStrategy {
144
        return this._instance || (this._instance = new this());
×
145
    }
146

147
    public override sort(
148
        data: any[],
149
        fieldName: string,
150
        dir: SortingDirection,
151
        ignoreCase: boolean,
152
        valueResolver: (obj: any, key: string, isDate?: boolean) => any,
153
        isDate?: boolean,
154
        isTime?: boolean,
155
        grid?: GridType
156
    ) {
UNCOV
157
        const key = fieldName;
×
UNCOV
158
        const reverse = (dir === SortingDirection.Desc ? -1 : 1);
×
UNCOV
159
        const cmpFunc = (obj1: any, obj2: any) => this.compareObjects(obj1, obj2, key, reverse, ignoreCase, valueResolver, isDate, isTime, grid);
×
UNCOV
160
        return this.arraySort(data, cmpFunc);
×
161
    }
162

163
    protected override compareObjects(
164
        obj1: any,
165
        obj2: any,
166
        key: string,
167
        reverse: number,
168
        ignoreCase: boolean,
169
        valueResolver: (obj: any, key: string, isDate?: boolean, isTime?: boolean) => any,
170
        isDate: boolean,
171
        isTime: boolean,
172
        grid?: GridType
173
    ) {
UNCOV
174
        let a = valueResolver.call(this, obj1, key, isDate, isTime);
×
UNCOV
175
        let b = valueResolver.call(this, obj2, key, isDate, isTime);
×
176

UNCOV
177
        if (grid) {
×
UNCOV
178
            const col = grid.getColumnByName(key);
×
UNCOV
179
            if (col && col.formatter) {
×
UNCOV
180
                a = col.formatter(a);
×
UNCOV
181
                b = col.formatter(b);
×
182
            }
183
        }
184

UNCOV
185
        if (ignoreCase) {
×
UNCOV
186
            a = a && a.toLowerCase ? a.toLowerCase() : a;
×
UNCOV
187
            b = b && b.toLowerCase ? b.toLowerCase() : b;
×
188
        }
UNCOV
189
        return reverse * this.compareValues(a, b);
×
190
    }
191
}
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