feat(editor): Add missing extension methods for expressions (#8845)

This commit is contained in:
Elias Meire
2024-03-20 12:05:54 +01:00
committed by GitHub
parent 7176cd1407
commit 5e84c2ab89
28 changed files with 809 additions and 39 deletions

View File

@@ -2,6 +2,7 @@
* @jest-environment jsdom
*/
import { arrayExtensions } from '../../src/Extensions/ArrayExtensions';
import { evaluate } from './Helpers';
describe('Data Transformation Functions', () => {
@@ -234,5 +235,27 @@ describe('Data Transformation Functions', () => {
[16, 17, 18, 19, 20],
]);
});
test('.toJsonString() should work on an array', () => {
expect(evaluate('={{ [true, 1, "one", {foo: "bar"}].toJsonString() }}')).toEqual(
'[true,1,"one",{"foo":"bar"}]',
);
});
describe('Conversion methods', () => {
test('should exist but return undefined (to not break expressions with mixed data)', () => {
expect(evaluate('={{ numberList(1, 20).toInt() }}')).toBeUndefined();
expect(evaluate('={{ numberList(1, 20).toFloat() }}')).toBeUndefined();
expect(evaluate('={{ numberList(1, 20).toBoolean() }}')).toBeUndefined();
expect(evaluate('={{ numberList(1, 20).toDateTime() }}')).toBeUndefined();
});
test('should not have a doc (hidden from autocomplete)', () => {
expect(arrayExtensions.functions.toInt.doc).toBeUndefined();
expect(arrayExtensions.functions.toFloat.doc).toBeUndefined();
expect(arrayExtensions.functions.toBoolean.doc).toBeUndefined();
expect(arrayExtensions.functions.toDateTime.doc).toBeUndefined();
});
});
});
});

View File

@@ -0,0 +1,39 @@
/**
* @jest-environment jsdom
*/
import { booleanExtensions } from '../../src/Extensions/BooleanExtensions';
import { evaluate } from './Helpers';
describe('Data Transformation Functions', () => {
describe('Boolean Data Transformation Functions', () => {
describe('Conversion methods', () => {
describe('toInt/toFloat', () => {
test('should return 1 for true, 0 for false', () => {
expect(evaluate('={{ (true).toInt() }}')).toEqual(1);
expect(evaluate('={{ (true).toFloat() }}')).toEqual(1);
expect(evaluate('={{ (false).toInt() }}')).toEqual(0);
expect(evaluate('={{ (false).toFloat() }}')).toEqual(0);
});
});
describe('toDateTime', () => {
test('should return undefined', () => {
expect(evaluate('={{ (true).toDateTime() }}')).toBeUndefined();
});
});
describe('toBoolean', () => {
test('should return itself', () => {
expect(evaluate('={{ (true).toDateTime() }}')).toBeUndefined();
});
});
test('should not have a doc (hidden from autocomplete)', () => {
expect(booleanExtensions.functions.toFloat.doc).toBeUndefined();
expect(booleanExtensions.functions.toBoolean.doc).toBeUndefined();
expect(booleanExtensions.functions.toDateTime.doc).toBeUndefined();
});
});
});
});

View File

@@ -5,6 +5,7 @@
import { DateTime } from 'luxon';
import { getGlobalState } from '@/GlobalState';
import { evaluate, getLocalISOString } from './Helpers';
import { dateExtensions } from '../../src/Extensions/DateExtensions';
const { defaultTimezone } = getGlobalState();
@@ -107,5 +108,56 @@ describe('Data Transformation Functions', () => {
evaluate("={{ $now.isBetween($now, '2023-06-23', '2023-09-21'.toDate()) }}"),
).toThrow();
});
describe('toDateTime', () => {
test('should return itself for DateTime', () => {
const result = evaluate(
"={{ DateTime.fromFormat('01-01-2024', 'dd-MM-yyyy').toDateTime() }}",
) as unknown as DateTime;
expect(result).toBeInstanceOf(DateTime);
expect(result.day).toEqual(1);
expect(result.month).toEqual(1);
expect(result.year).toEqual(2024);
});
test('should return a DateTime for JS Date', () => {
const result = evaluate(
'={{ new Date(2024, 0, 1, 12).toDateTime() }}',
) as unknown as DateTime;
expect(result).toBeInstanceOf(DateTime);
expect(result.day).toEqual(1);
expect(result.month).toEqual(1);
expect(result.year).toEqual(2024);
});
});
describe('toInt/toFloat', () => {
test('should return milliseconds for DateTime', () => {
expect(evaluate("={{ DateTime.fromISO('2024-01-01T00:00:00.000Z').toInt() }}")).toEqual(
1704067200000,
);
});
test('should return milliseconds for JS Date', () => {
expect(evaluate('={{ new Date("2024-01-01T00:00:00.000Z").toFloat() }}')).toEqual(
1704067200000,
);
});
test('should not have a doc (hidden from autocomplete)', () => {
expect(dateExtensions.functions.toInt.doc).toBeUndefined();
expect(dateExtensions.functions.toFloat.doc).toBeUndefined();
});
});
describe('toBoolean', () => {
test('should return undefined', () => {
expect(evaluate('={{ new Date("2024-01-01T00:00:00.000Z").toBoolean() }}')).toBeUndefined();
});
test('should not have a doc (hidden from autocomplete)', () => {
expect(dateExtensions.functions.toBoolean.doc).toBeUndefined();
});
});
});
});

View File

@@ -55,6 +55,52 @@ describe('Data Transformation Functions', () => {
expect(() => evaluate('={{ (NaN).isEven() }}')).toThrow();
expect(() => evaluate('={{ (9.2).isEven() }}')).toThrow();
});
describe('toDateTime', () => {
test('from milliseconds (default)', () => {
expect(evaluate('={{ (1704085200000).toDateTime().toISO() }}')).toEqual(
'2024-01-01T00:00:00.000-05:00',
);
expect(evaluate('={{ (1704085200000).toDateTime("ms").toISO() }}')).toEqual(
'2024-01-01T00:00:00.000-05:00',
);
});
test('from seconds', () => {
expect(evaluate('={{ (1704085200).toDateTime("s").toISO() }}')).toEqual(
'2024-01-01T00:00:00.000-05:00',
);
});
test('from Excel 1900 format', () => {
expect(evaluate('={{ (42144).toDateTime("excel").toISO() }}')).toEqual(
'2015-05-19T20:00:00.000-04:00',
);
});
});
describe('toInt', () => {
test('should round numbers', () => {
expect(evaluate('={{ (42144).toInt() }}')).toEqual(42144);
expect(evaluate('={{ (42144.345).toInt() }}')).toEqual(42144);
expect(evaluate('={{ (42144.545).toInt() }}')).toEqual(42145);
});
});
describe('toFloat', () => {
test('should return itself', () => {
expect(evaluate('={{ (42144).toFloat() }}')).toEqual(42144);
expect(evaluate('={{ (42144.345).toFloat() }}')).toEqual(42144.345);
});
});
describe('toBoolean', () => {
test('should return false for 0, 1 for other numbers', () => {
expect(evaluate('={{ (42144).toBoolean() }}')).toBe(true);
expect(evaluate('={{ (-1.549).toBoolean() }}')).toBe(true);
expect(evaluate('={{ (0).toBoolean() }}')).toBe(false);
});
});
});
describe('Multiple expressions', () => {

View File

@@ -1,3 +1,4 @@
import { objectExtensions } from '../../src/Extensions/ObjectExtensions';
import { evaluate } from './Helpers';
describe('Data Transformation Functions', () => {
@@ -89,5 +90,27 @@ describe('Data Transformation Functions', () => {
test('.values should work on an object', () => {
expect(evaluate('={{ ({ test1: 1, test2: "2" }).values() }}')).toEqual([1, '2']);
});
test('.toJsonString() should work on an object', () => {
expect(evaluate('={{ ({ test1: 1, test2: "2" }).toJsonString() }}')).toEqual(
'{"test1":1,"test2":"2"}',
);
});
describe('Conversion methods', () => {
test('should exist but return undefined (to not break expressions with mixed data)', () => {
expect(evaluate('={{ ({ test1: 1, test2: "2" }).toInt() }}')).toBeUndefined();
expect(evaluate('={{ ({ test1: 1, test2: "2" }).toFloat() }}')).toBeUndefined();
expect(evaluate('={{ ({ test1: 1, test2: "2" }).toBoolean() }}')).toBeUndefined();
expect(evaluate('={{ ({ test1: 1, test2: "2" }).toDateTime() }}')).toBeUndefined();
});
it('should not have a doc (hidden from autocomplete)', () => {
expect(objectExtensions.functions.toInt.doc).toBeUndefined();
expect(objectExtensions.functions.toFloat.doc).toBeUndefined();
expect(objectExtensions.functions.toBoolean.doc).toBeUndefined();
expect(objectExtensions.functions.toDateTime.doc).toBeUndefined();
});
});
});
});

View File

@@ -1,7 +1,9 @@
/**
* @jest-environment jsdom
*/
import { DateTime } from 'luxon';
import { evaluate } from './Helpers';
import { ExpressionExtensionError } from '../../src/errors';
describe('Data Transformation Functions', () => {
describe('String Data Transformation Functions', () => {
@@ -244,5 +246,48 @@ describe('Data Transformation Functions', () => {
expect(evaluate('={{ "aaaaaaaa".isEmail() }}')).toEqual(false);
expect(evaluate('={{ "test @ n8n".isEmail() }}')).toEqual(false);
});
test('.toDateTime should work on a variety of formats', () => {
expect(evaluate('={{ "Wed, 21 Oct 2015 07:28:00 GMT".toDateTime() }}')).toBeInstanceOf(
DateTime,
);
expect(evaluate('={{ "2008-11-11".toDateTime() }}')).toBeInstanceOf(DateTime);
expect(evaluate('={{ "1-Feb-2024".toDateTime() }}')).toBeInstanceOf(DateTime);
expect(() => evaluate('={{ "hi".toDateTime() }}')).toThrowError(
new ExpressionExtensionError('cannot convert to Luxon DateTime'),
);
});
test('.extractUrlPath should work on a string', () => {
expect(
evaluate('={{ "https://example.com/orders/1/detail#hash?foo=bar".extractUrlPath() }}'),
).toEqual('/orders/1/detail');
expect(evaluate('={{ "hi".extractUrlPath() }}')).toBeUndefined();
});
test('.parseJson should work on a string', () => {
expect(evaluate('={{ \'{"test1":1,"test2":"2"}\'.parseJson() }}')).toEqual({
test1: 1,
test2: '2',
});
expect(evaluate('={{ "hi".parseJson() }}')).toBeUndefined();
});
test('.toBoolean should work on a string', () => {
expect(evaluate('={{ "False".toBoolean() }}')).toBe(false);
expect(evaluate('={{ "".toBoolean() }}')).toBe(false);
expect(evaluate('={{ "0".toBoolean() }}')).toBe(false);
expect(evaluate('={{ "no".toBoolean() }}')).toBe(false);
expect(evaluate('={{ "TRUE".toBoolean() }}')).toBe(true);
expect(evaluate('={{ "hello".toBoolean() }}')).toBe(true);
});
test('.base64Encode should work on a string', () => {
expect(evaluate('={{ "n8n test".base64Encode() }}')).toBe('bjhuIHRlc3Q=');
});
test('.base64Decode should work on a string', () => {
expect(evaluate('={{ "bjhuIHRlc3Q=".base64Decode() }}')).toBe('n8n test');
});
});
});