mirror of
https://github.com/n8n-io/n8n.git
synced 2026-12-05 01:00:52 +00:00
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
553 lines
23 KiB
TypeScript
553 lines
23 KiB
TypeScript
// @vitest-environment jsdom
|
|
import { DateTime } from 'luxon';
|
|
|
|
import { evaluate, asDateTime } from './helpers';
|
|
import { ExpressionExtensionError } from '../../src/errors';
|
|
|
|
describe('Data Transformation Functions', () => {
|
|
describe('String Data Transformation Functions', () => {
|
|
describe('.isEmpty', () => {
|
|
test('should work correctly on a string that is not empty', () => {
|
|
expect(evaluate('={{"NotBlank".isEmpty()}}')).toEqual(false);
|
|
});
|
|
|
|
test('should work correctly on a string that is empty', () => {
|
|
expect(evaluate('={{"".isEmpty()}}')).toEqual(true);
|
|
});
|
|
});
|
|
|
|
describe('.isNotEmpty', () => {
|
|
test('should work correctly on a string that is not empty', () => {
|
|
expect(evaluate('={{"NotBlank".isNotEmpty()}}')).toEqual(true);
|
|
});
|
|
|
|
test('should work correctly on a string that is empty', () => {
|
|
expect(evaluate('={{"".isNotEmpty()}}')).toEqual(false);
|
|
});
|
|
});
|
|
|
|
test('.length should return the string length', () => {
|
|
expect(evaluate('={{"String".length()}}')).toEqual(6);
|
|
});
|
|
|
|
describe('.hash()', () => {
|
|
test.each([
|
|
['base64', 'MTIzNDU='],
|
|
['md5', '827ccb0eea8a706c4c34a16891f84e7b'],
|
|
['sha1', '8cb2237d0679ca88db6464eac60da96345513964'],
|
|
['sha224', 'a7470858e79c282bc2f6adfd831b132672dfd1224c1e78cbf5bcd057'],
|
|
['sha256', '5994471abb01112afcc18159f6cc74b4f511b99806da59b3caf5a9c173cacfc5'],
|
|
[
|
|
'sha384',
|
|
'0fa76955abfa9dafd83facca8343a92aa09497f98101086611b0bfa95dbc0dcc661d62e9568a5a032ba81960f3e55d4a',
|
|
],
|
|
[
|
|
'sha512',
|
|
'3627909a29c31381a071ec27f7c9ca97726182aed29a7ddd2e54353322cfb30abb9e3a6df2ac2c20fe23436311d678564d0c8d305930575f60e2d3d048184d79',
|
|
],
|
|
[
|
|
'sha3',
|
|
'0a2a1719bf3ce682afdbedf3b23857818d526efbe7fcb372b31347c26239a0f916c398b7ad8dd0ee76e8e388604d0b0f925d5e913ad2d3165b9b35b3844cd5e6',
|
|
],
|
|
])('should work for %p', (hashFn, hashValue) => {
|
|
expect(evaluate(`={{ "12345".hash("${hashFn}") }}`)).toEqual(hashValue);
|
|
expect(evaluate(`={{ "12345".hash("${hashFn.toLowerCase()}") }}`)).toEqual(hashValue);
|
|
});
|
|
|
|
test('should throw on invalid algorithm', () => {
|
|
expect(() => evaluate('={{ "12345".hash("invalid") }}')).toThrow('Unknown algorithm');
|
|
});
|
|
});
|
|
|
|
test('.urlDecode should work correctly on a string', () => {
|
|
expect(evaluate('={{ "string%20with%20spaces".urlDecode(false) }}')).toEqual(
|
|
'string with spaces',
|
|
);
|
|
});
|
|
|
|
test('.urlEncode should work correctly on a string', () => {
|
|
expect(evaluate('={{ "string with spaces".urlEncode(false) }}')).toEqual(
|
|
'string%20with%20spaces',
|
|
);
|
|
});
|
|
|
|
describe('.removeTags', () => {
|
|
test('should work correctly on a string', () => {
|
|
expect(evaluate('={{ "<html><head>test</head></html>".removeTags() }}')).toEqual('test');
|
|
});
|
|
|
|
test.each([
|
|
['<b>bold</b>, <a>link</a>', 'bold, link'],
|
|
['<p class="x">Hi</p><br/>there', 'Hithere'],
|
|
['<!-- note -->text', 'text'],
|
|
['<?xml version="1.0"?><a>x</a>', 'x'],
|
|
['<élan>x</élan>', 'x'],
|
|
['<_id>idhere</_id>', 'idhere'],
|
|
['<:x>y</:x>', 'y'],
|
|
['<ns:tag>y</ns:tag>', 'y'],
|
|
['trailing <b', 'trailing '],
|
|
])('should remove the tags from %s', (input, expected) => {
|
|
expect(evaluate('={{ $json.value.removeTags() }}', [{ value: input }])).toEqual(expected);
|
|
});
|
|
|
|
test.each([
|
|
['price < 100 USD', 'price 100 USD'],
|
|
['a<2 and b>1', 'a2 and b>1'],
|
|
['x <= y', 'x = y'],
|
|
['I <3 you', 'I 3 you'],
|
|
['a < b and c > d', 'a b and c > d'],
|
|
['<< 3', ' 3'],
|
|
['<>', '>'],
|
|
['<%= name %>', '%= name %>'],
|
|
['price <€50 today', 'price €50 today'],
|
|
['<b>bold</b> < 100', 'bold 100'],
|
|
])('should keep the text after a less-than sign in %s', (input, expected) => {
|
|
expect(evaluate('={{ $json.value.removeTags() }}', [{ value: input }])).toEqual(expected);
|
|
});
|
|
|
|
test.each([
|
|
['<<b>script>alert(1)<</b>/script>', 'script>alert(1)/script>'],
|
|
['<<img>img src=x onerror=alert(1)>', 'img src=x onerror=alert(1)>'],
|
|
])('should not leave a tag in the output of %s', (input, expected) => {
|
|
expect(evaluate('={{ $json.value.removeTags() }}', [{ value: input }])).toEqual(expected);
|
|
});
|
|
|
|
test('should not form a tag when two outputs are joined', () => {
|
|
expect(
|
|
evaluate('={{ $json.code.removeTags() + $json.phone.removeTags() }}', [
|
|
{ code: '+49 <', phone: 'a href=https://example.com>Call' },
|
|
]),
|
|
).toEqual('+49 a href=https://example.com>Call');
|
|
});
|
|
});
|
|
|
|
test('.removeMarkdown should work correctly on a string', () => {
|
|
expect(evaluate('={{ "<html><head>test</head></html>".removeMarkdown() }}')).toEqual('test');
|
|
});
|
|
|
|
test('.toLowerCase should work correctly on a string', () => {
|
|
expect(evaluate('={{ "TEST".toLowerCase() }}')).toEqual('test');
|
|
});
|
|
|
|
describe('.toDate', () => {
|
|
test('should work correctly on a date string', () => {
|
|
expect(evaluate('={{ "2022-09-01T19:42:28.164Z".toDate() }}')).toEqual(
|
|
new Date('2022-09-01T19:42:28.164Z'),
|
|
);
|
|
});
|
|
|
|
test('should throw on invalid date', () => {
|
|
expect(() => evaluate('={{ "2022-09-32T19:42:28.164Z".toDate() }}')).toThrow(
|
|
'cannot convert to date',
|
|
);
|
|
});
|
|
});
|
|
|
|
test('.toFloat should work correctly on a string', () => {
|
|
expect(evaluate('={{ "1.1".toFloat() }}')).toEqual(1.1);
|
|
expect(evaluate('={{ "1.1".toDecimalNumber() }}')).toEqual(1.1);
|
|
});
|
|
|
|
test('.toInt should work correctly on a string', () => {
|
|
expect(evaluate('={{ "1.1".toInt() }}')).toEqual(1);
|
|
expect(evaluate('={{ "1.1".toWholeNumber() }}')).toEqual(1);
|
|
expect(evaluate('={{ "1.5".toInt() }}')).toEqual(1);
|
|
expect(evaluate('={{ "1.5".toWholeNumber() }}')).toEqual(1);
|
|
});
|
|
|
|
test('.quote should work correctly on a string', () => {
|
|
expect(evaluate('={{ "test".quote() }}')).toEqual('"test"');
|
|
expect(evaluate('={{ "\\"test\\"".quote() }}')).toEqual('"\\"test\\""');
|
|
});
|
|
|
|
test('.isNumeric should work correctly on a string', () => {
|
|
expect(evaluate('={{ "".isNumeric() }}')).toEqual(false);
|
|
expect(evaluate('={{ "asdf".isNumeric() }}')).toEqual(false);
|
|
expect(evaluate('={{ "1234".isNumeric() }}')).toEqual(true);
|
|
expect(evaluate('={{ "4e4".isNumeric() }}')).toEqual(true);
|
|
expect(evaluate('={{ "4.4".isNumeric() }}')).toEqual(true);
|
|
});
|
|
|
|
test('.isUrl should work on a string', () => {
|
|
expect(evaluate('={{ "https://example.com/".isUrl() }}')).toEqual(true);
|
|
expect(evaluate('={{ "http://example.com/".isUrl() }}')).toEqual(true);
|
|
expect(evaluate('={{ "ftp://example.com/".isUrl() }}')).toEqual(true);
|
|
expect(evaluate('={{ "example.com".isUrl() }}')).toEqual(false);
|
|
expect(evaluate('={{ "www.example.com".isUrl() }}')).toEqual(false);
|
|
expect(evaluate('={{ "https://www.example.com/".isUrl() }}')).toEqual(true);
|
|
expect(evaluate('={{ "https://example.com/path".isUrl() }}')).toEqual(true);
|
|
expect(evaluate('={{ "https://example.com/path?query=1".isUrl() }}')).toEqual(true);
|
|
expect(evaluate('={{ "https://example.com/path#fragment".isUrl() }}')).toEqual(true);
|
|
expect(evaluate('={{ "https://example.com:8080".isUrl() }}')).toEqual(true);
|
|
expect(evaluate('={{ "https://example.com?query=1".isUrl() }}')).toEqual(true);
|
|
expect(evaluate('={{ "https://example.com#fragment".isUrl() }}')).toEqual(true);
|
|
expect(evaluate('={{ "example.com/path".isUrl() }}')).toEqual(false);
|
|
expect(evaluate('={{ "http:///".isUrl() }}')).toEqual(false);
|
|
expect(evaluate('={{ "https://".isUrl() }}')).toEqual(false);
|
|
expect(evaluate('={{ "example".isUrl() }}')).toEqual(false);
|
|
expect(evaluate('={{ "".isUrl() }}')).toEqual(false);
|
|
});
|
|
|
|
test('.isDomain should work on a string', () => {
|
|
expect(evaluate('={{ "example.com".isDomain() }}')).toEqual(true);
|
|
expect(evaluate('={{ "asdf".isDomain() }}')).toEqual(false);
|
|
expect(evaluate('={{ "https://example.com/".isDomain() }}')).toEqual(false);
|
|
expect(evaluate('={{ "www.example.com".isDomain() }}')).toEqual(true);
|
|
expect(evaluate('={{ "subdomain.example.com".isDomain() }}')).toEqual(true);
|
|
expect(evaluate('={{ "example.co.uk".isDomain() }}')).toEqual(true);
|
|
expect(evaluate('={{ "example".isDomain() }}')).toEqual(false);
|
|
expect(evaluate('={{ "example.".isDomain() }}')).toEqual(false);
|
|
expect(evaluate('={{ ".com".isDomain() }}')).toEqual(false);
|
|
expect(evaluate('={{ "example..com".isDomain() }}')).toEqual(false);
|
|
expect(evaluate('={{ "example_com".isDomain() }}')).toEqual(false);
|
|
expect(evaluate('={{ "example/com".isDomain() }}')).toEqual(false);
|
|
expect(evaluate('={{ "example com".isDomain() }}')).toEqual(false);
|
|
expect(evaluate('={{ "www.example..com".isDomain() }}')).toEqual(false);
|
|
expect(evaluate('={{ "123.com".isDomain() }}')).toEqual(true);
|
|
expect(evaluate('={{ "xn--80aswg.xn--p1ai".isDomain() }}')).toEqual(true); // Punycode domain
|
|
expect(evaluate('={{ "example.com:8080".isDomain() }}')).toEqual(true);
|
|
expect(evaluate('={{ "".isDomain() }}')).toEqual(false);
|
|
});
|
|
|
|
test('.toSnakeCase should work on a string', () => {
|
|
expect(evaluate('={{ "I am a test!".toSnakeCase() }}')).toEqual('i_am_a_test');
|
|
expect(evaluate('={{ "i_am_a_test".toSnakeCase() }}')).toEqual('i_am_a_test');
|
|
});
|
|
|
|
test('.toSentenceCase should work on a string', () => {
|
|
expect(
|
|
evaluate(
|
|
'={{ "i am a test! i have multiple types of Punctuation. or do i?".toSentenceCase() }}',
|
|
),
|
|
).toEqual('I am a test! I have multiple types of punctuation. Or do i?');
|
|
expect(evaluate('={{ "i am a test!".toSentenceCase() }}')).toEqual('I am a test!');
|
|
expect(evaluate('={{ "i am a test".toSentenceCase() }}')).toEqual('I am a test');
|
|
});
|
|
|
|
test('.toSentenceCase should not drop trailing text that has no letters', () => {
|
|
expect(evaluate('={{ "hello world. 123".toSentenceCase() }}')).toEqual('Hello world. 123');
|
|
expect(evaluate('={{ "end with punc. 42!".toSentenceCase() }}')).toEqual(
|
|
'End with punc. 42!',
|
|
);
|
|
expect(evaluate('={{ "growth is high. 50%".toSentenceCase() }}')).toEqual(
|
|
'Growth is high. 50%',
|
|
);
|
|
});
|
|
|
|
test('.toSentenceCase should return letter-free input unchanged', () => {
|
|
expect(evaluate('={{ "42".toSentenceCase() }}')).toEqual('42');
|
|
expect(evaluate('={{ "".toSentenceCase() }}')).toEqual('');
|
|
});
|
|
|
|
test('.extractUrl should work on a string', () => {
|
|
expect(
|
|
evaluate(
|
|
'={{ "I am a test with a url: https://example.net/ and I am a test with an email: test@example.org".extractUrl() }}',
|
|
),
|
|
).toEqual('https://example.net/');
|
|
expect(
|
|
evaluate(
|
|
'={{ "Check this out: https://subdomain.example.com:3000/path?q=1#hash".extractUrl() }}',
|
|
),
|
|
).toEqual('https://subdomain.example.com:3000/path?q=1#hash');
|
|
expect(evaluate('={{ "Invalid URL: http:///example.com".extractUrl() }}')).toEqual(undefined);
|
|
expect(
|
|
evaluate(
|
|
'={{ "Mixed content: https://www.example.com and http://www.example.org".extractUrl() }}',
|
|
),
|
|
).toEqual('https://www.example.com');
|
|
expect(
|
|
evaluate('={{ "Text without URL: This is just a simple text".extractUrl() }}'),
|
|
).toEqual(undefined);
|
|
expect(
|
|
evaluate('={{ "URL with Unicode: http://www.xn--80aswg.xn--j1amh".extractUrl() }}'),
|
|
).toEqual('http://www.xn--80aswg.xn--j1amh');
|
|
expect(
|
|
evaluate('={{ "Localhost URL: http://localhost:8080/test?x=1".extractUrl() }}'),
|
|
).toEqual('http://localhost:8080/test?x=1');
|
|
expect(
|
|
evaluate('={{ "IP URL: http://192.168.1.1:8000/path?q=value#frag".extractUrl() }}'),
|
|
).toEqual('http://192.168.1.1:8000/path?q=value#frag');
|
|
});
|
|
|
|
test.each([
|
|
['Docs: https://docs.n8n.io.', 'https://docs.n8n.io'],
|
|
['Visit https://example.com, then come back', 'https://example.com'],
|
|
['File at https://example.com/data.csv;', 'https://example.com/data.csv'],
|
|
['Search https://example.com/search?q=n8n!', 'https://example.com/search?q=n8n'],
|
|
['See (https://example.com) for details', 'https://example.com'],
|
|
['(see https://example.com/docs).', 'https://example.com/docs'],
|
|
['Wiki (https://example.com/C_(lang))', 'https://example.com/C_(lang)'],
|
|
['Go to <https://example.com/a> now', 'https://example.com/a'],
|
|
['Go to "https://example.com/a" now', 'https://example.com/a'],
|
|
])('.extractUrl should stop where the URL stops in %j', (text, url) => {
|
|
expect(evaluate('={{ $json.text.extractUrl() }}', [{ text }])).toEqual(url);
|
|
});
|
|
|
|
test.each([
|
|
'https://example.com/',
|
|
'https://example.com/a.b,c;d:e!f',
|
|
'https://example.com/?time=12:30',
|
|
'https://example.com/?q=:)&page=2',
|
|
'https://en.wikipedia.org/wiki/C_(programming_language)',
|
|
"https://en.wikipedia.org/wiki/Kids'_Choice_Awards",
|
|
])('.extractUrl should keep all characters of %j', (url) => {
|
|
expect(evaluate('={{ $json.text.extractUrl() }}', [{ text: `Go to ${url} now` }])).toEqual(
|
|
url,
|
|
);
|
|
});
|
|
|
|
test('.extractDomain should work on a string', () => {
|
|
expect(evaluate('={{ "test@example.org".extractDomain() }}')).toEqual('example.org');
|
|
expect(evaluate('={{ "https://example.org/".extractDomain() }}')).toEqual('example.org');
|
|
expect(evaluate('={{ "https://www.google.com".extractDomain() }}')).toEqual('www.google.com');
|
|
expect(evaluate('={{ "http://example.org".extractDomain() }}')).toEqual('example.org');
|
|
expect(evaluate('={{ "ftp://ftp.example.com".extractDomain() }}')).toEqual('ftp.example.com');
|
|
expect(evaluate('={{ "google.com".extractDomain() }}')).toEqual('google.com');
|
|
expect(evaluate('={{ "www.example.net".extractDomain() }}')).toEqual('www.example.net');
|
|
expect(evaluate('={{ "//example.com".extractDomain() }}')).toEqual('example.com');
|
|
expect(evaluate('={{ "mailto:john.doe@example.com".extractDomain() }}')).toEqual(
|
|
'example.com',
|
|
);
|
|
expect(evaluate('={{ "tel:+1-555-123-4567".extractDomain() }}')).toEqual(undefined);
|
|
expect(evaluate('={{ "jane.doe@example.org".extractDomain() }}')).toEqual('example.org');
|
|
expect(evaluate('={{ "name+tag@example.com".extractDomain() }}')).toEqual('example.com');
|
|
expect(evaluate('={{ "first.last@example.co.uk".extractDomain() }}')).toEqual(
|
|
'example.co.uk',
|
|
);
|
|
expect(evaluate('={{ "user@subdomain.example.com".extractDomain() }}')).toEqual(
|
|
'subdomain.example.com',
|
|
);
|
|
expect(evaluate('={{ "www.example.net?test=1213".extractDomain() }}')).toEqual(
|
|
'www.example.net',
|
|
);
|
|
expect(evaluate('={{ "www.example.net?test".extractDomain() }}')).toEqual('www.example.net');
|
|
expect(evaluate('={{ "www.example.net#tesdt123".extractDomain() }}')).toEqual(
|
|
'www.example.net',
|
|
);
|
|
expect(evaluate('={{ "https://www.example.net?test=1213".extractDomain() }}')).toEqual(
|
|
'www.example.net',
|
|
);
|
|
expect(evaluate('={{ "https://www.example.net?test".extractDomain() }}')).toEqual(
|
|
'www.example.net',
|
|
);
|
|
expect(evaluate('={{ "https://www.example.net#tesdt123".extractDomain() }}')).toEqual(
|
|
'www.example.net',
|
|
);
|
|
expect(evaluate('={{ "https://192.168.1.1".extractDomain() }}')).toEqual('192.168.1.1');
|
|
expect(evaluate('={{ "http://www.xn--80aswg.xn--j1amh".extractDomain() }}')).toEqual(
|
|
'www.xn--80aswg.xn--j1amh',
|
|
);
|
|
expect(evaluate('={{ "https://localhost".extractDomain() }}')).toEqual('localhost');
|
|
expect(evaluate('={{ "https://localhost?test=123".extractDomain() }}')).toEqual('localhost');
|
|
expect(evaluate('={{ "https://www.example_with_underscore.com".extractDomain() }}')).toEqual(
|
|
'www.example_with_underscore.com',
|
|
);
|
|
expect(evaluate('={{ "https://www.example.com:8080".extractDomain() }}')).toEqual(
|
|
'www.example.com',
|
|
);
|
|
expect(evaluate('={{ "https://example.space".extractDomain() }}')).toEqual('example.space');
|
|
});
|
|
|
|
test('.extractEmail should work on a string', () => {
|
|
expect(
|
|
evaluate(
|
|
'={{ "I am a test with a url: https://example.net/ and I am a test with an email: test@example.org".extractEmail() }}',
|
|
),
|
|
).toEqual('test@example.org');
|
|
});
|
|
|
|
test('.isEmail should work on a string', () => {
|
|
expect(evaluate('={{ "test@example.com".isEmail() }}')).toEqual(true);
|
|
expect(evaluate('={{ "aaaaaaaa".isEmail() }}')).toEqual(false);
|
|
expect(evaluate('={{ "test @ n8n".isEmail() }}')).toEqual(false);
|
|
});
|
|
|
|
test.each([
|
|
['test@example.com', true],
|
|
['first.last@sub.example.org', true],
|
|
['name+tag@example.com', true],
|
|
['john@[192.168.1.1]', true],
|
|
['user@example.xn--p1ai', true],
|
|
['user@example.xn--vermgensberater-ctb', true],
|
|
['USER@EXAMPLE.XN--P1AI', true],
|
|
['"john"@example.com', true],
|
|
['test@example.com!', false],
|
|
['test@example.com;', false],
|
|
['test@example.com/extra', false],
|
|
['<test@example.com>', false],
|
|
['x,me@example.com', false],
|
|
['.user@example.com', false],
|
|
['a..b@example.com', false],
|
|
['a@b.com,c@d.com', false],
|
|
['"a"@example.com,"b"@other.org', false],
|
|
['"a\nb"@example.com', false],
|
|
['mailto:test@example.com', false],
|
|
['https://medium.com/@jane.doe', false],
|
|
['https://example.com/contact?email=a@b.com', false],
|
|
["It's me@example.com", false],
|
|
['test@example.com\n', true],
|
|
['test@example.com\r\n', true],
|
|
['\ttest@example.com', true],
|
|
[' test@example.com', false],
|
|
['test@example.com ', false],
|
|
['x\ttest@example.com', false],
|
|
['a@b.com\nc@d.com', false],
|
|
['"john doe"@example.com', false],
|
|
])('.isEmail should check the whole string: %j', (value, expected) => {
|
|
expect(evaluate('={{ $json.value.isEmail() }}', [{ value }])).toEqual(expected);
|
|
});
|
|
|
|
test.each([
|
|
['https://medium.com/@jane.doe', 'medium.com'],
|
|
['https://example.com/contact?email=a@b.com', 'example.com'],
|
|
['MAILTO:john.doe@example.com', 'example.com'],
|
|
['mailto:john.doe@example.com?subject=Hello', 'example.com'],
|
|
['mailto:a@b.com,c@d.com', 'b.com'],
|
|
['mailto:example.com', 'example.com'],
|
|
['<test@example.com>', 'example.com'],
|
|
['test@example.com;', 'example.com'],
|
|
['test@example.com\n', 'example.com'],
|
|
['https://user@example.com/', 'example.com'],
|
|
['https://user@localhost/path?email=a@other.org', 'localhost'],
|
|
['https://user@example.xn--p1ai/', 'example.xn--p1ai'],
|
|
['http://a.example\\@b.example/', undefined],
|
|
['ftp://john@corp.com@files.example.org/', 'files.example.org'],
|
|
['example.com/a@b.com', 'b.com'],
|
|
['user@example.xn--p1ai', 'example.xn--p1ai'],
|
|
['Name <test@example.com>', undefined],
|
|
])('.extractDomain should read the domain of %j', (value, expected) => {
|
|
expect(evaluate('={{ $json.value.extractDomain() }}', [{ value }])).toEqual(expected);
|
|
});
|
|
|
|
test.each([
|
|
['Contact test@example.com; thanks', 'test@example.com'],
|
|
['<test@example.com>', 'test@example.com'],
|
|
['mailto:test@example.com', 'test@example.com'],
|
|
['user@example.xn--p1ai', 'user@example.xn--p1ai'],
|
|
['user@example.xn--vermgensberater-ctb', 'user@example.xn--vermgensberater-ctb'],
|
|
['"a"@example.com,"b"@other.org', '"a"@example.com'],
|
|
])('.extractEmail should find the address inside %j', (value, expected) => {
|
|
expect(evaluate('={{ $json.value.extractEmail() }}', [{ value }])).toEqual(expected);
|
|
});
|
|
|
|
test('.toDateTime should work on a variety of formats', () => {
|
|
expect(
|
|
asDateTime(evaluate('={{ "Wed, 21 Oct 2015 07:28:00 GMT".toDateTime() }}')),
|
|
).toBeInstanceOf(DateTime);
|
|
expect(asDateTime(evaluate('={{ "2008-11-11".toDateTime() }}'))).toBeInstanceOf(DateTime);
|
|
expect(asDateTime(evaluate('={{ "1-Feb-2024".toDateTime() }}'))).toBeInstanceOf(DateTime);
|
|
expect(asDateTime(evaluate('={{ "1713976144063".toDateTime("ms") }}'))).toBeInstanceOf(
|
|
DateTime,
|
|
);
|
|
expect(asDateTime(evaluate('={{ "31-01-2024".toDateTime("dd-MM-yyyy") }}'))).toBeInstanceOf(
|
|
DateTime,
|
|
);
|
|
|
|
vi.useFakeTimers({ now: new Date() });
|
|
expect(() => evaluate('={{ "hi".toDateTime() }}')).toThrow(ExpressionExtensionError);
|
|
expect(() => evaluate('={{ "hi".toDateTime() }}')).toThrow(
|
|
'cannot convert to Luxon DateTime',
|
|
);
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
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.each([
|
|
['https://de.wikipedia.org/wiki/Käse', '/wiki/K%C3%A4se'],
|
|
['https://example.com/my report.pdf', '/my%20report.pdf'],
|
|
['https://example.com/a<b>c', '/a%3Cb%3Ec'],
|
|
['https://example.com/v1/../v2/users', '/v2/users'],
|
|
['https://example.com/a/./b', '/a/b'],
|
|
['https://example.com/a/b/..', '/a/'],
|
|
['https://example.com/a%2fb', '/a%2fb'],
|
|
['https://example.com/a+b;c=d', '/a+b;c=d'],
|
|
['http:example.com/path', '/path'],
|
|
['mailto:me@example.com', 'me@example.com'],
|
|
['https://exa mple.com/path', undefined],
|
|
['https://example.com:port/path', undefined],
|
|
])('.extractUrlPath should give the path of `new URL()` for %j', (url, path) => {
|
|
expect(evaluate('={{ $json.url.extractUrlPath() }}', [{ url }])).toEqual(path);
|
|
});
|
|
|
|
test('.extractUrlPath should replace a lone surrogate in the path', () => {
|
|
expect(
|
|
evaluate('={{ ("https://example.com/" + "😀".substring(0, 1)).extractUrlPath() }}'),
|
|
).toEqual('/%EF%BF%BD');
|
|
});
|
|
|
|
test('.parseJson should work on a string', () => {
|
|
expect(evaluate('={{ \'{"test1":1,"test2":"2"}\'.parseJson() }}')).toEqual({
|
|
test1: 1,
|
|
test2: '2',
|
|
});
|
|
});
|
|
|
|
test('.parseJson should throw on invalid JSON', () => {
|
|
expect(() => evaluate("={{ \"{'test1':1,'test2':'2'}\".parseJson() }}")).toThrowError(
|
|
"Parsing failed. Check you're using double quotes",
|
|
);
|
|
expect(() => evaluate('={{ "No JSON here".parseJson() }}')).toThrowError('Parsing failed');
|
|
});
|
|
|
|
test('.toJsonString should work on a string', () => {
|
|
expect(evaluate('={{ "test".toJsonString() }}')).toEqual(JSON.stringify('test'));
|
|
expect(evaluate('={{ "The \\"best\\" colours: red\\nbrown".toJsonString() }}')).toEqual(
|
|
JSON.stringify('The "best" colours: red\nbrown'),
|
|
);
|
|
expect(evaluate('={{ "".toJsonString() }}')).toEqual(JSON.stringify(''));
|
|
});
|
|
|
|
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');
|
|
});
|
|
|
|
test.each([
|
|
["'😀'.substring(0, 1)", '77+9'],
|
|
["'𐀀'[0]", '77+9'],
|
|
["'é😀'", 'w6nwn5iA'],
|
|
])(
|
|
'.base64Encode should encode %s as UTF-8 with U+FFFD for lone surrogates',
|
|
(value, expected) => {
|
|
expect(evaluate(`={{ ${value}.base64Encode() }}`)).toBe(expected);
|
|
expect(evaluate(`={{ ${value}.hash('base64') }}`)).toBe(expected);
|
|
},
|
|
);
|
|
|
|
test.each([
|
|
['/w==', '\uFFFD'],
|
|
['wA==', '\uFFFD'],
|
|
['7aCA', '\uFFFD\uFFFD\uFFFD'],
|
|
['w6nwn5iA', 'é😀'],
|
|
['a@b.com', 'i\uFFFD('],
|
|
['a-b_c', 'k\uFFFD\uFFFD'],
|
|
['aGVs bG8=', 'hello'],
|
|
['!!!', ''],
|
|
])(
|
|
'.base64Decode should decode %s, skip non-base64 characters and give U+FFFD for invalid UTF-8',
|
|
(value, expected) => {
|
|
expect(evaluate(`={{ "${value}".base64Decode() }}`)).toBe(expected);
|
|
},
|
|
);
|
|
});
|
|
});
|