fix: handling boolean and number values in filters (#825)

This commit is contained in:
Daniel Lautzenheiser 2023-05-25 12:03:09 -04:00 committed by GitHub
parent d0dba6dbf2
commit edab0de662
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 12 deletions

View File

@ -9,6 +9,8 @@ describe('filterEntries', () => {
data: {
language: 'en',
tags: ['tag-1', 'tag-2', 'fish-catfish'],
draft: true,
numbers: [1, 5],
},
});
@ -17,6 +19,8 @@ describe('filterEntries', () => {
data: {
language: 'fr',
tags: ['tag-1', 'tag-4'],
draft: false,
numbers: [5, 6],
},
});
@ -25,6 +29,8 @@ describe('filterEntries', () => {
data: {
language: 'gr',
tags: ['tag-1', 'tag-4'],
draft: false,
numbers: [5, 6],
},
});
@ -33,6 +39,8 @@ describe('filterEntries', () => {
data: {
language: 'gr',
tags: ['tag-1', 'tag-3'],
draft: false,
numbers: [1],
},
});
@ -41,6 +49,8 @@ describe('filterEntries', () => {
data: {
language: 'gr',
tags: ['tag-3'],
draft: false,
numbers: [8],
},
});
@ -49,6 +59,8 @@ describe('filterEntries', () => {
data: {
language: 'gr',
tags: ['tag-1', 'tag-4', 'fish-pike'],
draft: true,
numbers: [4, 8],
},
});
@ -75,6 +87,11 @@ describe('filterEntries', () => {
mockRandomFileNameEntry,
mockTags1and4Entry,
]);
expect(filterEntries(entries, { field: 'draft', value: 'true' })).toEqual([
mockEnglishEntry,
mockTags1and4Entry,
]);
});
it('should filter fields if multiple filter values are provided (must match only one)', () => {
@ -101,13 +118,8 @@ describe('filterEntries', () => {
mockIndexEntry,
mockTags1and4Entry,
]);
});
it('should filter fields if field value is an array and multiple filter values are provided (must include only one)', () => {
expect(filterEntries(entries, { field: 'tags', value: ['tag-3', 'tag-4'] })).toEqual([
mockFrenchEntry,
mockIndexEntry,
mockUnderscoreIndexEntry,
expect(filterEntries(entries, { field: 'numbers', value: '8' })).toEqual([
mockRandomFileNameEntry,
mockTags1and4Entry,
]);
@ -121,12 +133,22 @@ describe('filterEntries', () => {
mockRandomFileNameEntry,
mockTags1and4Entry,
]);
expect(filterEntries(entries, { field: 'numbers', value: ['5', '6'] })).toEqual([
mockEnglishEntry,
mockFrenchEntry,
mockIndexEntry,
]);
});
it('should match all values if matchAll is one (value is an array, multiple filter values are provided)', () => {
expect(
filterEntries(entries, { field: 'tags', value: ['tag-1', 'tag-4'], matchAll: true }),
).toEqual([mockFrenchEntry, mockIndexEntry, mockTags1and4Entry]);
expect(
filterEntries(entries, { field: 'numbers', value: ['5', '6'], matchAll: true }),
).toEqual([mockFrenchEntry, mockIndexEntry]);
});
it('should filter fields based on pattern when value is an array', () => {

View File

@ -15,24 +15,36 @@ function entryMatchesFieldRule(entry: Entry, filterRule: FieldFilterRule): boole
if (Array.isArray(fieldValue)) {
if (Array.isArray(filterRule.value)) {
if (filterRule.matchAll) {
return Boolean(filterRule.value.every(ruleValue => fieldValue.includes(ruleValue)));
return Boolean(
filterRule.value.every(ruleValue =>
fieldValue.find(v => String(v) === String(ruleValue)),
),
);
}
return Boolean(fieldValue.find(v => filterRule.value.includes(String(v))));
return Boolean(
fieldValue.find(v =>
Boolean(
(filterRule.value as string[]).find(
filterRuleValue => String(filterRuleValue) === String(v),
),
),
),
);
}
return fieldValue.includes(filterRule.value);
return Boolean(fieldValue.find(v => String(v) === String(filterRule.value)));
}
if (Array.isArray(filterRule.value)) {
if (filterRule.matchAll) {
return Boolean(filterRule.value.every(ruleValue => fieldValue === ruleValue));
return Boolean(filterRule.value.every(ruleValue => String(fieldValue) === String(ruleValue)));
}
return filterRule.value.includes(String(fieldValue));
return Boolean(filterRule.value.find(v => String(v) === String(fieldValue)));
}
return fieldValue === filterRule.value;
return String(fieldValue) === String(filterRule.value);
}
function entryMatchesRule(entry: Entry, filterRule: FilterRule) {