Skip to content

Commit

Permalink
fix: Fix filter result error when field value is non-array
Browse files Browse the repository at this point in the history
  • Loading branch information
Junjia Huang committed Oct 13, 2023
1 parent 675e55f commit 4ac9271
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 6 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,15 @@ 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.type', 'in', ['type-1', 'type-2'])).toBeTruthy();
expect(evaluateFilter(mockItem, 'spec.type', 'in', ['type-3', 'type-4'])).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.type', 'nin', ['type-1', 'type-2'])).toBeFalsy();
expect(evaluateFilter(mockItem, 'spec.type', 'nin', ['type-3', 'type-4'])).toBeTruthy();
});

test('handles "contains" operator', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/filter-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ 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));
}
case 'nin': {
if (!Array.isArray(fieldValue) || !Array.isArray(value)) {
if (!Array.isArray(value)) {
return false;
}
return value.every(item => !_.includes(fieldValue, item));
Expand Down

0 comments on commit 4ac9271

Please sign in to comment.