diff --git a/packages/table-core/src/fns/filterFns.ts b/packages/table-core/src/fns/filterFns.ts index 44c4fb02d2..7e85698890 100644 --- a/packages/table-core/src/fns/filterFns.ts +++ b/packages/table-core/src/fns/filterFns.ts @@ -217,7 +217,10 @@ const filterFn_between = Object.assign( Number(filterValues[0]) > Number(filterValues[1])) || (['', undefined] as Array).includes(filterValues[1]) || filterFn_lessThan(row, columnId, filterValues[1])), - { autoRemove: (val: any) => !val }, + { + autoRemove: (val: any) => + testFalsy(val) || (testFalsy(val[0]) && testFalsy(val[1])), + }, ) /** @@ -238,7 +241,10 @@ const filterFn_betweenInclusive = Object.assign( Number(filterValues[0]) > Number(filterValues[1])) || (['', undefined] as Array).includes(filterValues[1]) || filterFn_lessThanOrEqualTo(row, columnId, filterValues[1])), - { autoRemove: (val: any) => !val }, + { + autoRemove: (val: any) => + testFalsy(val) || (testFalsy(val[0]) && testFalsy(val[1])), + }, ) /** diff --git a/packages/table-core/tests/unit/fns/filterFns.test.ts b/packages/table-core/tests/unit/fns/filterFns.test.ts index 8b896576f7..1b6f74a092 100644 --- a/packages/table-core/tests/unit/fns/filterFns.test.ts +++ b/packages/table-core/tests/unit/fns/filterFns.test.ts @@ -12,6 +12,7 @@ import { filterFn_lessThan, filterFn_lessThanOrEqualTo, filterFn_weakEquals, + filterFns, } from '../../../src' import { getStaticTestData } from '../../fixtures/data/generateTestData' @@ -512,4 +513,54 @@ describe('Filter Functions', () => { }) }) }) + + describe('Range Filters', () => { + describe('filterFns.between.autoRemove', () => { + const autoRemove = filterFns.between.autoRemove! + + it('should auto-remove when both endpoints are undefined', () => { + expect(autoRemove([undefined, undefined])).toBe(true) + }) + it('should auto-remove when both endpoints are null', () => { + expect(autoRemove([null, null])).toBe(true) + }) + it('should auto-remove when both endpoints are empty strings', () => { + expect(autoRemove(['', ''])).toBe(true) + }) + it('should NOT auto-remove when both endpoints are valid numbers', () => { + expect(autoRemove([5, 10])).toBe(false) + }) + it('should NOT auto-remove when lower bound is 0 (falsy number)', () => { + expect(autoRemove([0, 10])).toBe(false) + }) + it('should NOT auto-remove when only one endpoint is provided', () => { + expect(autoRemove([undefined, 10])).toBe(false) + expect(autoRemove([5, undefined])).toBe(false) + }) + }) + + describe('filterFns.betweenInclusive.autoRemove', () => { + const autoRemove = filterFns.betweenInclusive.autoRemove! + + it('should auto-remove when both endpoints are undefined', () => { + expect(autoRemove([undefined, undefined])).toBe(true) + }) + it('should auto-remove when both endpoints are null', () => { + expect(autoRemove([null, null])).toBe(true) + }) + it('should auto-remove when both endpoints are empty strings', () => { + expect(autoRemove(['', ''])).toBe(true) + }) + it('should NOT auto-remove when both endpoints are valid numbers', () => { + expect(autoRemove([5, 10])).toBe(false) + }) + it('should NOT auto-remove when lower bound is 0 (falsy number)', () => { + expect(autoRemove([0, 10])).toBe(false) + }) + it('should NOT auto-remove when only one endpoint is provided', () => { + expect(autoRemove([undefined, 10])).toBe(false) + expect(autoRemove([5, undefined])).toBe(false) + }) + }) + }) })