From bb108567d0fa6b93982c39e5b5cca68468972536 Mon Sep 17 00:00:00 2001 From: Olesya KOROVINA Date: Fri, 10 Sep 2021 09:27:01 +0200 Subject: [PATCH] feat(filter): add "is in" filter (#806) --- src/services/filters-parser.js | 4 +++- test/services/filters-parser.test.js | 8 ++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/services/filters-parser.js b/src/services/filters-parser.js index 2011b7af..1a4ecb85 100644 --- a/src/services/filters-parser.js +++ b/src/services/filters-parser.js @@ -94,7 +94,9 @@ function FiltersParser(modelSchema, timezone, options) { case 'includes_all': return { [this.OPERATORS.CONTAINS]: value }; case 'in': - return { [this.OPERATORS.IN]: value }; + return typeof value === 'string' + ? { [this.OPERATORS.IN]: value.split(',').map((elem) => elem.trim()) } + : { [this.OPERATORS.IN]: value }; default: throw new NoMatchingOperatorError(); } diff --git a/test/services/filters-parser.test.js b/test/services/filters-parser.test.js index 05dfae04..b5e02804 100644 --- a/test/services/filters-parser.test.js +++ b/test/services/filters-parser.test.js @@ -88,7 +88,7 @@ describe('services > filters-parser', () => { }); describe('formatOperatorValue function', () => { - const values = [5, 'toto', null]; + const values = [5, 'toto,tutu ', null]; values.forEach((value) => { it(`should return the appropriate value (${typeof value})`, () => { @@ -113,7 +113,11 @@ describe('services > filters-parser', () => { [OPERATORS.EQ]: '', }], }); - expect(defaultFiltersParser.formatOperatorValue('in', value)).toStrictEqual({ [OPERATORS.IN]: value }); + expect(defaultFiltersParser.formatOperatorValue('in', value)).toStrictEqual( + typeof value === 'string' + ? { [OPERATORS.IN]: value.split(',').map((elem) => elem.trim()) } + : { [OPERATORS.IN]: value }, + ); }); it('should raise an error on unknown operator', () => {