Skip to content

Commit

Permalink
Add helper function to create filter sets
Browse files Browse the repository at this point in the history
  • Loading branch information
alexesprit committed Nov 22, 2020
1 parent 883efdc commit 690867d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
41 changes: 40 additions & 1 deletion test/filter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
});
});

0 comments on commit 690867d

Please sign in to comment.