Skip to content

Commit

Permalink
fix(filters): disregard time when filtering date only format (#134)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ghiscoding authored Oct 1, 2020
1 parent 66effcf commit 7bd2d19
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export class Example12 {
{
id: 'start', name: 'Start', field: 'start', sortable: true,
formatter: Formatters.dateIso, columnGroup: 'Period',
type: FieldType.date, outputType: FieldType.dateIso,
type: FieldType.dateIso, outputType: FieldType.dateIso,
filterable: true, filter: { model: Filters.compoundDate },
editor: { model: Editors.date, massUpdate: true, params: { hideClearButton: false } },
},
Expand All @@ -166,7 +166,7 @@ export class Example12 {
{
id: 'finish', name: 'Finish', field: 'finish', sortable: true,
formatter: Formatters.dateIso, columnGroup: 'Period',
type: FieldType.date, outputType: FieldType.dateIso,
type: FieldType.dateIso, outputType: FieldType.dateIso,
filterable: true, filter: { model: Filters.compoundDate },
editor: {
model: Editors.date,
Expand Down Expand Up @@ -378,15 +378,16 @@ export class Example12 {
const randomFinishYear = (new Date().getFullYear()) + Math.floor(Math.random() * 10); // use only years not lower than 3 years ago
const randomMonth = Math.floor(Math.random() * 11);
const randomDay = Math.floor((Math.random() * 29));
const randomFinish = new Date(randomFinishYear, (randomMonth + 1), randomDay);
const randomTime = Math.floor((Math.random() * 59));
const randomFinish = new Date(randomFinishYear, (randomMonth + 1), randomDay, randomTime, randomTime, randomTime);
const randomPercentComplete = Math.floor(Math.random() * 100) + 15; // make it over 15 for E2E testing purposes

tmpArray[i] = {
id: i,
title: 'Task ' + i,
duration: Math.floor(Math.random() * 100) + 10,
percentComplete: randomPercentComplete > 100 ? 100 : randomPercentComplete,
start: new Date(randomYear, randomMonth, randomDay),
start: new Date(randomYear, randomMonth, randomDay, randomDay, randomTime, randomTime, randomTime),
finish: (i % 3 === 0 && (randomFinish > new Date() && i > 3)) ? randomFinish : '', // make sure the random date is earlier than today and it's index is bigger than 3
cost: (i % 33 === 0) ? null : Math.round(Math.random() * 10000) / 100,
completed: (i % 3 === 0 && (randomFinish > new Date() && i > 3)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ describe('dateIsoFilterCondition method', () => {
expect(output).toBe(true);
});

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"', () => {
const options = { dataKey: '', operator: 'EQ', fieldType: FieldType.dateIso, cellValue: new Date('1993-12-25T14:02:02.103Z'), searchTerms: ['1993-12-25'] } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(true);
});

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"', () => {
const options = { dataKey: '', operator: 'EQ', fieldType: FieldType.dateIso, cellValue: '1993-12-25', searchTerms: [new Date('1993-12-25T14:02:02.103Z')] } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(true);
});

it('should return False when cell value is not the same value as the searchTerm', () => {
const options = { dataKey: '', operator: 'EQ', fieldType: FieldType.dateIso, cellValue: '1993-12-25', searchTerms: ['2003-03-14'] } as FilterConditionOption;
const output = executeMappedCondition(options);
Expand Down
14 changes: 10 additions & 4 deletions packages/common/src/filter-conditions/executeMappedCondition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function executeAssociatedDateCondition(options: FilterConditionOption): boolean
// cell value in moment format
const dateCell = moment(options.cellValue, FORMAT, true);

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

// 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
// ref, see https://stackoverflow.com/a/19699447/1212166
const dateCellTimestamp = FORMAT.toLowerCase().includes('h') ? parseInt(dateCell.format('X'), 10) : parseInt(dateCell.clone().startOf('day').format('X'), 10);

// run the filter condition with date in Unix Timestamp format
if (isRangeSearch) {
const isInclusive = options.operator && options.operator === OperatorType.rangeInclusive;
const resultCondition1 = testFilterCondition((isInclusive ? '>=' : '>'), parseInt(dateCell.format('X'), 10), parseInt(dateSearch1.format('X'), 10));
const resultCondition2 = testFilterCondition((isInclusive ? '<=' : '<'), parseInt(dateCell.format('X'), 10), parseInt(dateSearch2.format('X'), 10));
const resultCondition1 = testFilterCondition((isInclusive ? '>=' : '>'), dateCellTimestamp, parseInt(dateSearch1.format('X'), 10));
const resultCondition2 = testFilterCondition((isInclusive ? '<=' : '<'), dateCellTimestamp, parseInt(dateSearch2.format('X'), 10));
return (resultCondition1 && resultCondition2);
}
return testFilterCondition(options.operator || '==', parseInt(dateCell.format('X'), 10), parseInt(dateSearch1.format('X'), 10));

const dateSearchTimestamp1 = FORMAT.toLowerCase().includes('h') ? parseInt(dateSearch1.format('X'), 10) : parseInt(dateSearch1.clone().startOf('day').format('X'), 10);
return testFilterCondition(options.operator || '==', dateCellTimestamp, dateSearchTimestamp1);
}
Binary file not shown.

0 comments on commit 7bd2d19

Please sign in to comment.