diff --git a/src/filter.ts b/src/filter.ts index 98df3ed..48fd95c 100644 --- a/src/filter.ts +++ b/src/filter.ts @@ -58,11 +58,11 @@ export class MetadataFilter { * @throws Throw an error if an invalid field is specified */ filterField(field: string, fieldValue: string): string { - if (!(field in this.mergedFilterSet)) { - throw new TypeError(`Invalid filter field: ${field}`); + if (field in this.mergedFilterSet) { + return this.filterText(fieldValue, this.mergedFilterSet[field]); } - return this.filterText(fieldValue, this.mergedFilterSet[field]); + throw new TypeError(`Invalid filter field: ${field}`); } /** diff --git a/test/filter.spec.ts b/test/filter.spec.ts index 33257a9..7e91c49 100644 --- a/test/filter.spec.ts +++ b/test/filter.spec.ts @@ -111,6 +111,7 @@ describe('Test filtering empty strings', () => { it('should not call filter function is the input is null', () => { const input = null; + // @ts-ignore const actual = filter.filterField('artist', input); expect(actual).to.be.equal(input); @@ -127,6 +128,7 @@ describe('Test filtering invalid filter field', () => { describe('Test invalid filter', () => { it('should throw error if the filter set is not specified', () => { + // @ts-ignore expect(() => createFilter(null)).to.throw(); }); diff --git a/test/helper/util.ts b/test/helper/util.ts index 09dd064..53faa83 100644 --- a/test/helper/util.ts +++ b/test/helper/util.ts @@ -1,4 +1,4 @@ -import { MetadataFilter, createFilter } from '../../src/filter'; +import { MetadataFilter, createFilter, FilterSet } from '../../src/filter'; import { FilterFuncion } from '../../src/functions'; /** @@ -17,7 +17,7 @@ export function createFilterFromFunction( const filterSet = fields.reduce((filterSet, field) => { filterSet[field] = filterFunc; return filterSet; - }, {}); + }, {} as FilterSet); return createFilter(filterSet); }