Skip to content

Commit 9e3acdc

Browse files
authored
fix(filters): disregard time when filtering date only format (#593)
* fix(filters): disregard time when filtering date only format - if we pass a Date object that has time and we use a dateIso format, or any other without time, we should disregard the time portion when using the CompoundDate Filter
1 parent 43e483e commit 9e3acdc

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

src/app/modules/angular-slickgrid/filter-conditions/__tests__/dateIsoFilterCondition.spec.ts

+13
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ describe('dateIsoFilterCondition method', () => {
3232
expect(output).toBe(true);
3333
});
3434

35+
it('should return True when input value provided is equal to the searchTerms, even when passing Date+Time in cell value, and is called by "executeMappedCondition"', () => {
36+
const options = { dataKey: '', operator: 'EQ', fieldType: FieldType.dateIso, cellValue: new Date('1993-12-25T14:02:02.103Z'), searchTerms: ['1993-12-25'] } as FilterConditionOption;
37+
const output = executeMappedCondition(options);
38+
expect(output).toBe(true);
39+
});
40+
41+
it('should return True when input value provided is equal to the searchTerms, even when passing Date+Time in search value, and is called by "executeMappedCondition"', () => {
42+
// @ts-ignore
43+
const options = { dataKey: '', operator: 'EQ', fieldType: FieldType.dateIso, cellValue: '1993-12-25', searchTerms: [new Date('1993-12-25T14:02:02.103Z')] } as FilterConditionOption;
44+
const output = executeMappedCondition(options);
45+
expect(output).toBe(true);
46+
});
47+
3548
it('should return False when cell value is not the same value as the searchTerm', () => {
3649
const options = { dataKey: '', operator: 'EQ', fieldType: FieldType.dateIso, cellValue: '1993-12-25', searchTerms: ['2003-03-14'] } as FilterConditionOption;
3750
const output = executeMappedCondition(options);

src/app/modules/angular-slickgrid/filter-conditions/executeMappedCondition.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function executeAssociatedDateCondition(options: FilterConditionOption): boolean
8585
// cell value in moment format
8686
const dateCell = moment(options.cellValue, FORMAT, true);
8787

88-
if (searchTerms.length === 2 || ((searchTerms[0] as string).indexOf('..') > 0)) {
88+
if (searchTerms.length === 2 || (typeof searchTerms[0] === 'string' && (searchTerms[0] as string).indexOf('..') > 0)) {
8989
isRangeSearch = true;
9090
const searchValues = (searchTerms.length === 2) ? searchTerms : (searchTerms[0] as string).split('..');
9191
const searchValue1 = (Array.isArray(searchValues) && searchValues[0] || '') as Date | string;
@@ -107,12 +107,18 @@ function executeAssociatedDateCondition(options: FilterConditionOption): boolean
107107
dateSearch1 = moment(searchTerms[0] as Date | string, FORMAT, true);
108108
}
109109

110+
// when comparing with Dates only (without time), we need to disregard the time portion, we can do so by setting our time to start at midnight
111+
// ref, see https://stackoverflow.com/a/19699447/1212166
112+
const dateCellTimestamp = FORMAT.toLowerCase().includes('h') ? parseInt(dateCell.format('X'), 10) : parseInt(dateCell.clone().startOf('day').format('X'), 10);
113+
110114
// run the filter condition with date in Unix Timestamp format
111115
if (isRangeSearch) {
112116
const isInclusive = options.operator && options.operator === OperatorType.rangeInclusive;
113-
const resultCondition1 = testFilterCondition((isInclusive ? '>=' : '>'), parseInt(dateCell.format('X'), 10), parseInt(dateSearch1.format('X'), 10));
114-
const resultCondition2 = testFilterCondition((isInclusive ? '<=' : '<'), parseInt(dateCell.format('X'), 10), parseInt(dateSearch2.format('X'), 10));
117+
const resultCondition1 = testFilterCondition((isInclusive ? '>=' : '>'), dateCellTimestamp, parseInt(dateSearch1.format('X'), 10));
118+
const resultCondition2 = testFilterCondition((isInclusive ? '<=' : '<'), dateCellTimestamp, parseInt(dateSearch2.format('X'), 10));
115119
return (resultCondition1 && resultCondition2);
116120
}
117-
return testFilterCondition(options.operator || '==', parseInt(dateCell.format('X'), 10), parseInt(dateSearch1.format('X'), 10));
121+
122+
const dateSearchTimestamp1 = FORMAT.toLowerCase().includes('h') ? parseInt(dateSearch1.format('X'), 10) : parseInt(dateSearch1.clone().startOf('day').format('X'), 10);
123+
return testFilterCondition(options.operator || '==', dateCellTimestamp, dateSearchTimestamp1);
118124
};

test/tsconfig.spec.json

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
2-
"extends": "./tsconfig.json",
32
"compilerOptions": {
43
"outDir": "../out-tsc/spec",
54
"baseUrl": "./",

0 commit comments

Comments
 (0)