Skip to content

Commit

Permalink
Merge pull request #16 from webzard-io/fix/data-provider-fileter
Browse files Browse the repository at this point in the history
fix: Fix filter result error when field value is non-array
  • Loading branch information
self-transition authored Oct 13, 2023
2 parents 9d7efc8 + ad487a5 commit a922912
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
11 changes: 10 additions & 1 deletion __tests__/utils/filter-data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ describe('evaluateFilter function', () => {
total: 10,
labels: ['label-1', 'label-2'],
description: null,
type: 'type-1'
}
} as Unstructured;

Expand Down Expand Up @@ -143,11 +144,19 @@ describe('evaluateFilter function', () => {
test('handles "in" operator', () => {
expect(evaluateFilter(mockItem, 'spec.labels', 'in', ['label-1', 'label-2'])).toBeTruthy();
expect(evaluateFilter(mockItem, 'spec.total', 'in', ['label-1', 'label-2'])).toBeFalsy();
expect(evaluateFilter(mockItem, 'spec.total', 'in', [10, 20])).toBeTruthy();
expect(evaluateFilter(mockItem, 'spec.type', 'in', ['type-1', 'type-2'])).toBeTruthy();
expect(evaluateFilter(mockItem, 'spec.type', 'in', ['type-3', 'type-4'])).toBeFalsy();
expect(evaluateFilter(mockItem, 'spec.type', 'in', 'type-1')).toBeFalsy();
});

test('handles "nin" operator', () => {
expect(evaluateFilter(mockItem, 'spec.labels', 'nin', ['label-3', 'label-4'])).toBeTruthy();
expect(evaluateFilter(mockItem, 'spec.total', 'nin', ['label-3', 'label-4'])).toBeFalsy();
expect(evaluateFilter(mockItem, 'spec.total', 'nin', ['label-3', 'label-4'])).toBeTruthy();
expect(evaluateFilter(mockItem, 'spec.total', 'nin', [10, 20])).toBeFalsy();
expect(evaluateFilter(mockItem, 'spec.type', 'nin', ['type-1', 'type-2'])).toBeFalsy();
expect(evaluateFilter(mockItem, 'spec.type', 'nin', ['type-3', 'type-4'])).toBeTruthy();
expect(evaluateFilter(mockItem, 'spec.type', 'in', 'type-1')).toBeFalsy();
});

test('handles "contains" operator', () => {
Expand Down
18 changes: 14 additions & 4 deletions src/utils/filter-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,26 @@ export function evaluateFilter(
case 'gte':
return fieldValue >= value;
case 'in': {
if (!Array.isArray(fieldValue) || !Array.isArray(value)) {
if (!Array.isArray(value)) {
return false;
}
return value.some(item => _.includes(fieldValue, item));
return value.some(item => {
if (Array.isArray(fieldValue)) {
return _.includes(fieldValue, item);
}
return item === fieldValue
});
}
case 'nin': {
if (!Array.isArray(fieldValue) || !Array.isArray(value)) {
if (!Array.isArray(value)) {
return false;
}
return value.every(item => !_.includes(fieldValue, item));
return value.every(item => {
if (Array.isArray(fieldValue)) {
return !_.includes(fieldValue, item);
}
return item !== fieldValue
});
}
case 'contains':
return _.includes(fieldValue, value);
Expand Down

0 comments on commit a922912

Please sign in to comment.