From c15f9b70257688d24fe59b0a51902a4834f80e85 Mon Sep 17 00:00:00 2001 From: Elias Meire Date: Thu, 25 Jan 2024 15:13:17 +0100 Subject: [PATCH] Add new 'is empty' and 'is not empty' operators to Filter --- .../components/FilterConditions/constants.ts | 12 +++++++ .../src/NodeParameters/FilterParameter.ts | 4 +++ .../workflow/test/FilterParameter.test.ts | 32 +++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/packages/editor-ui/src/components/FilterConditions/constants.ts b/packages/editor-ui/src/components/FilterConditions/constants.ts index 578d74c71a362..59d65975298a2 100644 --- a/packages/editor-ui/src/components/FilterConditions/constants.ts +++ b/packages/editor-ui/src/components/FilterConditions/constants.ts @@ -23,6 +23,18 @@ export const OPERATORS_BY_ID = { name: 'filter.operator.notExists', singleValue: true, }, + 'string:empty': { + type: 'string', + operation: 'empty', + name: 'filter.operator.empty', + singleValue: true, + }, + 'string:notEmpty': { + type: 'string', + operation: 'notEmpty', + name: 'filter.operator.notEmpty', + singleValue: true, + }, 'string:equals': { type: 'string', operation: 'equals', name: 'filter.operator.equals' }, 'string:notEquals': { type: 'string', operation: 'notEquals', name: 'filter.operator.notEquals' }, 'string:contains': { type: 'string', operation: 'contains', name: 'filter.operator.contains' }, diff --git a/packages/workflow/src/NodeParameters/FilterParameter.ts b/packages/workflow/src/NodeParameters/FilterParameter.ts index c66b3e5140baf..ac3810390feac 100644 --- a/packages/workflow/src/NodeParameters/FilterParameter.ts +++ b/packages/workflow/src/NodeParameters/FilterParameter.ts @@ -179,6 +179,10 @@ export function executeFilterCondition( const right = (rightValue ?? '') as string; switch (condition.operator.operation) { + case 'empty': + return left.length === 0; + case 'notEmpty': + return left.length !== 0; case 'equals': return left === right; case 'notEquals': diff --git a/packages/workflow/test/FilterParameter.test.ts b/packages/workflow/test/FilterParameter.test.ts index 3dde0b66791ba..4b6e8e7f8a22c 100644 --- a/packages/workflow/test/FilterParameter.test.ts +++ b/packages/workflow/test/FilterParameter.test.ts @@ -250,6 +250,38 @@ describe('FilterParameter', () => { }); describe('string', () => { + it.each([ + { left: null, expected: true }, + { left: undefined, expected: true }, + { left: '', expected: true }, + { left: '🐛', expected: false }, + ])('string:empty($left) === $expected', ({ left, expected }) => { + const result = executeFilter( + filterFactory({ + conditions: [ + { id: '1', leftValue: left, operator: { operation: 'empty', type: 'string' } }, + ], + }), + ); + expect(result).toBe(expected); + }); + + it.each([ + { left: null, expected: false }, + { left: undefined, expected: false }, + { left: '', expected: false }, + { left: '🐛', expected: true }, + ])('string:notEmpty($left) === $expected', ({ left, expected }) => { + const result = executeFilter( + filterFactory({ + conditions: [ + { id: '1', leftValue: left, operator: { operation: 'notEmpty', type: 'string' } }, + ], + }), + ); + expect(result).toBe(expected); + }); + it.each([ { left: 'first string', right: 'first string', expected: true }, { left: 'first string', right: 'second string', expected: false },