Skip to content

Commit

Permalink
feat: Add "Does not start with" property filter operator (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
jperals authored Mar 4, 2024
1 parent 84ab362 commit 82e3d54
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/__tests__/operations/property-filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ const propertyFiltering = {
filteringProperties: [
{
key: 'id',
operators: [':', '!:', '^'],
operators: [':', '!:', '^', '!^'],
groupValuesLabel: 'Id values',
propertyLabel: 'Id',
},
{
key: 'field',
operators: [':', '!:', '^', '?'],
operators: [':', '!:', '^', '!^', '?'],
groupValuesLabel: 'Field values',
propertyLabel: 'Field',
},
Expand Down Expand Up @@ -154,7 +154,7 @@ describe('Supported operators', () => {
const { items: processed } = processItems(items, { propertyFilteringQuery: operatorQuery }, { propertyFiltering });
expect(processed).toEqual([items[3]]);
});
test.each<PropertyFilterOperator>(['<', '<=', '>', '>=', ':', '!:', '!=', '^'])(
test.each<PropertyFilterOperator>(['<', '<=', '>', '>=', ':', '!:', '!=', '^', '!^'])(
'%s operator is not supported by default',
operator => {
const operatorQuery = {
Expand Down Expand Up @@ -295,6 +295,28 @@ test('"starts-with" operator matching', () => {
expect(processed).toEqual([items[0], items[1], items[2]]);
});

test('"not-starts-with" operator matching', () => {
const items = [
{ id: 1, field: 'a' },
{ id: 2, field: 'ab' },
{ id: 3, field: 'bc' },
{ id: 4, field: 'bcd' },
{ id: 5, field: 'b' },
{ id: 6, field: 'c' },
{ id: 7, field: 'd' },
{ id: 8, field: '' },
{ id: 9, field: 'abc' },
{ id: 10, field: 'abcd' },
{ id: 11, field: 'abCd' },
];
const startsWithQuery = {
tokens: [{ propertyKey: 'field', operator: '!^', value: 'abc' }],
operation: 'and',
} as const;
const { items: processed } = processItems(items, { propertyFilteringQuery: startsWithQuery }, { propertyFiltering });
expect(processed).toEqual([items[0], items[1], items[2], items[3], items[4], items[5], items[6], items[7]]);
});

test('unsupported operator results in an exception', () => {
const items = [{ id: 1, field: 'match me' }];
const unsupportedOperatorQuery = {
Expand Down
2 changes: 2 additions & 0 deletions src/operations/property-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ const filterUsingOperator = (
return (itemValue + '').toLowerCase().indexOf((tokenValue + '').toLowerCase()) === -1;
case '^':
return (itemValue + '').toLowerCase().startsWith((tokenValue + '').toLowerCase());
case '!^':
return !(itemValue + '').toLowerCase().startsWith((tokenValue + '').toLowerCase());
// The unsupported operators result in an exception being thrown.
// The exception can be avoided if using the match function.
default:
Expand Down

0 comments on commit 82e3d54

Please sign in to comment.