Skip to content

Commit

Permalink
feat(editor): Add isEmpty on DateTime, add is empty to all types in f…
Browse files Browse the repository at this point in the history
…ilter component (#9645)
  • Loading branch information
elsmr authored and RicardoE105 committed Jun 9, 2024
1 parent 8f93b0b commit a83c32b
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 4 deletions.
36 changes: 36 additions & 0 deletions packages/editor-ui/src/components/FilterConditions/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ export const OPERATORS_BY_ID = {
name: 'filter.operator.notExists',
singleValue: true,
},
'number:empty': {
type: 'number',
operation: 'empty',
name: 'filter.operator.empty',
singleValue: true,
},
'number:notEmpty': {
type: 'number',
operation: 'notEmpty',
name: 'filter.operator.notEmpty',
singleValue: true,
},
'number:equals': { type: 'number', operation: 'equals', name: 'filter.operator.equals' },
'number:notEquals': { type: 'number', operation: 'notEquals', name: 'filter.operator.notEquals' },
'number:gt': { type: 'number', operation: 'gt', name: 'filter.operator.gt' },
Expand All @@ -91,6 +103,18 @@ export const OPERATORS_BY_ID = {
name: 'filter.operator.notExists',
singleValue: true,
},
'dateTime:empty': {
type: 'dateTime',
operation: 'empty',
name: 'filter.operator.empty',
singleValue: true,
},
'dateTime:notEmpty': {
type: 'dateTime',
operation: 'notEmpty',
name: 'filter.operator.notEmpty',
singleValue: true,
},
'dateTime:equals': { type: 'dateTime', operation: 'equals', name: 'filter.operator.equals' },
'dateTime:notEquals': {
type: 'dateTime',
Expand Down Expand Up @@ -121,6 +145,18 @@ export const OPERATORS_BY_ID = {
name: 'filter.operator.notExists',
singleValue: true,
},
'boolean:empty': {
type: 'boolean',
operation: 'empty',
name: 'filter.operator.empty',
singleValue: true,
},
'boolean:notEmpty': {
type: 'boolean',
operation: 'notEmpty',
name: 'filter.operator.notEmpty',
singleValue: true,
},
'boolean:true': {
type: 'boolean',
operation: 'true',
Expand Down
2 changes: 1 addition & 1 deletion packages/workflow/src/Extensions/ArrayExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ compact.doc = {

isEmpty.doc = {
name: 'isEmpty',
description: 'Returns <code>true</code> if the array has no elements',
description: 'Returns <code>true</code> if the array has no elements or is <code>null</code>',
examples: [
{ example: '[].isEmpty()', evaluated: 'true' },
{ example: "['quick', 'brown', 'fox'].isEmpty()", evaluated: 'false' },
Expand Down
37 changes: 36 additions & 1 deletion packages/workflow/src/Extensions/DateExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,15 @@ function toBoolean() {
return undefined;
}

// Only null/undefined return true, this is handled in ExpressionExtension.ts
function isEmpty(): boolean {
return false;
}

function isNotEmpty(): boolean {
return true;
}

endOfMonth.doc = {
name: 'endOfMonth',
returnType: 'DateTime',
Expand Down Expand Up @@ -547,7 +556,7 @@ diffToNow.doc = {
evaluated: '371.9',
},
{
example: "dt = '2023-03-30T18:49:07.234.toDateTime()\ndt.diffToNow(['months', 'days'])",
example: "dt = '2023-03-30T18:49:07.234'.toDateTime()\ndt.diffToNow(['months', 'days'])",
evaluated: '{ months: 12, days: 5.9 }',
},
],
Expand All @@ -565,6 +574,30 @@ diffToNow.doc = {
docURL: 'https://docs.n8n.io/code/builtin/data-transformation-functions/dates/#date-diffToNow',
};

isEmpty.doc = {
name: 'isEmpty',
description:
'Returns <code>false</code> for all DateTimes. Returns <code>true</code> for <code>null</code>.',
examples: [
{ example: "dt = '2023-03-30T18:49:07.234'.toDateTime()\ndt.isEmpty()", evaluated: 'false' },
{ example: 'dt = null\ndt.isEmpty()', evaluated: 'true' },
],
returnType: 'boolean',
docURL: 'https://docs.n8n.io/code/builtin/data-transformation-functions/arrays/#array-isEmpty',
};

isNotEmpty.doc = {
name: 'isNotEmpty',
description:
'Returns <code>true</code> for all DateTimes. Returns <code>false</code> for <code>null</code>.',
examples: [
{ example: "dt = '2023-03-30T18:49:07.234'.toDateTime()\ndt.isNotEmpty()", evaluated: 'true' },
{ example: 'dt = null\ndt.isNotEmpty()', evaluated: 'false' },
],
returnType: 'boolean',
docURL: 'https://docs.n8n.io/code/builtin/data-transformation-functions/arrays/#array-isNotEmpty',
};

export const dateExtensions: ExtensionMap = {
typeName: 'Date',
functions: {
Expand All @@ -584,5 +617,7 @@ export const dateExtensions: ExtensionMap = {
toInt,
toFloat,
toBoolean,
isEmpty,
isNotEmpty,
},
};
3 changes: 2 additions & 1 deletion packages/workflow/src/Extensions/ObjectExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ export function toDateTime() {

isEmpty.doc = {
name: 'isEmpty',
description: 'Returns <code>true</code> if the Object has no keys (fields) set',
description:
'Returns <code>true</code> if the Object has no keys (fields) set or is <code>null</code>',
examples: [
{ example: "({'name': 'Nathan'}).isEmpty()", evaluated: 'false' },
{ example: '({}).isEmpty()', evaluated: 'true' },
Expand Down
2 changes: 1 addition & 1 deletion packages/workflow/src/Extensions/StringExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ isUrl.doc = {

isEmpty.doc = {
name: 'isEmpty',
description: 'Returns <code>true</code> if the string has no characters.',
description: 'Returns <code>true</code> if the string has no characters or is <code>null</code>',
section: 'validation',
returnType: 'boolean',
docURL: 'https://docs.n8n.io/code/builtin/data-transformation-functions/strings/#string-isEmpty',
Expand Down
14 changes: 14 additions & 0 deletions packages/workflow/src/NodeParameters/FilterParameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ export function executeFilterCondition(
const right = rightValue as number;

switch (condition.operator.operation) {
case 'empty':
return !exists;
case 'notEmpty':
return exists;
case 'equals':
return left === right;
case 'notEquals':
Expand All @@ -230,6 +234,12 @@ export function executeFilterCondition(
const left = leftValue as DateTime;
const right = rightValue as DateTime;

if (condition.operator.operation === 'empty') {
return !exists;
} else if (condition.operator.operation === 'notEmpty') {
return exists;
}

if (!left || !right) {
return false;
}
Expand All @@ -254,6 +264,10 @@ export function executeFilterCondition(
const right = rightValue as boolean;

switch (condition.operator.operation) {
case 'empty':
return !exists;
case 'notEmpty':
return exists;
case 'true':
return left;
case 'false':
Expand Down
116 changes: 116 additions & 0 deletions packages/workflow/test/FilterParameter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,48 @@ describe('FilterParameter', () => {
expect(result).toBe(expected);
});

it.each([
{ left: 0, expected: false },
{ left: 15, expected: false },
{ left: -15.4, expected: false },
{ left: NaN, expected: true },
{ left: null, expected: true },
])('number:empty($left) === $expected', ({ left, expected }) => {
const result = executeFilter(
filterFactory({
conditions: [
{
id: '1',
leftValue: left,
operator: { operation: 'empty', type: 'number' },
},
],
}),
);
expect(result).toBe(expected);
});

it.each([
{ left: 0, expected: true },
{ left: 15, expected: true },
{ left: -15.4, expected: true },
{ left: NaN, expected: false },
{ left: null, expected: false },
])('number:notEmpty($left) === $expected', ({ left, expected }) => {
const result = executeFilter(
filterFactory({
conditions: [
{
id: '1',
leftValue: left,
operator: { operation: 'notEmpty', type: 'number' },
},
],
}),
);
expect(result).toBe(expected);
});

it.each([
{ left: 0, right: 0, expected: true },
{ left: 15, right: 15, expected: true },
Expand Down Expand Up @@ -706,6 +748,42 @@ describe('FilterParameter', () => {
});

describe('dateTime', () => {
it.each([
{ left: '2023-11-15T17:10:49.113Z', expected: false },
{ left: null, expected: true },
])('dateTime:empty($left) === $expected', ({ left, expected }) => {
const result = executeFilter(
filterFactory({
conditions: [
{
id: '1',
leftValue: left,
operator: { operation: 'empty', type: 'dateTime' },
},
],
}),
);
expect(result).toBe(expected);
});

it.each([
{ left: '2023-11-15T17:10:49.113Z', expected: true },
{ left: null, expected: false },
])('dateTime:notEmpty($left) === $expected', ({ left, expected }) => {
const result = executeFilter(
filterFactory({
conditions: [
{
id: '1',
leftValue: left,
operator: { operation: 'notEmpty', type: 'dateTime' },
},
],
}),
);
expect(result).toBe(expected);
});

it.each([
{ left: '2023-11-15T17:10:49.113Z', right: '2023-11-15T17:10:49.113Z', expected: true },
{ left: '2023-11-15T17:10:49.113Z', right: '2023-11-15T17:12:49.113Z', expected: false },
Expand Down Expand Up @@ -838,6 +916,44 @@ describe('FilterParameter', () => {
});

describe('boolean', () => {
it.each([
{ left: true, expected: false },
{ left: false, expected: false },
{ left: null, expected: true },
])('boolean:empty($left) === $expected', ({ left, expected }) => {
const result = executeFilter(
filterFactory({
conditions: [
{
id: '1',
leftValue: left,
operator: { operation: 'empty', type: 'boolean' },
},
],
}),
);
expect(result).toBe(expected);
});

it.each([
{ left: true, expected: true },
{ left: false, expected: true },
{ left: null, expected: false },
])('boolean:notEmpty($left) === $expected', ({ left, expected }) => {
const result = executeFilter(
filterFactory({
conditions: [
{
id: '1',
leftValue: left,
operator: { operation: 'notEmpty', type: 'boolean' },
},
],
}),
);
expect(result).toBe(expected);
});

it.each([
{ left: true, expected: true },
{ left: false, expected: false },
Expand Down

0 comments on commit a83c32b

Please sign in to comment.