Skip to content

Commit 0909494

Browse files
committed
chore(*): refactor filtering conditions
1 parent d860375 commit 0909494

File tree

1 file changed

+90
-97
lines changed

1 file changed

+90
-97
lines changed

projects/igniteui-angular/src/lib/data-operations/filtering-condition.ts

+90-97
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,87 @@ export class IgxBooleanFilteringOperand extends IgxFilteringOperand {
102102
}
103103
}
104104

105+
/**
106+
* @internal
107+
* @hidden
108+
*/
109+
class IgxBaseDateTimeFilteringOperand extends IgxFilteringOperand {
110+
protected constructor() {
111+
super();
112+
this.operations = [{
113+
name: 'empty',
114+
isUnary: true,
115+
iconName: 'is-empty',
116+
logic: (target: Date) => target === null || target === undefined
117+
}, {
118+
name: 'notEmpty',
119+
isUnary: true,
120+
iconName: 'not-empty',
121+
logic: (target: Date) => target !== null && target !== undefined
122+
}].concat(this.operations);
123+
}
124+
125+
/**
126+
* Splits a Date object into parts
127+
*
128+
* @memberof IgxDateFilteringOperand
129+
*/
130+
public static getDateParts(date: Date, dateFormat?: string): IDateParts {
131+
const res = {
132+
day: null,
133+
hours: null,
134+
milliseconds: null,
135+
minutes: null,
136+
month: null,
137+
seconds: null,
138+
year: null
139+
};
140+
if (!date || !dateFormat) {
141+
return res;
142+
}
143+
if (dateFormat.indexOf('y') >= 0) {
144+
res.year = date.getFullYear();
145+
}
146+
if (dateFormat.indexOf('M') >= 0) {
147+
res.month = date.getMonth();
148+
}
149+
if (dateFormat.indexOf('d') >= 0) {
150+
res.day = date.getDate();
151+
}
152+
if (dateFormat.indexOf('h') >= 0) {
153+
res.hours = date.getHours();
154+
}
155+
if (dateFormat.indexOf('m') >= 0) {
156+
res.minutes = date.getMinutes();
157+
}
158+
if (dateFormat.indexOf('s') >= 0) {
159+
res.seconds = date.getSeconds();
160+
}
161+
if (dateFormat.indexOf('f') >= 0) {
162+
res.milliseconds = date.getMilliseconds();
163+
}
164+
return res;
165+
}
166+
167+
protected findValueInSet(target: any, searchVal: Set<any>) {
168+
if (!target) {
169+
return false;
170+
}
171+
return searchVal.has(target.toISOString());
172+
}
173+
174+
protected validateInputData(target: Date) {
175+
if (!(target instanceof Date)) {
176+
throw new Error('Could not perform filtering on \'date\' column because the datasource object type is not \'Date\'.');
177+
}
178+
}
179+
}
105180
/**
106181
* Provides filtering operations for Dates
107182
*
108183
* @export
109184
*/
110-
export class IgxDateFilteringOperand extends IgxFilteringOperand {
185+
export class IgxDateFilteringOperand extends IgxBaseDateTimeFilteringOperand {
111186
protected constructor() {
112187
super();
113188
this.operations = [{
@@ -310,73 +385,8 @@ export class IgxDateFilteringOperand extends IgxFilteringOperand {
310385
const now = IgxDateFilteringOperand.getDateParts(new Date(), 'y');
311386
return d.year === now.year + 1;
312387
}
313-
}, {
314-
name: 'empty',
315-
isUnary: true,
316-
iconName: 'is-empty',
317-
logic: (target: Date) => target === null || target === undefined
318-
}, {
319-
name: 'notEmpty',
320-
isUnary: true,
321-
iconName: 'not-empty',
322-
logic: (target: Date) => target !== null && target !== undefined
323388
}].concat(this.operations);
324389
}
325-
326-
/**
327-
* Splits a Date object into parts
328-
*
329-
* @memberof IgxDateFilteringOperand
330-
*/
331-
public static getDateParts(date: Date, dateFormat?: string): IDateParts {
332-
const res = {
333-
day: null,
334-
hours: null,
335-
milliseconds: null,
336-
minutes: null,
337-
month: null,
338-
seconds: null,
339-
year: null
340-
};
341-
if (!date || !dateFormat) {
342-
return res;
343-
}
344-
if (dateFormat.indexOf('y') >= 0) {
345-
res.year = date.getFullYear();
346-
}
347-
if (dateFormat.indexOf('M') >= 0) {
348-
res.month = date.getMonth();
349-
}
350-
if (dateFormat.indexOf('d') >= 0) {
351-
res.day = date.getDate();
352-
}
353-
if (dateFormat.indexOf('h') >= 0) {
354-
res.hours = date.getHours();
355-
}
356-
if (dateFormat.indexOf('m') >= 0) {
357-
res.minutes = date.getMinutes();
358-
}
359-
if (dateFormat.indexOf('s') >= 0) {
360-
res.seconds = date.getSeconds();
361-
}
362-
if (dateFormat.indexOf('f') >= 0) {
363-
res.milliseconds = date.getMilliseconds();
364-
}
365-
return res;
366-
}
367-
368-
protected findValueInSet(target: any, searchVal: Set<any>) {
369-
if (!target) {
370-
return false;
371-
}
372-
return searchVal.has(target.toISOString());
373-
}
374-
375-
protected validateInputData(target: Date) {
376-
if (!(target instanceof Date)) {
377-
throw new Error('Could not perform filtering on \'date\' column because the datasource object type is not \'Date\'.');
378-
}
379-
}
380390
}
381391

382392
export class IgxDateTimeFilteringOperand extends IgxDateFilteringOperand {
@@ -426,7 +436,7 @@ export class IgxDateTimeFilteringOperand extends IgxDateFilteringOperand {
426436
}
427437
}
428438

429-
export class IgxTimeFilteringOperand extends IgxFilteringOperand {
439+
export class IgxTimeFilteringOperand extends IgxBaseDateTimeFilteringOperand {
430440
protected constructor() {
431441
super();
432442
this.operations = [{
@@ -438,8 +448,8 @@ export class IgxTimeFilteringOperand extends IgxFilteringOperand {
438448
return false;
439449
}
440450
this.validateInputData(target);
441-
const targetp = IgxDateFilteringOperand.getDateParts(target, 'hms');
442-
const searchp = IgxDateFilteringOperand.getDateParts(searchVal, 'hms');
451+
const targetp = IgxTimeFilteringOperand.getDateParts(target, 'hms');
452+
const searchp = IgxTimeFilteringOperand.getDateParts(searchVal, 'hms');
443453
return targetp.hours === searchp.hours &&
444454
targetp.minutes === searchp.minutes &&
445455
targetp.seconds === searchp.seconds;
@@ -453,8 +463,8 @@ export class IgxTimeFilteringOperand extends IgxFilteringOperand {
453463
return true;
454464
}
455465
this.validateInputData(target);
456-
const targetp = IgxDateFilteringOperand.getDateParts(target, 'hms');
457-
const searchp = IgxDateFilteringOperand.getDateParts(searchVal, 'hms');
466+
const targetp = IgxTimeFilteringOperand.getDateParts(target, 'hms');
467+
const searchp = IgxTimeFilteringOperand.getDateParts(searchVal, 'hms');
458468
return targetp.hours !== searchp.hours ||
459469
targetp.minutes !== searchp.minutes ||
460470
targetp.seconds !== searchp.seconds;
@@ -469,8 +479,8 @@ export class IgxTimeFilteringOperand extends IgxFilteringOperand {
469479
}
470480

471481
this.validateInputData(target);
472-
const targetn = IgxDateFilteringOperand.getDateParts(target, 'hms');
473-
const search = IgxDateFilteringOperand.getDateParts(searchVal, 'hms');
482+
const targetn = IgxTimeFilteringOperand.getDateParts(target, 'hms');
483+
const search = IgxTimeFilteringOperand.getDateParts(searchVal, 'hms');
474484

475485
return targetn.hours < search.hours ? true : targetn.hours === search.hours && targetn.minutes < search.minutes ?
476486
true : targetn.hours === search.hours && targetn.minutes === search.minutes && targetn.seconds < search.seconds;
@@ -485,8 +495,8 @@ export class IgxTimeFilteringOperand extends IgxFilteringOperand {
485495
}
486496

487497
this.validateInputData(target);
488-
const targetn = IgxDateFilteringOperand.getDateParts(target, 'hms');
489-
const search = IgxDateFilteringOperand.getDateParts(searchVal, 'hms');
498+
const targetn = IgxTimeFilteringOperand.getDateParts(target, 'hms');
499+
const search = IgxTimeFilteringOperand.getDateParts(searchVal, 'hms');
490500

491501
return targetn.hours > search.hours ? true : targetn.hours === search.hours && targetn.minutes > search.minutes ?
492502
true : targetn.hours === search.hours && targetn.minutes === search.minutes && targetn.seconds > search.seconds;
@@ -501,8 +511,8 @@ export class IgxTimeFilteringOperand extends IgxFilteringOperand {
501511
}
502512

503513
this.validateInputData(target);
504-
const targetn = IgxDateFilteringOperand.getDateParts(target, 'hms');
505-
const search = IgxDateFilteringOperand.getDateParts(searchVal, 'hms');
514+
const targetn = IgxTimeFilteringOperand.getDateParts(target, 'hms');
515+
const search = IgxTimeFilteringOperand.getDateParts(searchVal, 'hms');
506516
return (targetn.hours === search.hours && targetn.minutes === search.minutes && targetn.seconds === search.seconds) ||
507517
targetn.hours < search.hours ? true : targetn.hours === search.hours && targetn.minutes < search.minutes ?
508518
true : targetn.hours === search.hours && targetn.minutes === search.minutes && targetn.seconds < search.seconds;
@@ -517,37 +527,20 @@ export class IgxTimeFilteringOperand extends IgxFilteringOperand {
517527
}
518528

519529
this.validateInputData(target);
520-
const targetn = IgxDateFilteringOperand.getDateParts(target, 'hms');
521-
const search = IgxDateFilteringOperand.getDateParts(searchVal, 'hms');
530+
const targetn = IgxTimeFilteringOperand.getDateParts(target, 'hms');
531+
const search = IgxTimeFilteringOperand.getDateParts(searchVal, 'hms');
522532
return (targetn.hours === search.hours && targetn.minutes === search.minutes && targetn.seconds === search.seconds) ||
523533
targetn.hours > search.hours ? true : targetn.hours === search.hours && targetn.minutes > search.minutes ?
524534
true : targetn.hours === search.hours && targetn.minutes === search.minutes && targetn.seconds > search.seconds;
525535
}
526-
}, {
527-
name: 'empty',
528-
isUnary: true,
529-
iconName: 'is-empty',
530-
logic: (target: Date) => target === null || target === undefined
531-
}, {
532-
name: 'notEmpty',
533-
isUnary: true,
534-
iconName: 'not-empty',
535-
logic: (target: Date) => target !== null && target !== undefined
536536
}].concat(this.operations);
537537
}
538538

539-
540539
protected findValueInSet(target: any, searchVal: Set<any>) {
541540
if (!target) {
542541
return false;
543542
}
544-
return searchVal.has(target);
545-
}
546-
547-
protected validateInputData(target: Date) {
548-
if (!(target instanceof Date)) {
549-
throw new Error('Could not perform filtering on \'date\' column because the datasource object type is not \'Date\'.');
550-
}
543+
return searchVal.has(target.toLocaleTimeString());
551544
}
552545
}
553546

0 commit comments

Comments
 (0)