• 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

3.85
/projects/igniteui-angular/src/lib/directives/filter/filter.directive.ts
1
import {
2
    Directive,
3
    EventEmitter,
4
    Input,
5
    OnChanges,
6
    Output,
7
    Pipe,
8
    PipeTransform,
9
    SimpleChanges
10
} from '@angular/core';
11

12
export class IgxFilterOptions {
13
    // Input text value that will be used as a filtering pattern (matching condition is based on it)
UNCOV
14
    public inputValue = '';
×
15

16
    // Item property, which value should be used for filtering
17
    public key: string | string[];
18

19
    // Represent items of the list. It should be used to handle declaratively defined widgets
20
    public items: any[];
21

22
    // Function - get value to be tested from the item
23
    // item - single item of the list to be filtered
24
    // key - property name of item, which value should be tested
25
    // Default behavior - returns "key"- named property value of item if key is provided,
26
    // otherwise textContent of the item's html element
27
    public get_value(item: any, key: string): string {
UNCOV
28
        let result = '';
×
29

UNCOV
30
        if (key && item[key]) {
×
UNCOV
31
            result = item[key].toString();
×
UNCOV
32
        } else if (item.element) {
×
UNCOV
33
            if (item.element.nativeElement) {
×
34
                result = item.element.nativeElement.textContent.trim();
×
35
            // Check if element doesn't return the DOM element directly
UNCOV
36
            } else if (item.element.textContent) {
×
UNCOV
37
                result = item.element.textContent.trim();
×
38
            }
39
        }
40

UNCOV
41
        return result;
×
42
    }
43

44
    // Function - formats the original text before matching process
45
    // Default behavior - returns text to lower case
46
    public formatter(valueToTest: string): string {
UNCOV
47
        return valueToTest.toLowerCase();
×
48
    }
49

50
    // Function - determines whether the item met the condition
51
    // valueToTest - text value that should be tested
52
    // inputValue - text value from input that condition is based on
53
    // Default behavior - "contains"
54
    public matchFn(valueToTest: string, inputValue: string): boolean {
UNCOV
55
        return valueToTest.indexOf(inputValue && inputValue.toLowerCase() || '') > -1;
×
56
    }
57

58
    // Function - executed after matching test for every matched item
59
    // Default behavior - shows the item
60
    public metConditionFn(item: any) {
UNCOV
61
        if (item.hasOwnProperty('hidden')) {
×
UNCOV
62
            item.hidden = false;
×
63
        }
64
    }
65

66
    // Function - executed for every NOT matched item after matching test
67
    // Default behavior - hides the item
68
    public overdueConditionFn(item: any) {
UNCOV
69
        if (item.hasOwnProperty('hidden')) {
×
UNCOV
70
            item.hidden = true;
×
71
        }
72
    }
73
}
74

75

76
@Directive({
77
    selector: '[igxFilter]',
78
    standalone: true
79
})
80
export class IgxFilterDirective implements OnChanges {
2✔
UNCOV
81
    @Output() public filtering = new EventEmitter(false); // synchronous event emitter
×
UNCOV
82
    @Output() public filtered = new EventEmitter();
×
83

84
    @Input('igxFilter') public filterOptions: IgxFilterOptions;
85

86
    constructor() {
87
    }
88

89
    public ngOnChanges(changes: SimpleChanges) {
90
        // Detect only changes of input value
UNCOV
91
        if (changes.filterOptions &&
×
92
            changes.filterOptions.currentValue &&
93
            changes.filterOptions.currentValue.inputValue !== undefined &&
94
            changes.filterOptions.previousValue &&
95
            changes.filterOptions.currentValue.inputValue !== changes.filterOptions.previousValue.inputValue) {
UNCOV
96
            this.filter();
×
97
        }
98
    }
99

100
    private filter() {
UNCOV
101
        if (!this.filterOptions.items) {
×
102
            return;
×
103
        }
104

UNCOV
105
        const args = { cancel: false, items: this.filterOptions.items };
×
UNCOV
106
        this.filtering.emit(args);
×
107

UNCOV
108
        if (args.cancel) {
×
UNCOV
109
            return;
×
110
        }
111

UNCOV
112
        const pipe = new IgxFilterPipe();
×
113

UNCOV
114
        const filtered = pipe.transform(this.filterOptions.items, this.filterOptions);
×
UNCOV
115
        this.filtered.emit({ filteredItems: filtered });
×
116
    }
117
}
118

119
@Pipe({
120
    name: 'igxFilter',
121
    pure: false,
122
    standalone: true
123
})
124
export class IgxFilterPipe implements PipeTransform {
2✔
125
    private findMatchByKey(item: any, options: IgxFilterOptions, key: string) {
UNCOV
126
        const match = options.matchFn(options.formatter(options.get_value(item, key)), options.inputValue);
×
127

UNCOV
128
        if (match) {
×
UNCOV
129
            if (options.metConditionFn) {
×
UNCOV
130
                options.metConditionFn(item);
×
131
            }
132
        } else {
UNCOV
133
            if (options.overdueConditionFn) {
×
UNCOV
134
                options.overdueConditionFn(item);
×
135
            }
136
        }
137

UNCOV
138
        return match;
×
139
    }
140

141
    public transform(items: any[],
142
                     // options - initial settings of filter functionality
143
                     options: IgxFilterOptions) {
144

UNCOV
145
        let result = [];
×
146

UNCOV
147
        if (!items || !items.length || !options) {
×
148
            return;
×
149
        }
150

UNCOV
151
        if (options.items) {
×
UNCOV
152
            items = options.items;
×
153
        }
154

UNCOV
155
        result = items.filter((item: any) => {
×
UNCOV
156
            if (!Array.isArray(options.key)) {
×
UNCOV
157
                return this.findMatchByKey(item, options, options.key);
×
158
            } else {
159
                let isMatch = false;
×
160
                options.key.forEach(key => {
×
161
                    if (this.findMatchByKey(item, options, key)) {
×
162
                        isMatch = true;
×
163
                    }
164
                });
165
                return isMatch;
×
166
            }
167
        });
168

UNCOV
169
        return result;
×
170
    }
171
}
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