From edab0de6628b408c7f7d44190132d94af2cdab59 Mon Sep 17 00:00:00 2001 From: Daniel Lautzenheiser Date: Thu, 25 May 2023 12:03:09 -0400 Subject: [PATCH] fix: handling boolean and number values in filters (#825) --- .../lib/util/__tests__/filter.util.spec.ts | 34 +++++++++++++++---- packages/core/src/lib/util/filter.util.ts | 24 +++++++++---- 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/packages/core/src/lib/util/__tests__/filter.util.spec.ts b/packages/core/src/lib/util/__tests__/filter.util.spec.ts index 92895e2c..516ff0e1 100644 --- a/packages/core/src/lib/util/__tests__/filter.util.spec.ts +++ b/packages/core/src/lib/util/__tests__/filter.util.spec.ts @@ -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', () => { diff --git a/packages/core/src/lib/util/filter.util.ts b/packages/core/src/lib/util/filter.util.ts index 17babf1d..8b981346 100644 --- a/packages/core/src/lib/util/filter.util.ts +++ b/packages/core/src/lib/util/filter.util.ts @@ -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) {