From 690867d9f832ff7fc1a8788c5c0e67f0c157bc0e Mon Sep 17 00:00:00 2001 From: alexesprit Date: Sun, 22 Nov 2020 23:55:58 +0300 Subject: [PATCH] Add helper function to create filter sets --- src/filter.ts | 30 ++++++++++++++++++++++++++++++ test/filter.spec.ts | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/src/filter.ts b/src/filter.ts index 48fd95c..398e676 100644 --- a/src/filter.ts +++ b/src/filter.ts @@ -15,6 +15,36 @@ export function createFilter(filterSet: FilterSet): MetadataFilter { return new MetadataFilter(filterSet); } +/** + * Create a filter set where each given field has given filter function(s). + * Useful to create a filter where multiple fields should be filtered with + * the same filter functions. + * + * @param fields Array of fields to filter + * @param filterFn Filter function or array of filter functions + * + * @return Filter set object + */ +export function createFilterSetForFields( + fields: string[], + filterFn: FilterFuncion | FilterFuncion[] +): FilterSet { + if (!Array.isArray(fields)) { + throw new TypeError( + `Invalid 'fields' argument: expected 'string[]', got '${typeof fields}'` + ); + } + + if (fields.length === 0) { + throw new Error("Invalid 'fields' argument: received an empty array"); + } + + return fields.reduce((acc, field) => { + acc[field] = filterFn; + return acc; + }, {} as FilterSet); +} + /** * Base filter object that filters metadata fields by given filter set. * diff --git a/test/filter.spec.ts b/test/filter.spec.ts index 7e91c49..7893a35 100644 --- a/test/filter.spec.ts +++ b/test/filter.spec.ts @@ -2,7 +2,7 @@ import chai from 'chai'; import { expect } from 'chai'; import spies from 'chai-spies'; -import { createFilter } from '../src/filter'; +import { createFilter, createFilterSetForFields } from '../src/filter'; import { dummyFn } from './helper/util'; import { testExtendedFilter } from './helper/test-filter'; @@ -154,3 +154,42 @@ describe('Test method chaining', () => { testExtendedFilter(filter, fn1, fn2, fn3); }); + +describe('Test creating filter set for fields', () => { + const fn1 = chai.spy(dummyFn); + const fn2 = chai.spy(dummyFn); + + it('should throw error when received invalid argument', () => { + // @ts-ignore + expect(() => createFilterSetForFields(null, fn1)).to.throw; + }); + + it('should throw error when received empty fields array', () => { + expect(() => createFilterSetForFields([], fn1)).to.throw; + }); + + it('should create filter set with single functions', () => { + const filterSet = createFilterSetForFields(['foo', 'bar', 'baz'], fn1); + const expectedResult = { + foo: fn1, + bar: fn1, + baz: fn1, + }; + + expect(filterSet).to.be.deep.equal(expectedResult); + }); + + it('should create filter set with multiple filter functions', () => { + const filterSet = createFilterSetForFields( + ['foo', 'bar', 'baz'], + [fn1, fn2] + ); + const expectedResult = { + foo: [fn1, fn2], + bar: [fn1, fn2], + baz: [fn1, fn2], + }; + + expect(filterSet).to.be.deep.equal(expectedResult); + }); +});