Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix filter result error when field value is non-array #16

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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