Skip to content

Commit

Permalink
fix: Ensure that auto-filtering only matches primitive values (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
gethinwebster authored Jan 2, 2025
1 parent 7af5676 commit ac5f136
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
35 changes: 35 additions & 0 deletions src/__tests__/operations/filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,39 @@ describe('with filteringFunction', () => {
expect(filteringSpy).toHaveBeenCalledTimes(3);
expect(filteringSpy).toHaveBeenCalledWith(expect.any(Object), '', undefined);
});

test('does not match object values', () => {
const items = [
{ id: 1, field: {} },
{ id: 2, field: 'object' },
{ id: 3, field: [{}, {}] },
{ id: 4, field: 'Some text containing [object Object]' },
];
const { items: processed } = processItems(
items,
{ filteringText: 'object' },
{
filtering: {},
}
);
expect(processed).toHaveLength(2);
expect(processed[0]?.id).toEqual(2);
expect(processed[1]?.id).toEqual(4);
});

test('does not match against date objects', () => {
const items = [
{ id: 1, field: new Date('2024-12-01') },
{ id: 2, field: '2024-12-01' },
];
const { items: processed } = processItems(
items,
{ filteringText: '2024' },
{
filtering: {},
}
);
expect(processed).toHaveLength(1);
expect(processed[0]?.id).toEqual(2);
});
});
13 changes: 7 additions & 6 deletions src/operations/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ function defaultFilteringFunction<T>(item: T, filteringText: string, filteringFi
filteringFields = filteringFields || Object.keys(item as Record<string, any>);
const lowFilteringText = filteringText.toLowerCase();

return filteringFields.some(
key =>
String((item as Record<string, any>)[key])
.toLowerCase()
.indexOf(lowFilteringText) > -1
);
return filteringFields.some(key => {
const value = (item as Record<string, any>)[key];
if (value && typeof value === 'object') {
return false;
}
return String(value).toLowerCase().indexOf(lowFilteringText) > -1;
});
}

export function createFilterPredicate<T>(
Expand Down

0 comments on commit ac5f136

Please sign in to comment.