mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 18:12:04 +00:00
feat(editor): Completions for extensions in expression editor (#5130)
* 🔥 Remove test extensions * 🚧 Add test description * 📘 Expand types * ⚡ Export extensions * ⚡ Export collection * ⚡ Mark all proxies * ✏️ Rename for clarity * ⚡ Export from barrel * ✨ Create datatype completions * ⚡ Mount datatype completions * 🧪 Adjust tests * ⚡ Add `path` prop * 🔥 Remove `()` from completion labels * ⚡ Filter out completions for pseudo-proxies * 🐛 Fix method error * ⚡ Add metrics * ✏️ Improve naming * ✨ Start completion on empty resolvable * ✨ Implement completion previews * ⚡ Break out completion manager * ⚡ Implement in expression editor modal * ✏️ Improve naming * ⚡ Filter out irrelevant completions * ✨ Add preview hint * ✏️ Improve comments * 🎨 Style preview hint * ⚡ Expand `hasNoParams` * ⚡ Add spacing for readability * ⚡ Add error codes * ✏️ Add comment * 🐛 Fix Esc behavior * ⚡ Parse Unicode * ⚡ Throw on invalid `DateTime` * ⚡ Fix second root completion detection * ⚡ Switch message at completable prefix position * 🐛 Fix function names for non-dev build * 🐛 Fix `json` handling * 🔥 Comment out previews * ♻️ Apply feedback * 🔥 Remove extensions * 🚚 Rename extensions * ⚡ Adjust some implementations * 🔥 Remove dummy extensions * 🐛 Fix object regex * ♻️ Apply feedback * ✏️ Fix typos * ✏️ Add `fn is not a function` message * 🔥 Remove check * ✨ Add `isNotEmpty` for objects * 🚚 Rename `global` to `alpha` * 🔥 Remove `encrypt` * ⚡ Restore `is not a function` error * ⚡ Support `week` on `extract()` * 🧪 Fix tests * ⚡ Add validation to some string extensions * ⚡ Validate number arrays in some extensions * 🧪 Fix tests * ✏️ Improve error message * ⏪ Revert extensions framework changes * 🧹 Previews cleanup * ⚡ Condense blank completions * ⚡ Refactor dollar completions * ⚡ Refactor non-dollar completions * ⚡ Refactor Luxon completions * ⚡ Refactor datatype completions * ⚡ Use `DATETIMEUNIT_MAP` * ✏️ Update test description * ⏪ Revert "Use `DATETIMEUNIT_MAP`" This reverts commit 472a77df5cd789905d162f3c3db02ac767b89b4e. * 🧪 Add tests * ♻️ Restore generic extensions * 🔥 Remove logs * 🧪 Expand tests * ✨ Add `Math` completions * ✏️ List breaking change * ⚡ Add doc tooltips * 🐛 Fix node selector regex * 🐛 Fix `context` resolution * 🐛 Allow dollar completions in args * ⚡ Make numeric array methods context-dependent * 📝 Adjust docs * 🐛 Fix selector ref * ⚡ Surface error for valid URL * 🐛 Disallow whitespace in `isEmail` check * 🧪 Fix test for `isUrl` * ⚡ Add comma validator in `toFloat` * ⚡ Add validation to `$jmespath()` * ⏪ Revert valid URL error * ⚡ Adjust `$jmespath()` validation * 🧪 Adjust `isUrl` test * ⚡ Remove `{}` and `[]` from compact * ✏️ Update docs * 🚚 Rename `stripTags` to `removeTags` * ⚡ Do not inject whitespace inside resolvable * ⚡ Make completions aware of `()` * ✏️ Add note * ⚡ Update sorting * ⚡ Hide active node name from node selector * 🔥 Remove `length()` and its aliases * ⚡ Validate non-zero for `chunk` * ✏️ Reword all error messages * 🐛 Fix `$now` and `$today` * ⚡ Simplify with `stripExcessParens` * ⚡ Fold luxon into datatype * 🧪 Clean up tests * 🔥 Remove tests for removed methods * 👕 Fix type * ⬆️ Upgrade lang pack * ⏪ Undo change to `vitest` command * 🔥 Remove unused method * ⚡ Separate `return` line * ✏️ Improve description * 🧪 Expand tests for initial-only completions * 🧪 Add bracket-aware completions * ⚡ Make check for `all()` stricter * ✏️ Adjust explanatory comments * 🔥 Remove unneded copy * 🔥 Remove outdated comment * ⚡ Make naming consistent * ✏️ Update comments * ⚡ Improve URL scheme check * ✏️ Add comment * 🚚 Move extension * ✏️ Update `BREAKING-CHANGES.md` * ✏️ Update upcoming version * ✏️ Fix grammar * ✏️ Shorten message * 🐛 Fix `Esc` behavior * 🐛 Fix `isNumeric` * ✨ Support native methods * 🧪 Skip Pinia tests * ✏️ Shorten description * 🔥 Remove outdated comment * 🧪 Unskip Pinia tests * ✏️ Add comments * 🧪 Expand tests to natives * ✏️ Add clarifying comments * ⚡ Use `setTimeout` to make telemetry non-blocking * 🐛 Account for no active node in cred modal * ✨ Resolve without workflow * 🔥 Remove `Esc` handling on NDV * ⚡ Use `isDateTime` * 🚚 Move `unique` to next phase This array extension takes optional args. * ⚡ Merge export * 🧪 Fix tests * ⏪ Restore check * ✏️ Make breaking change description more accurate * 🧪 Fix e2e tests
This commit is contained in:
@@ -61,6 +61,7 @@
|
||||
"lodash.set": "^4.3.2",
|
||||
"luxon": "^3.1.0",
|
||||
"recast": "^0.21.5",
|
||||
"title-case": "^3.0.3",
|
||||
"transliteration": "^2.3.5",
|
||||
"xml2js": "^0.4.23"
|
||||
}
|
||||
|
||||
@@ -34,6 +34,10 @@ tmpl.tmpl.errorHandler = (error: Error) => {
|
||||
if (error.context.failExecution) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
if (typeof process === 'undefined' && error.clientOnly) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -44,6 +48,10 @@ export class Expression {
|
||||
this.workflow = workflow;
|
||||
}
|
||||
|
||||
static resolveWithoutWorkflow(expression: string) {
|
||||
return tmpl.tmpl(expression, {});
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an object to a string in a way to make it clear that
|
||||
* the value comes from an object
|
||||
@@ -51,7 +59,16 @@ export class Expression {
|
||||
*/
|
||||
convertObjectValueToString(value: object): string {
|
||||
const typeName = Array.isArray(value) ? 'Array' : 'Object';
|
||||
return `[${typeName}: ${JSON.stringify(value)}]`;
|
||||
|
||||
if (DateTime.isDateTime(value) && value.invalidReason !== null) {
|
||||
throw new Error('invalid DateTime');
|
||||
}
|
||||
|
||||
const result = JSON.stringify(value)
|
||||
.replace(/,"/g, ', "') // spacing for
|
||||
.replace(/":/g, '": '); // readability
|
||||
|
||||
return `[${typeName}: ${result}]`;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -268,7 +285,11 @@ export class Expression {
|
||||
const returnValue = this.renderExpression(extendedExpression, data);
|
||||
if (typeof returnValue === 'function') {
|
||||
if (returnValue.name === '$') throw new Error('invalid syntax');
|
||||
throw new Error('This is a function. Please add ()');
|
||||
|
||||
if (returnValue.name === 'DateTime')
|
||||
throw new Error('this is a DateTime, please access its methods');
|
||||
|
||||
throw new Error('this is a function, please add ()');
|
||||
} else if (typeof returnValue === 'string') {
|
||||
return returnValue;
|
||||
} else if (returnValue !== null && typeof returnValue === 'object') {
|
||||
@@ -293,6 +314,10 @@ export class Expression {
|
||||
if (error.context.failExecution) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
if (typeof process === 'undefined' && error.clientOnly) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// Syntax errors resolve to `Error` on the frontend and `null` on the backend.
|
||||
@@ -305,6 +330,19 @@ export class Expression {
|
||||
) {
|
||||
throw new Error('invalid syntax');
|
||||
}
|
||||
|
||||
if (
|
||||
typeof process === 'undefined' &&
|
||||
error instanceof Error &&
|
||||
error.name === 'TypeError' &&
|
||||
error.message.endsWith('is not a function')
|
||||
) {
|
||||
const match = error.message.match(/(?<msg>[^.]+is not a function)/);
|
||||
|
||||
if (!match?.groups?.msg) return null;
|
||||
|
||||
throw new Error(match.groups.msg);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -325,7 +363,7 @@ export class Expression {
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
if (!output?.code) {
|
||||
throw new ExpressionExtensionError('Failed to extend syntax');
|
||||
throw new ExpressionExtensionError('invalid syntax');
|
||||
}
|
||||
|
||||
let text = output.code;
|
||||
|
||||
@@ -5,6 +5,8 @@ import { ExecutionBaseError } from './NodeErrors';
|
||||
* Class for instantiating an expression error
|
||||
*/
|
||||
export class ExpressionError extends ExecutionBaseError {
|
||||
clientOnly = false;
|
||||
|
||||
constructor(
|
||||
message: string,
|
||||
options?: {
|
||||
@@ -13,6 +15,7 @@ export class ExpressionError extends ExecutionBaseError {
|
||||
description?: string;
|
||||
descriptionTemplate?: string;
|
||||
failExecution?: boolean;
|
||||
clientOnly?: boolean; // whether to throw error only on frontend
|
||||
functionality?: 'pairedItem';
|
||||
itemIndex?: number;
|
||||
messageTemplate?: string;
|
||||
@@ -28,6 +31,10 @@ export class ExpressionError extends ExecutionBaseError {
|
||||
this.description = options.description;
|
||||
}
|
||||
|
||||
if (options?.clientOnly) {
|
||||
this.clientOnly = options.clientOnly;
|
||||
}
|
||||
|
||||
this.context.failExecution = !!options?.failExecution;
|
||||
|
||||
const allowedKeys = [
|
||||
|
||||
@@ -87,11 +87,11 @@ function first(value: unknown[]): unknown {
|
||||
return value[0];
|
||||
}
|
||||
|
||||
function isBlank(value: unknown[]): boolean {
|
||||
function isEmpty(value: unknown[]): boolean {
|
||||
return value.length === 0;
|
||||
}
|
||||
|
||||
function isPresent(value: unknown[]): boolean {
|
||||
function isNotEmpty(value: unknown[]): boolean {
|
||||
return value.length > 0;
|
||||
}
|
||||
|
||||
@@ -99,10 +99,6 @@ function last(value: unknown[]): unknown {
|
||||
return value[value.length - 1];
|
||||
}
|
||||
|
||||
function length(value: unknown[]): number {
|
||||
return Array.isArray(value) ? value.length : 0;
|
||||
}
|
||||
|
||||
function pluck(value: unknown[], extraArgs: unknown[]): unknown[] {
|
||||
if (!Array.isArray(extraArgs)) {
|
||||
throw new ExpressionError('arguments must be passed to pluck');
|
||||
@@ -121,7 +117,7 @@ function pluck(value: unknown[], extraArgs: unknown[]): unknown[] {
|
||||
}) as unknown[];
|
||||
}
|
||||
|
||||
function random(value: unknown[]): unknown {
|
||||
function randomItem(value: unknown[]): unknown {
|
||||
const len = value === undefined ? 0 : value.length;
|
||||
return len ? value[Math.floor(Math.random() * len)] : undefined;
|
||||
}
|
||||
@@ -149,7 +145,15 @@ function unique(value: unknown[], extraArgs: string[]): unknown[] {
|
||||
}, []);
|
||||
}
|
||||
|
||||
const ensureNumberArray = (arr: unknown[], { fnName }: { fnName: string }) => {
|
||||
if (arr.some((i) => typeof i !== 'number')) {
|
||||
throw new ExpressionExtensionError(`${fnName}(): all array elements must be numbers`);
|
||||
}
|
||||
};
|
||||
|
||||
function sum(value: unknown[]): number {
|
||||
ensureNumberArray(value, { fnName: 'sum' });
|
||||
|
||||
return value.reduce((p: number, c: unknown) => {
|
||||
if (typeof c === 'string') {
|
||||
return p + parseFloat(c);
|
||||
@@ -162,6 +166,8 @@ function sum(value: unknown[]): number {
|
||||
}
|
||||
|
||||
function min(value: unknown[]): number {
|
||||
ensureNumberArray(value, { fnName: 'min' });
|
||||
|
||||
return Math.min(
|
||||
...value.map((v) => {
|
||||
if (typeof v === 'string') {
|
||||
@@ -176,6 +182,8 @@ function min(value: unknown[]): number {
|
||||
}
|
||||
|
||||
function max(value: unknown[]): number {
|
||||
ensureNumberArray(value, { fnName: 'max' });
|
||||
|
||||
return Math.max(
|
||||
...value.map((v) => {
|
||||
if (typeof v === 'string') {
|
||||
@@ -190,6 +198,8 @@ function max(value: unknown[]): number {
|
||||
}
|
||||
|
||||
export function average(value: unknown[]) {
|
||||
ensureNumberArray(value, { fnName: 'average' });
|
||||
|
||||
// This would usually be NaN but I don't think users
|
||||
// will expect that
|
||||
if (value.length === 0) {
|
||||
@@ -200,7 +210,11 @@ export function average(value: unknown[]) {
|
||||
|
||||
function compact(value: unknown[]): unknown[] {
|
||||
return value
|
||||
.filter((v) => v !== null && v !== undefined)
|
||||
.filter((v) => {
|
||||
if (v && typeof v === 'object' && Object.keys(v).length === 0) return false;
|
||||
|
||||
return v !== null && v !== undefined && v !== 'nil' && v !== '';
|
||||
})
|
||||
.map((v) => {
|
||||
if (typeof v === 'object' && v !== null) {
|
||||
return oCompact(v);
|
||||
@@ -213,7 +227,7 @@ function smartJoin(value: unknown[], extraArgs: string[]): object {
|
||||
const [keyField, valueField] = extraArgs;
|
||||
if (!keyField || !valueField || typeof keyField !== 'string' || typeof valueField !== 'string') {
|
||||
throw new ExpressionExtensionError(
|
||||
'smartJoin requires 2 arguments: keyField and nameField. e.g. .smartJoin("name", "value")',
|
||||
'smartJoin(): expected two string args, e.g. .smartJoin("name", "value")',
|
||||
);
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-return
|
||||
@@ -229,8 +243,8 @@ function smartJoin(value: unknown[], extraArgs: string[]): object {
|
||||
|
||||
function chunk(value: unknown[], extraArgs: number[]) {
|
||||
const [chunkSize] = extraArgs;
|
||||
if (typeof chunkSize !== 'number') {
|
||||
throw new ExpressionExtensionError('chunk requires 1 parameter: chunkSize. e.g. .chunk(5)');
|
||||
if (typeof chunkSize !== 'number' || chunkSize === 0) {
|
||||
throw new ExpressionExtensionError('chunk(): expected non-zero numeric arg, e.g. .chunk(5)');
|
||||
}
|
||||
const chunks: unknown[][] = [];
|
||||
for (let i = 0; i < value.length; i += chunkSize) {
|
||||
@@ -241,30 +255,10 @@ function chunk(value: unknown[], extraArgs: number[]) {
|
||||
return chunks;
|
||||
}
|
||||
|
||||
function filter(value: unknown[], extraArgs: unknown[]): unknown[] {
|
||||
const [field, term] = extraArgs as [string | (() => void), unknown | string];
|
||||
if (typeof field !== 'string' && typeof field !== 'function') {
|
||||
throw new ExpressionExtensionError(
|
||||
'filter requires 1 or 2 arguments: (field and term), (term and [optional keepOrRemove "keep" or "remove" default "keep"] (for string arrays)), or function. e.g. .filter("type", "home") or .filter((i) => i.type === "home") or .filter("home", [optional keepOrRemove]) (for string arrays)',
|
||||
);
|
||||
}
|
||||
if (value.every((i) => typeof i === 'string') && typeof field === 'string') {
|
||||
return (value as string[]).filter((i) =>
|
||||
term === 'remove' ? !i.includes(field) : i.includes(field),
|
||||
);
|
||||
} else if (typeof field === 'string') {
|
||||
return value.filter(
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
|
||||
(v) => typeof v === 'object' && v !== null && field in v && (v as any)[field] === term,
|
||||
);
|
||||
}
|
||||
return value.filter(field);
|
||||
}
|
||||
|
||||
function renameKeys(value: unknown[], extraArgs: string[]): unknown[] {
|
||||
if (extraArgs.length === 0 || extraArgs.length % 2 !== 0) {
|
||||
throw new ExpressionExtensionError(
|
||||
'renameKeys requires an even amount of arguments: from1, to1 [, from2, to2, ...]. e.g. .renameKeys("name", "title")',
|
||||
'renameKeys(): expected an even amount of args: from1, to1 [, from2, to2, ...]. e.g. .renameKeys("name", "title")',
|
||||
);
|
||||
}
|
||||
return value.map((v) => {
|
||||
@@ -291,7 +285,7 @@ function merge(value: unknown[], extraArgs: unknown[][]): unknown[] {
|
||||
const [others] = extraArgs;
|
||||
if (!Array.isArray(others)) {
|
||||
throw new ExpressionExtensionError(
|
||||
'merge requires 1 argument that is an array. e.g. .merge([{ id: 1, otherValue: 3 }])',
|
||||
'merge(): expected array arg, e.g. .merge([{ id: 1, otherValue: 3 }])',
|
||||
);
|
||||
}
|
||||
const listLength = value.length > others.length ? value.length : others.length;
|
||||
@@ -314,9 +308,7 @@ function merge(value: unknown[], extraArgs: unknown[][]): unknown[] {
|
||||
function union(value: unknown[], extraArgs: unknown[][]): unknown[] {
|
||||
const [others] = extraArgs;
|
||||
if (!Array.isArray(others)) {
|
||||
throw new ExpressionExtensionError(
|
||||
'union requires 1 argument that is an array. e.g. .union([1, 2, 3, 4])',
|
||||
);
|
||||
throw new ExpressionExtensionError('union(): expected array arg, e.g. .union([1, 2, 3, 4])');
|
||||
}
|
||||
const newArr: unknown[] = Array.from(value);
|
||||
for (const v of others) {
|
||||
@@ -331,7 +323,7 @@ function difference(value: unknown[], extraArgs: unknown[][]): unknown[] {
|
||||
const [others] = extraArgs;
|
||||
if (!Array.isArray(others)) {
|
||||
throw new ExpressionExtensionError(
|
||||
'difference requires 1 argument that is an array. e.g. .difference([1, 2, 3, 4])',
|
||||
'difference(): expected array arg, e.g. .difference([1, 2, 3, 4])',
|
||||
);
|
||||
}
|
||||
const newArr: unknown[] = [];
|
||||
@@ -347,7 +339,7 @@ function intersection(value: unknown[], extraArgs: unknown[][]): unknown[] {
|
||||
const [others] = extraArgs;
|
||||
if (!Array.isArray(others)) {
|
||||
throw new ExpressionExtensionError(
|
||||
'difference requires 1 argument that is an array. e.g. .difference([1, 2, 3, 4])',
|
||||
'intersection(): expected array arg, e.g. .intersection([1, 2, 3, 4])',
|
||||
);
|
||||
}
|
||||
const newArr: unknown[] = [];
|
||||
@@ -364,27 +356,129 @@ function intersection(value: unknown[], extraArgs: unknown[][]): unknown[] {
|
||||
return unique(newArr, []);
|
||||
}
|
||||
|
||||
average.doc = {
|
||||
name: 'average',
|
||||
description: 'Returns the mean average of all values in the array',
|
||||
returnType: 'number',
|
||||
};
|
||||
|
||||
compact.doc = {
|
||||
name: 'compact',
|
||||
description: 'Removes all empty values from the array',
|
||||
returnType: 'array',
|
||||
};
|
||||
|
||||
isEmpty.doc = {
|
||||
name: 'isEmpty',
|
||||
description: 'Checks if the array doesn’t have any elements',
|
||||
returnType: 'boolean',
|
||||
};
|
||||
|
||||
isNotEmpty.doc = {
|
||||
name: 'isNotEmpty',
|
||||
description: 'Checks if the array has elements',
|
||||
returnType: 'boolean',
|
||||
};
|
||||
|
||||
first.doc = {
|
||||
name: 'first',
|
||||
description: 'Returns the first element of the array',
|
||||
returnType: 'array item',
|
||||
};
|
||||
|
||||
last.doc = {
|
||||
name: 'last',
|
||||
description: 'Returns the last element of the array',
|
||||
returnType: 'array item',
|
||||
};
|
||||
|
||||
max.doc = {
|
||||
name: 'max',
|
||||
description: 'Gets the maximum value from a number-only array',
|
||||
returnType: 'number',
|
||||
};
|
||||
|
||||
min.doc = {
|
||||
name: 'min',
|
||||
description: 'Gets the minimum value from a number-only array',
|
||||
returnType: 'number',
|
||||
};
|
||||
|
||||
randomItem.doc = {
|
||||
name: 'randomItem',
|
||||
description: 'Returns a random element from an array',
|
||||
returnType: 'number',
|
||||
};
|
||||
|
||||
sum.doc = {
|
||||
name: 'sum',
|
||||
description: 'Returns the total sum all the values in an array of parsable numbers',
|
||||
returnType: 'number',
|
||||
};
|
||||
|
||||
// @TODO_NEXT_PHASE: Surface extensions below which take args
|
||||
|
||||
chunk.doc = {
|
||||
name: 'chunk',
|
||||
returnType: 'array',
|
||||
};
|
||||
|
||||
difference.doc = {
|
||||
name: 'difference',
|
||||
returnType: 'array',
|
||||
};
|
||||
|
||||
intersection.doc = {
|
||||
name: 'intersection',
|
||||
returnType: 'array',
|
||||
};
|
||||
|
||||
merge.doc = {
|
||||
name: 'merge',
|
||||
returnType: 'array',
|
||||
};
|
||||
|
||||
pluck.doc = {
|
||||
name: 'pluck',
|
||||
returnType: 'array',
|
||||
};
|
||||
|
||||
renameKeys.doc = {
|
||||
name: 'renameKeys',
|
||||
returnType: 'array',
|
||||
};
|
||||
|
||||
smartJoin.doc = {
|
||||
name: 'smartJoin',
|
||||
returnType: 'array',
|
||||
};
|
||||
|
||||
union.doc = {
|
||||
name: 'union',
|
||||
returnType: 'array',
|
||||
};
|
||||
|
||||
unique.doc = {
|
||||
name: 'unique',
|
||||
returnType: 'array item',
|
||||
aliases: ['removeDuplicates'],
|
||||
};
|
||||
|
||||
export const arrayExtensions: ExtensionMap = {
|
||||
typeName: 'Array',
|
||||
functions: {
|
||||
count: length,
|
||||
duplicates: unique,
|
||||
filter,
|
||||
removeDuplicates: unique,
|
||||
first,
|
||||
last,
|
||||
length,
|
||||
pluck,
|
||||
unique,
|
||||
random,
|
||||
randomItem: random,
|
||||
remove: unique,
|
||||
size: length,
|
||||
randomItem,
|
||||
sum,
|
||||
min,
|
||||
max,
|
||||
average,
|
||||
isPresent,
|
||||
isBlank,
|
||||
isNotEmpty,
|
||||
isEmpty,
|
||||
compact,
|
||||
smartJoin,
|
||||
chunk,
|
||||
|
||||
@@ -1,14 +1,7 @@
|
||||
/* eslint-disable @typescript-eslint/unbound-method */
|
||||
/* eslint-disable @typescript-eslint/explicit-member-accessibility */
|
||||
import type {
|
||||
DateTimeFormatOptions,
|
||||
DateTimeUnit,
|
||||
Duration,
|
||||
DurationLike,
|
||||
DurationObjectUnits,
|
||||
LocaleOptions,
|
||||
} from 'luxon';
|
||||
import { DateTime } from 'luxon';
|
||||
import type { DateTimeUnit, DurationLike, DurationObjectUnits, LocaleOptions } from 'luxon';
|
||||
import type { ExtensionMap } from './Extensions';
|
||||
|
||||
type DurationUnit =
|
||||
@@ -23,6 +16,7 @@ type DurationUnit =
|
||||
| 'years';
|
||||
type DatePart =
|
||||
| 'day'
|
||||
| 'week'
|
||||
| 'month'
|
||||
| 'year'
|
||||
| 'hour'
|
||||
@@ -103,7 +97,7 @@ function endOfMonth(date: Date | DateTime): Date {
|
||||
}
|
||||
|
||||
function extract(inputDate: Date | DateTime, extraArgs: DatePart[]): number | Date {
|
||||
const [part] = extraArgs;
|
||||
let [part] = extraArgs;
|
||||
let date = inputDate;
|
||||
if (isDateTime(date)) {
|
||||
date = date.toJSDate();
|
||||
@@ -117,6 +111,10 @@ function extract(inputDate: Date | DateTime, extraArgs: DatePart[]): number | Da
|
||||
return Math.floor(diff / (1000 * 60 * 60 * 24));
|
||||
}
|
||||
|
||||
if (part === 'week') {
|
||||
part = 'weekNumber';
|
||||
}
|
||||
|
||||
return DateTime.fromJSDate(date).get((DATETIMEUNIT_MAP[part] as keyof DateTime) || part);
|
||||
}
|
||||
|
||||
@@ -194,73 +192,60 @@ function plus(date: Date | DateTime, extraArgs: unknown[]): Date | DateTime {
|
||||
return DateTime.fromJSDate(date).plus(generateDurationObject(durationValue, unit)).toJSDate();
|
||||
}
|
||||
|
||||
function toLocaleString(date: Date | DateTime, extraArgs: unknown[]): string {
|
||||
const [locale, dateFormat = { timeStyle: 'short', dateStyle: 'short' }] = extraArgs as [
|
||||
string | undefined,
|
||||
DateTimeFormatOptions,
|
||||
];
|
||||
endOfMonth.doc = {
|
||||
name: 'endOfMonth',
|
||||
returnType: 'Date',
|
||||
description: 'Transforms a date to the last possible moment that lies within the month',
|
||||
};
|
||||
|
||||
if (isDateTime(date)) {
|
||||
return date.toLocaleString(dateFormat, { locale });
|
||||
}
|
||||
return DateTime.fromJSDate(date).toLocaleString(dateFormat, { locale });
|
||||
}
|
||||
isDst.doc = {
|
||||
name: 'isDst',
|
||||
returnType: 'boolean',
|
||||
description: 'Checks if a Date is within Daylight Savings Time',
|
||||
};
|
||||
|
||||
function toTimeFromNow(date: Date | DateTime): string {
|
||||
let diffObj: Duration;
|
||||
if (isDateTime(date)) {
|
||||
diffObj = date.diffNow();
|
||||
} else {
|
||||
diffObj = DateTime.fromJSDate(date).diffNow();
|
||||
}
|
||||
isWeekend.doc = {
|
||||
name: 'isWeekend',
|
||||
returnType: 'boolean',
|
||||
description: 'Checks if the Date falls on a Saturday or Sunday',
|
||||
};
|
||||
|
||||
const as = (unit: DurationUnit) => {
|
||||
return Math.round(Math.abs(diffObj.as(unit)));
|
||||
};
|
||||
// @TODO_NEXT_PHASE: Surface extensions below which take args
|
||||
|
||||
if (as('years')) {
|
||||
return `${as('years')} years ago`;
|
||||
}
|
||||
if (as('months')) {
|
||||
return `${as('months')} months ago`;
|
||||
}
|
||||
if (as('weeks')) {
|
||||
return `${as('weeks')} weeks ago`;
|
||||
}
|
||||
if (as('days')) {
|
||||
return `${as('days')} days ago`;
|
||||
}
|
||||
if (as('hours')) {
|
||||
return `${as('hours')} hours ago`;
|
||||
}
|
||||
if (as('minutes')) {
|
||||
return `${as('minutes')} minutes ago`;
|
||||
}
|
||||
if (as('seconds') && as('seconds') > 10) {
|
||||
return `${as('seconds')} seconds ago`;
|
||||
}
|
||||
return 'just now';
|
||||
}
|
||||
beginningOf.doc = {
|
||||
name: 'beginningOf',
|
||||
returnType: 'Date',
|
||||
};
|
||||
|
||||
function timeTo(date: Date | DateTime, extraArgs: unknown[]): Duration {
|
||||
const [diff = new Date().toISOString(), unit = 'seconds'] = extraArgs as [string, DurationUnit];
|
||||
const diffDate = new Date(diff);
|
||||
if (isDateTime(date)) {
|
||||
return date.diff(DateTime.fromJSDate(diffDate), DURATION_MAP[unit] || unit);
|
||||
}
|
||||
return DateTime.fromJSDate(date).diff(DateTime.fromJSDate(diffDate), DURATION_MAP[unit] || unit);
|
||||
}
|
||||
extract.doc = {
|
||||
name: 'extract',
|
||||
returnType: 'number',
|
||||
};
|
||||
|
||||
function toDate(date: Date | DateTime) {
|
||||
if (isDateTime(date)) {
|
||||
return date.set({ hour: 0, minute: 0, second: 0, millisecond: 0 }).toJSDate();
|
||||
}
|
||||
let datetime = DateTime.fromJSDate(date);
|
||||
if (date.getTimezoneOffset() === 0) {
|
||||
datetime = datetime.setZone('UTC');
|
||||
}
|
||||
return datetime.set({ hour: 0, minute: 0, second: 0, millisecond: 0 }).toJSDate();
|
||||
}
|
||||
format.doc = {
|
||||
name: 'format',
|
||||
returnType: '(?)',
|
||||
};
|
||||
|
||||
isBetween.doc = {
|
||||
name: 'isBetween',
|
||||
returnType: 'boolean',
|
||||
};
|
||||
|
||||
isInLast.doc = {
|
||||
name: 'isInLast',
|
||||
returnType: 'boolean',
|
||||
};
|
||||
|
||||
minus.doc = {
|
||||
name: 'minus',
|
||||
returnType: 'Date',
|
||||
};
|
||||
|
||||
plus.doc = {
|
||||
name: 'plus',
|
||||
returnType: 'Date',
|
||||
};
|
||||
|
||||
export const dateExtensions: ExtensionMap = {
|
||||
typeName: 'Date',
|
||||
@@ -274,10 +259,6 @@ export const dateExtensions: ExtensionMap = {
|
||||
isWeekend,
|
||||
minus,
|
||||
plus,
|
||||
toTimeFromNow,
|
||||
timeTo,
|
||||
format,
|
||||
toLocaleString,
|
||||
toDate,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -11,15 +11,15 @@ import { objectExtensions } from './ObjectExtensions';
|
||||
|
||||
const EXPRESSION_EXTENDER = 'extend';
|
||||
|
||||
function isBlank(value: unknown) {
|
||||
function isEmpty(value: unknown) {
|
||||
return value === null || value === undefined || !value;
|
||||
}
|
||||
|
||||
function isPresent(value: unknown) {
|
||||
return !isBlank(value);
|
||||
function isNotEmpty(value: unknown) {
|
||||
return !isEmpty(value);
|
||||
}
|
||||
|
||||
const EXTENSION_OBJECTS = [
|
||||
export const EXTENSION_OBJECTS = [
|
||||
arrayExtensions,
|
||||
dateExtensions,
|
||||
numberExtensions,
|
||||
@@ -29,8 +29,8 @@ const EXTENSION_OBJECTS = [
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
const genericExtensions: Record<string, Function> = {
|
||||
isBlank,
|
||||
isPresent,
|
||||
isEmpty,
|
||||
isNotEmpty,
|
||||
};
|
||||
|
||||
const EXPRESSION_EXTENSION_METHODS = Array.from(
|
||||
|
||||
@@ -1,5 +1,13 @@
|
||||
export interface ExtensionMap {
|
||||
typeName: string;
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
functions: Record<string, Function>;
|
||||
functions: Record<string, Function & { doc?: DocMetadata }>;
|
||||
}
|
||||
|
||||
export type DocMetadata = {
|
||||
name: string;
|
||||
returnType: string;
|
||||
description?: string;
|
||||
aliases?: string[];
|
||||
args?: unknown[];
|
||||
};
|
||||
|
||||
@@ -13,26 +13,6 @@ function format(value: number, extraArgs: unknown[]): string {
|
||||
return new Intl.NumberFormat(locales, config).format(value);
|
||||
}
|
||||
|
||||
function isBlank(value: number): boolean {
|
||||
return typeof value !== 'number';
|
||||
}
|
||||
|
||||
function isPresent(value: number): boolean {
|
||||
return !isBlank(value);
|
||||
}
|
||||
|
||||
function random(value: number): number {
|
||||
return Math.floor(Math.random() * value);
|
||||
}
|
||||
|
||||
function isTrue(value: number) {
|
||||
return value === 1;
|
||||
}
|
||||
|
||||
function isFalse(value: number) {
|
||||
return value === 0;
|
||||
}
|
||||
|
||||
function isEven(value: number) {
|
||||
return value % 2 === 0;
|
||||
}
|
||||
@@ -54,19 +34,49 @@ function round(value: number, extraArgs: number[]) {
|
||||
return +value.toFixed(decimalPlaces);
|
||||
}
|
||||
|
||||
ceil.doc = {
|
||||
name: 'ceil',
|
||||
description: 'Rounds up a number to a whole number',
|
||||
returnType: 'number',
|
||||
};
|
||||
|
||||
floor.doc = {
|
||||
name: 'floor',
|
||||
description: 'Rounds down a number to a whole number',
|
||||
returnType: 'number',
|
||||
};
|
||||
|
||||
isEven.doc = {
|
||||
name: 'isEven',
|
||||
description: 'Returns true if the number is even. Only works on whole numbers.',
|
||||
returnType: 'boolean',
|
||||
};
|
||||
|
||||
isOdd.doc = {
|
||||
name: 'isOdd',
|
||||
description: 'Returns true if the number is odd. Only works on whole numbers.',
|
||||
returnType: 'boolean',
|
||||
};
|
||||
|
||||
// @TODO_NEXT_PHASE: Surface extensions below which take args
|
||||
|
||||
format.doc = {
|
||||
name: 'format',
|
||||
returnType: 'string',
|
||||
};
|
||||
|
||||
round.doc = {
|
||||
name: 'round',
|
||||
returnType: 'number',
|
||||
};
|
||||
|
||||
export const numberExtensions: ExtensionMap = {
|
||||
typeName: 'Number',
|
||||
functions: {
|
||||
ceil,
|
||||
floor,
|
||||
format,
|
||||
random,
|
||||
round,
|
||||
isBlank,
|
||||
isPresent,
|
||||
isTrue,
|
||||
isNotTrue: isFalse,
|
||||
isFalse,
|
||||
isEven,
|
||||
isOdd,
|
||||
},
|
||||
|
||||
@@ -4,7 +4,7 @@ import type { ExtensionMap } from './Extensions';
|
||||
export function merge(value: object, extraArgs: unknown[]): unknown {
|
||||
const [other] = extraArgs;
|
||||
if (typeof other !== 'object' || !other) {
|
||||
throw new ExpressionExtensionError('argument of merge must be an object');
|
||||
throw new ExpressionExtensionError('merge(): expected object arg');
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const newObject: any = { ...value };
|
||||
@@ -21,6 +21,10 @@ function isEmpty(value: object): boolean {
|
||||
return Object.keys(value).length === 0;
|
||||
}
|
||||
|
||||
function isNotEmpty(value: object): boolean {
|
||||
return !isEmpty(value);
|
||||
}
|
||||
|
||||
function hasField(value: object, extraArgs: string[]): boolean {
|
||||
const [name] = extraArgs;
|
||||
return name in value;
|
||||
@@ -40,7 +44,7 @@ function removeField(value: object, extraArgs: string[]): object {
|
||||
function removeFieldsContaining(value: object, extraArgs: string[]): object {
|
||||
const [match] = extraArgs;
|
||||
if (typeof match !== 'string') {
|
||||
throw new ExpressionExtensionError('argument of removeFieldsContaining must be an string');
|
||||
throw new ExpressionExtensionError('removeFieldsContaining(): expected string arg');
|
||||
}
|
||||
const newObject = { ...value };
|
||||
for (const [key, val] of Object.entries(value)) {
|
||||
@@ -55,7 +59,7 @@ function removeFieldsContaining(value: object, extraArgs: string[]): object {
|
||||
function keepFieldsContaining(value: object, extraArgs: string[]): object {
|
||||
const [match] = extraArgs;
|
||||
if (typeof match !== 'string') {
|
||||
throw new ExpressionExtensionError('argument of keepFieldsContaining must be an string');
|
||||
throw new ExpressionExtensionError('argument of keepFieldsContaining must be a string');
|
||||
}
|
||||
const newObject = { ...value };
|
||||
for (const [key, val] of Object.entries(value)) {
|
||||
@@ -71,8 +75,9 @@ export function compact(value: object): object {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const newObj: any = {};
|
||||
for (const [key, val] of Object.entries(value)) {
|
||||
if (val !== null && val !== undefined) {
|
||||
if (val !== null && val !== undefined && val !== 'nil' && val !== '') {
|
||||
if (typeof val === 'object') {
|
||||
if (Object.keys(val as object).length === 0) continue;
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-argument
|
||||
newObj[key] = compact(val);
|
||||
} else {
|
||||
@@ -89,10 +94,62 @@ export function urlEncode(value: object) {
|
||||
return new URLSearchParams(value as Record<string, string>).toString();
|
||||
}
|
||||
|
||||
isEmpty.doc = {
|
||||
name: 'isEmpty',
|
||||
description: 'Checks if the Object has no key-value pairs',
|
||||
returnType: 'boolean',
|
||||
};
|
||||
|
||||
isNotEmpty.doc = {
|
||||
name: 'isNotEmpty',
|
||||
description: 'Checks if the Object has key-value pairs',
|
||||
returnType: 'boolean',
|
||||
};
|
||||
|
||||
compact.doc = {
|
||||
name: 'compact',
|
||||
description: 'Removes empty values from an Object',
|
||||
returnType: 'boolean',
|
||||
};
|
||||
|
||||
urlEncode.doc = {
|
||||
name: 'urlEncode',
|
||||
description: 'Transforms an Object into a URL parameter list. Only top-level keys are supported.',
|
||||
returnType: 'string',
|
||||
};
|
||||
|
||||
// @TODO_NEXT_PHASE: Surface extensions below which take args
|
||||
|
||||
merge.doc = {
|
||||
name: 'merge',
|
||||
returnType: 'object',
|
||||
};
|
||||
|
||||
hasField.doc = {
|
||||
name: 'hasField',
|
||||
returnType: 'boolean',
|
||||
};
|
||||
|
||||
removeField.doc = {
|
||||
name: 'removeField',
|
||||
returnType: 'object',
|
||||
};
|
||||
|
||||
removeFieldsContaining.doc = {
|
||||
name: 'removeFieldsContaining',
|
||||
returnType: 'object',
|
||||
};
|
||||
|
||||
keepFieldsContaining.doc = {
|
||||
name: 'keepFieldsContaining',
|
||||
returnType: 'object',
|
||||
};
|
||||
|
||||
export const objectExtensions: ExtensionMap = {
|
||||
typeName: 'Object',
|
||||
functions: {
|
||||
isEmpty,
|
||||
isNotEmpty,
|
||||
merge,
|
||||
hasField,
|
||||
removeField,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
/* eslint-disable @typescript-eslint/unbound-method */
|
||||
// import { createHash } from 'crypto';
|
||||
import { titleCase } from 'title-case';
|
||||
import * as ExpressionError from '../ExpressionError';
|
||||
import type { ExtensionMap } from './Extensions';
|
||||
import CryptoJS from 'crypto-js';
|
||||
@@ -34,20 +35,17 @@ const URL_REGEXP =
|
||||
const CHAR_TEST_REGEXP = /\p{L}/u;
|
||||
const PUNC_TEST_REGEXP = /[!?.]/;
|
||||
|
||||
const TRUE_VALUES = ['true', '1', 't', 'yes', 'y'];
|
||||
const FALSE_VALUES = ['false', '0', 'f', 'no', 'n'];
|
||||
|
||||
function encrypt(value: string, extraArgs?: unknown): string {
|
||||
const [format = 'MD5'] = extraArgs as string[];
|
||||
if (format.toLowerCase() === 'base64') {
|
||||
function hash(value: string, extraArgs?: unknown): string {
|
||||
const [algorithm = 'MD5'] = extraArgs as string[];
|
||||
if (algorithm.toLowerCase() === 'base64') {
|
||||
// We're using a library instead of btoa because btoa only
|
||||
// works on ASCII
|
||||
return encode(value);
|
||||
}
|
||||
const hashFunction = hashFunctions[format.toLowerCase()];
|
||||
const hashFunction = hashFunctions[algorithm.toLowerCase()];
|
||||
if (!hashFunction) {
|
||||
throw new ExpressionError.ExpressionExtensionError(
|
||||
`Unknown encrypt type ${format}. Available types are: ${Object.keys(hashFunctions)
|
||||
`Unknown algorithm ${algorithm}. Available algorithms are: ${Object.keys(hashFunctions)
|
||||
.map((s) => s.toUpperCase())
|
||||
.join(', ')}, and Base64.`,
|
||||
);
|
||||
@@ -56,24 +54,12 @@ function encrypt(value: string, extraArgs?: unknown): string {
|
||||
// return createHash(format).update(value.toString()).digest('hex');
|
||||
}
|
||||
|
||||
function getOnlyFirstCharacters(value: string, extraArgs: number[]): string {
|
||||
const [end] = extraArgs;
|
||||
|
||||
if (typeof end !== 'number') {
|
||||
throw new ExpressionError.ExpressionExtensionError(
|
||||
'getOnlyFirstCharacters() requires a argument',
|
||||
);
|
||||
}
|
||||
|
||||
return value.slice(0, end);
|
||||
}
|
||||
|
||||
function isBlank(value: string): boolean {
|
||||
function isEmpty(value: string): boolean {
|
||||
return value === '';
|
||||
}
|
||||
|
||||
function isPresent(value: string): boolean {
|
||||
return !isBlank(value);
|
||||
function isNotEmpty(value: string): boolean {
|
||||
return !isEmpty(value);
|
||||
}
|
||||
|
||||
function length(value: string): number {
|
||||
@@ -122,16 +108,18 @@ function removeMarkdown(value: string): string {
|
||||
return output;
|
||||
}
|
||||
|
||||
function sayHi(value: string) {
|
||||
return `hi ${value}`;
|
||||
}
|
||||
|
||||
function stripTags(value: string): string {
|
||||
function removeTags(value: string): string {
|
||||
return value.replace(/<[^>]*>?/gm, '');
|
||||
}
|
||||
|
||||
function toDate(value: string): Date {
|
||||
return new Date(value.toString());
|
||||
const date = new Date(value.toString());
|
||||
|
||||
if (date.toString() === 'Invalid Date') {
|
||||
throw new ExpressionError.ExpressionExtensionError('cannot convert to date');
|
||||
}
|
||||
|
||||
return date;
|
||||
}
|
||||
|
||||
function urlDecode(value: string, extraArgs: boolean[]): string {
|
||||
@@ -152,11 +140,29 @@ function urlEncode(value: string, extraArgs: boolean[]): string {
|
||||
|
||||
function toInt(value: string, extraArgs: Array<number | undefined>) {
|
||||
const [radix] = extraArgs;
|
||||
return parseInt(value.replace(CURRENCY_REGEXP, ''), radix);
|
||||
const int = parseInt(value.replace(CURRENCY_REGEXP, ''), radix);
|
||||
|
||||
if (isNaN(int)) {
|
||||
throw new ExpressionError.ExpressionExtensionError('cannot convert to integer');
|
||||
}
|
||||
|
||||
return int;
|
||||
}
|
||||
|
||||
function toFloat(value: string) {
|
||||
return parseFloat(value.replace(CURRENCY_REGEXP, ''));
|
||||
if (value.includes(',')) {
|
||||
throw new ExpressionError.ExpressionExtensionError(
|
||||
'cannot convert to float, expected . as decimal separator',
|
||||
);
|
||||
}
|
||||
|
||||
const float = parseFloat(value.replace(CURRENCY_REGEXP, ''));
|
||||
|
||||
if (isNaN(float)) {
|
||||
throw new ExpressionError.ExpressionExtensionError('cannot convert to float');
|
||||
}
|
||||
|
||||
return float;
|
||||
}
|
||||
|
||||
function quote(value: string, extraArgs: string[]) {
|
||||
@@ -166,15 +172,9 @@ function quote(value: string, extraArgs: string[]) {
|
||||
.replace(new RegExp(`\\${quoteChar}`, 'g'), `\\${quoteChar}`)}${quoteChar}`;
|
||||
}
|
||||
|
||||
function isTrue(value: string) {
|
||||
return TRUE_VALUES.includes(value.toLowerCase());
|
||||
}
|
||||
|
||||
function isFalse(value: string) {
|
||||
return FALSE_VALUES.includes(value.toLowerCase());
|
||||
}
|
||||
|
||||
function isNumeric(value: string) {
|
||||
if (value.includes(' ')) return false;
|
||||
|
||||
return !isNaN(value as unknown as number) && !isNaN(parseFloat(value));
|
||||
}
|
||||
|
||||
@@ -185,7 +185,18 @@ function isUrl(value: string) {
|
||||
} catch (_error) {
|
||||
return false;
|
||||
}
|
||||
return url.protocol === 'http:' || url.protocol === 'https:';
|
||||
|
||||
// URL constructor tolerates missing `//` after protocol so check manually
|
||||
for (const scheme of ['http:', 'https:']) {
|
||||
if (
|
||||
url.protocol === scheme &&
|
||||
value.slice(scheme.length, scheme.length + '//'.length) === '//'
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function isDomain(value: string) {
|
||||
@@ -193,15 +204,22 @@ function isDomain(value: string) {
|
||||
}
|
||||
|
||||
function isEmail(value: string) {
|
||||
return EMAIL_REGEXP.test(value);
|
||||
}
|
||||
const result = EMAIL_REGEXP.test(value);
|
||||
|
||||
function stripSpecialChars(value: string) {
|
||||
return transliterate(value, { unknown: '?' });
|
||||
// email regex is loose so check manually for now
|
||||
if (result && value.includes(' ')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function toTitleCase(value: string) {
|
||||
return value.replace(/\w\S*/g, (v) => v.charAt(0).toLocaleUpperCase() + v.slice(1));
|
||||
return titleCase(value);
|
||||
}
|
||||
|
||||
function replaceSpecialChars(value: string) {
|
||||
return transliterate(value, { unknown: '?' });
|
||||
}
|
||||
|
||||
function toSentenceCase(value: string) {
|
||||
@@ -265,16 +283,153 @@ function extractUrl(value: string) {
|
||||
return matched[0];
|
||||
}
|
||||
|
||||
removeMarkdown.doc = {
|
||||
name: 'removeMarkdown',
|
||||
description: 'Removes Markdown formatting from a string',
|
||||
returnType: 'string',
|
||||
};
|
||||
|
||||
removeTags.doc = {
|
||||
name: 'removeTags',
|
||||
description: 'Removes tags, such as HTML or XML, from a string',
|
||||
returnType: 'string',
|
||||
};
|
||||
|
||||
toDate.doc = {
|
||||
name: 'toDate',
|
||||
description: 'Converts a string to a date',
|
||||
returnType: 'Date',
|
||||
};
|
||||
|
||||
toFloat.doc = {
|
||||
name: 'toFloat',
|
||||
description: 'Converts a string to a decimal number',
|
||||
returnType: 'number',
|
||||
aliases: ['toDecimalNumber'],
|
||||
};
|
||||
|
||||
toInt.doc = {
|
||||
name: 'toInt',
|
||||
description: 'Converts a string to an integer',
|
||||
returnType: 'number',
|
||||
aliases: ['toWholeNumber'],
|
||||
};
|
||||
|
||||
toSentenceCase.doc = {
|
||||
name: 'toSentenceCase',
|
||||
description: 'Formats a string to sentence case. Example: "This is a sentence"',
|
||||
returnType: 'string',
|
||||
};
|
||||
|
||||
toSnakeCase.doc = {
|
||||
name: 'toSnakeCase',
|
||||
description: 'Formats a string to snake case. Example: "this_is_snake_case"',
|
||||
returnType: 'string',
|
||||
};
|
||||
|
||||
toTitleCase.doc = {
|
||||
name: 'toTitleCase',
|
||||
description: 'Formats a string to title case. Example: "This Is a Title"',
|
||||
returnType: 'string',
|
||||
};
|
||||
|
||||
urlDecode.doc = {
|
||||
name: 'urlDecode',
|
||||
description:
|
||||
'Decodes a URL-encoded string. It decodes any percent-encoded characters in the input string, and replaces them with their original characters.',
|
||||
returnType: 'string',
|
||||
};
|
||||
|
||||
replaceSpecialChars.doc = {
|
||||
name: 'replaceSpecialChars',
|
||||
description: 'Replaces non-ASCII characters in a string with an ASCII representation',
|
||||
returnType: 'string',
|
||||
};
|
||||
|
||||
length.doc = {
|
||||
name: 'length',
|
||||
description: 'Returns the character count of a string',
|
||||
returnType: 'number',
|
||||
};
|
||||
|
||||
isDomain.doc = {
|
||||
name: 'isDomain',
|
||||
description: 'Checks if a string is a domain',
|
||||
returnType: 'boolean',
|
||||
};
|
||||
|
||||
isEmail.doc = {
|
||||
name: 'isEmail',
|
||||
description: 'Checks if a string is an email',
|
||||
returnType: 'boolean',
|
||||
};
|
||||
|
||||
isNumeric.doc = {
|
||||
name: 'isEmail',
|
||||
description: 'Checks if a string only contains digits',
|
||||
returnType: 'boolean',
|
||||
};
|
||||
|
||||
isUrl.doc = {
|
||||
name: 'isUrl',
|
||||
description: 'Checks if a string is a valid URL',
|
||||
returnType: 'boolean',
|
||||
};
|
||||
|
||||
isEmpty.doc = {
|
||||
name: 'isEmpty',
|
||||
description: 'Checks if a string is empty',
|
||||
returnType: 'boolean',
|
||||
};
|
||||
|
||||
isNotEmpty.doc = {
|
||||
name: 'isNotEmpty',
|
||||
description: 'Checks if a string has content',
|
||||
returnType: 'boolean',
|
||||
};
|
||||
|
||||
extractEmail.doc = {
|
||||
name: 'extractEmail',
|
||||
description: 'Extracts an email from a string. Returns undefined if none is found.',
|
||||
returnType: 'string',
|
||||
};
|
||||
|
||||
extractDomain.doc = {
|
||||
name: 'extractDomain',
|
||||
description:
|
||||
'Extracts a domain from a string containing a valid URL. Returns undefined if none is found.',
|
||||
returnType: 'string',
|
||||
};
|
||||
|
||||
extractUrl.doc = {
|
||||
name: 'extractUrl',
|
||||
description: 'Extracts a URL from a string. Returns undefined if none is found.',
|
||||
returnType: 'string',
|
||||
};
|
||||
|
||||
// @TODO_NEXT_PHASE: Surface extensions below which take args
|
||||
|
||||
hash.doc = {
|
||||
name: 'hash',
|
||||
returnType: 'string',
|
||||
};
|
||||
|
||||
urlEncode.doc = {
|
||||
name: 'urlEncode',
|
||||
returnType: 'string',
|
||||
};
|
||||
|
||||
quote.doc = {
|
||||
name: 'quote',
|
||||
returnType: 'string',
|
||||
};
|
||||
|
||||
export const stringExtensions: ExtensionMap = {
|
||||
typeName: 'String',
|
||||
functions: {
|
||||
encrypt,
|
||||
hash: encrypt,
|
||||
getOnlyFirstCharacters,
|
||||
hash,
|
||||
removeMarkdown,
|
||||
sayHi,
|
||||
stripTags,
|
||||
toBoolean: isTrue,
|
||||
removeTags,
|
||||
toDate,
|
||||
toDecimalNumber: toFloat,
|
||||
toFloat,
|
||||
@@ -286,18 +441,14 @@ export const stringExtensions: ExtensionMap = {
|
||||
urlDecode,
|
||||
urlEncode,
|
||||
quote,
|
||||
stripSpecialChars,
|
||||
replaceSpecialChars,
|
||||
length,
|
||||
isDomain,
|
||||
isEmail,
|
||||
isTrue,
|
||||
isFalse,
|
||||
isNotTrue: isFalse,
|
||||
isNumeric,
|
||||
isUrl,
|
||||
isURL: isUrl,
|
||||
isBlank,
|
||||
isPresent,
|
||||
isEmpty,
|
||||
isNotEmpty,
|
||||
extractEmail,
|
||||
extractDomain,
|
||||
extractUrl,
|
||||
|
||||
@@ -4,4 +4,7 @@ export {
|
||||
hasExpressionExtension,
|
||||
hasNativeMethod,
|
||||
extendTransform,
|
||||
EXTENSION_OBJECTS as ExpressionExtensions,
|
||||
} from './ExpressionExtension';
|
||||
|
||||
export type { DocMetadata } from './Extensions';
|
||||
|
||||
13
packages/workflow/src/NativeMethods/String.methods.ts
Normal file
13
packages/workflow/src/NativeMethods/String.methods.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
export const stringMethods = {
|
||||
typeName: 'String',
|
||||
functions: {
|
||||
// @TODO_NEXT_PHASE: Populate below and cover other datatypes
|
||||
// trim: {
|
||||
// doc: {
|
||||
// name: 'trim',
|
||||
// description: 'Removes whitespace from both ends of a string and returns a new string',
|
||||
// returnType: 'string',
|
||||
// },
|
||||
// },
|
||||
},
|
||||
};
|
||||
5
packages/workflow/src/NativeMethods/index.ts
Normal file
5
packages/workflow/src/NativeMethods/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { stringMethods } from './String.methods';
|
||||
|
||||
const NATIVE_METHODS = [stringMethods];
|
||||
|
||||
export { NATIVE_METHODS as NativeMethods };
|
||||
@@ -126,7 +126,7 @@ export class WorkflowDataProxy {
|
||||
const that = this;
|
||||
const node = this.workflow.nodes[nodeName];
|
||||
|
||||
if (!that.runExecutionData?.executionData && that.connectionInputData.length > 1) {
|
||||
if (!that.runExecutionData?.executionData && that.connectionInputData.length > 0) {
|
||||
return {}; // incoming connection has pinned data, so stub context object
|
||||
}
|
||||
|
||||
@@ -158,6 +158,7 @@ export class WorkflowDataProxy {
|
||||
};
|
||||
},
|
||||
get(target, name, receiver) {
|
||||
if (name === 'isProxy') return true;
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
name = name.toString();
|
||||
const contextData = NodeHelpers.getContext(that.runExecutionData!, 'node', node);
|
||||
@@ -179,6 +180,7 @@ export class WorkflowDataProxy {
|
||||
},
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
get(target, name, receiver) {
|
||||
if (name === 'isProxy') return true;
|
||||
name = name.toString();
|
||||
return that.selfData[name];
|
||||
},
|
||||
@@ -207,6 +209,7 @@ export class WorkflowDataProxy {
|
||||
};
|
||||
},
|
||||
get(target, name, receiver) {
|
||||
if (name === 'isProxy') return true;
|
||||
name = name.toString();
|
||||
|
||||
let returnValue: NodeParameterValueType;
|
||||
@@ -379,6 +382,7 @@ export class WorkflowDataProxy {
|
||||
{ binary: undefined, data: undefined, json: undefined },
|
||||
{
|
||||
get(target, name, receiver) {
|
||||
if (name === 'isProxy') return true;
|
||||
name = name.toString();
|
||||
|
||||
if (!node) {
|
||||
@@ -455,6 +459,8 @@ export class WorkflowDataProxy {
|
||||
{},
|
||||
{
|
||||
get(target, name, receiver) {
|
||||
if (name === 'isProxy') return true;
|
||||
|
||||
if (
|
||||
typeof process === 'undefined' || // env vars are inaccessible to frontend
|
||||
process.env.N8N_BLOCK_ENV_ACCESS_IN_NODE === 'true'
|
||||
@@ -490,6 +496,8 @@ export class WorkflowDataProxy {
|
||||
};
|
||||
},
|
||||
get(target, name, receiver) {
|
||||
if (name === 'isProxy') return true;
|
||||
|
||||
if (!that.executeData?.source) {
|
||||
// Means the previous node did not get executed yet
|
||||
return undefined;
|
||||
@@ -535,6 +543,8 @@ export class WorkflowDataProxy {
|
||||
};
|
||||
},
|
||||
get(target, name, receiver) {
|
||||
if (name === 'isProxy') return true;
|
||||
|
||||
if (allowedValues.includes(name.toString())) {
|
||||
const value = that.workflow[name as keyof typeof target];
|
||||
|
||||
@@ -567,6 +577,8 @@ export class WorkflowDataProxy {
|
||||
{},
|
||||
{
|
||||
get(target, name, receiver) {
|
||||
if (name === 'isProxy') return true;
|
||||
|
||||
const nodeName = name.toString();
|
||||
|
||||
if (that.workflow.getNode(nodeName) === null) {
|
||||
@@ -607,6 +619,14 @@ export class WorkflowDataProxy {
|
||||
|
||||
// replacing proxies with the actual data.
|
||||
const jmespathWrapper = (data: IDataObject | IDataObject[], query: string) => {
|
||||
if (typeof data !== 'object' || typeof query !== 'string') {
|
||||
throw new ExpressionError('expected two arguments (Object, string) for this function', {
|
||||
runIndex: that.runIndex,
|
||||
itemIndex: that.itemIndex,
|
||||
clientOnly: true,
|
||||
});
|
||||
}
|
||||
|
||||
if (!Array.isArray(data) && typeof data === 'object') {
|
||||
return jmespath.search({ ...data }, query);
|
||||
}
|
||||
@@ -939,6 +959,8 @@ export class WorkflowDataProxy {
|
||||
];
|
||||
},
|
||||
get(target, property, receiver) {
|
||||
if (property === 'isProxy') return true;
|
||||
|
||||
if (['pairedItem', 'itemMatching', 'item'].includes(property as string)) {
|
||||
const pairedItemMethod = (itemIndex?: number) => {
|
||||
if (itemIndex === undefined) {
|
||||
@@ -1059,6 +1081,8 @@ export class WorkflowDataProxy {
|
||||
};
|
||||
},
|
||||
get(target, property, receiver) {
|
||||
if (property === 'isProxy') return true;
|
||||
|
||||
if (property === 'item') {
|
||||
return that.connectionInputData[that.itemIndex];
|
||||
}
|
||||
@@ -1214,6 +1238,8 @@ export class WorkflowDataProxy {
|
||||
|
||||
return new Proxy(base, {
|
||||
get(target, name, receiver) {
|
||||
if (name === 'isProxy') return true;
|
||||
|
||||
if (['$data', '$json'].includes(name as string)) {
|
||||
return that.nodeDataGetter(that.activeNodeName, true)?.json;
|
||||
}
|
||||
|
||||
@@ -28,3 +28,8 @@ export {
|
||||
isINodePropertyCollectionList,
|
||||
isINodePropertyOptionsList,
|
||||
} from './type-guards';
|
||||
|
||||
export { ExpressionExtensions } from './Extensions';
|
||||
export { NativeMethods } from './NativeMethods';
|
||||
|
||||
export type { DocMetadata } from './Extensions';
|
||||
|
||||
@@ -6,16 +6,12 @@ import { evaluate } from './Helpers';
|
||||
|
||||
describe('Data Transformation Functions', () => {
|
||||
describe('Array Data Transformation Functions', () => {
|
||||
test('.random() should work correctly on an array', () => {
|
||||
expect(evaluate('={{ [1,2,3].random() }}')).not.toBeUndefined();
|
||||
});
|
||||
|
||||
test('.randomItem() alias should work correctly on an array', () => {
|
||||
test('.randomItem() should work correctly on an array', () => {
|
||||
expect(evaluate('={{ [1,2,3].randomItem() }}')).not.toBeUndefined();
|
||||
});
|
||||
|
||||
test('.isPresent() should work correctly on an array', () => {
|
||||
expect(evaluate('={{ [1,2,3, "imhere"].isPresent() }}')).toEqual(true);
|
||||
test('.isNotEmpty() should work correctly on an array', () => {
|
||||
expect(evaluate('={{ [1,2,3, "imhere"].isNotEmpty() }}')).toEqual(true);
|
||||
});
|
||||
|
||||
test('.pluck() should work correctly on an array', () => {
|
||||
@@ -46,24 +42,12 @@ describe('Data Transformation Functions', () => {
|
||||
);
|
||||
});
|
||||
|
||||
test('.isBlank() should work correctly on an array', () => {
|
||||
expect(evaluate('={{ [].isBlank() }}')).toEqual(true);
|
||||
test('.isEmpty() should work correctly on an array', () => {
|
||||
expect(evaluate('={{ [].isEmpty() }}')).toEqual(true);
|
||||
});
|
||||
|
||||
test('.isBlank() should work correctly on an array', () => {
|
||||
expect(evaluate('={{ [1].isBlank() }}')).toEqual(false);
|
||||
});
|
||||
|
||||
test('.length() should work correctly on an array', () => {
|
||||
expect(evaluate('={{ [].length() }}')).toEqual(0);
|
||||
});
|
||||
|
||||
test('.count() should work correctly on an array', () => {
|
||||
expect(evaluate('={{ [1].count() }}')).toEqual(1);
|
||||
});
|
||||
|
||||
test('.size() should work correctly on an array', () => {
|
||||
expect(evaluate('={{ [1,2].size() }}')).toEqual(2);
|
||||
test('.isEmpty() should work correctly on an array', () => {
|
||||
expect(evaluate('={{ [1].isEmpty() }}')).toEqual(false);
|
||||
});
|
||||
|
||||
test('.last() should work correctly on an array', () => {
|
||||
@@ -74,12 +58,6 @@ describe('Data Transformation Functions', () => {
|
||||
expect(evaluate('={{ ["repeat","repeat","a","b","c"].first() }}')).toEqual('repeat');
|
||||
});
|
||||
|
||||
test('.filter() should work correctly on an array', () => {
|
||||
expect(evaluate('={{ ["repeat","repeat","a","b","c"].filter("repeat") }}')).toEqual(
|
||||
expect.arrayContaining(['repeat', 'repeat']),
|
||||
);
|
||||
});
|
||||
|
||||
test('.merge() should work correctly on an array', () => {
|
||||
expect(
|
||||
evaluate(
|
||||
@@ -115,26 +93,22 @@ describe('Data Transformation Functions', () => {
|
||||
|
||||
test('.sum() should work on an array of numbers', () => {
|
||||
expect(evaluate('={{ [1, 2, 3, 4, 5, 6].sum() }}')).toEqual(21);
|
||||
expect(evaluate('={{ ["1", 2, 3, 4, 5, 6].sum() }}')).toEqual(21);
|
||||
expect(evaluate('={{ ["1", 2, 3, 4, 5, "bad"].sum() }}')).toBeNaN();
|
||||
expect(() => evaluate('={{ ["1", 2, 3, 4, 5, "bad"].sum() }}')).toThrow();
|
||||
});
|
||||
|
||||
test('.average() should work on an array of numbers', () => {
|
||||
expect(evaluate('={{ [1, 2, 3, 4, 5, 6].average() }}')).toEqual(3.5);
|
||||
expect(evaluate('={{ ["1", 2, 3, 4, 5, 6].average() }}')).toEqual(3.5);
|
||||
expect(evaluate('={{ ["1", 2, 3, 4, 5, "bad"].average() }}')).toBeNaN();
|
||||
expect(() => evaluate('={{ ["1", 2, 3, 4, 5, "bad"].average() }}')).toThrow();
|
||||
});
|
||||
|
||||
test('.min() should work on an array of numbers', () => {
|
||||
expect(evaluate('={{ [1, 2, 3, 4, 5, 6].min() }}')).toEqual(1);
|
||||
expect(evaluate('={{ ["1", 2, 3, 4, 5, 6].min() }}')).toEqual(1);
|
||||
expect(evaluate('={{ ["1", 2, 3, 4, 5, "bad"].min() }}')).toBeNaN();
|
||||
expect(() => evaluate('={{ ["1", 2, 3, 4, 5, "bad"].min() }}')).toThrow();
|
||||
});
|
||||
|
||||
test('.max() should work on an array of numbers', () => {
|
||||
expect(evaluate('={{ [1, 2, 3, 4, 5, 6].max() }}')).toEqual(6);
|
||||
expect(evaluate('={{ ["1", 2, 3, 4, 5, 6].max() }}')).toEqual(6);
|
||||
expect(evaluate('={{ ["1", 2, 3, 4, 5, "bad"].max() }}')).toBeNaN();
|
||||
expect(() => evaluate('={{ ["1", 2, 3, 4, 5, "bad"].max() }}')).toThrow();
|
||||
});
|
||||
|
||||
test('.union() should work on an array of objects', () => {
|
||||
@@ -181,18 +155,5 @@ describe('Data Transformation Functions', () => {
|
||||
[16, 17, 18, 19, 20],
|
||||
]);
|
||||
});
|
||||
|
||||
test('.filter() should work on a list of strings', () => {
|
||||
expect(
|
||||
evaluate(
|
||||
'={{ ["i am a test string", "i should be kept", "i should be removed test"].filter("test", "remove") }}',
|
||||
),
|
||||
).toEqual(['i should be kept']);
|
||||
expect(
|
||||
evaluate(
|
||||
'={{ ["i am a test string", "i should be kept test", "i should be removed"].filter("test") }}',
|
||||
),
|
||||
).toEqual(['i am a test string', 'i should be kept test']);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -14,11 +14,6 @@ describe('Data Transformation Functions', () => {
|
||||
expect(evaluate('={{ DateTime.local(2023, 1, 23).isWeekend() }}')).toBe(false);
|
||||
});
|
||||
|
||||
test('.toTimeFromNow() should work correctly on a date', () => {
|
||||
const JUST_NOW_STRING_RESULT = 'just now';
|
||||
expect(evaluate('={{DateTime.now().toTimeFromNow()}}')).toEqual(JUST_NOW_STRING_RESULT);
|
||||
});
|
||||
|
||||
test('.beginningOf("week") should work correctly on a date', () => {
|
||||
expect(evaluate('={{ DateTime.local(2023, 1, 20).beginningOf("week") }}')).toEqual(
|
||||
DateTime.local(2023, 1, 16, { zone: TEST_TIMEZONE }).toJSDate(),
|
||||
|
||||
@@ -8,19 +8,19 @@ import { evaluate } from './Helpers';
|
||||
|
||||
describe('Expression Extension Transforms', () => {
|
||||
describe('extend() transform', () => {
|
||||
test('Basic transform with .isBlank', () => {
|
||||
expect(extendTransform('"".isBlank()')!.code).toEqual('extend("", "isBlank", [])');
|
||||
test('Basic transform with .isEmpty', () => {
|
||||
expect(extendTransform('"".isEmpty()')!.code).toEqual('extend("", "isEmpty", [])');
|
||||
});
|
||||
|
||||
test('Chained transform with .sayHi.getOnlyFirstCharacters', () => {
|
||||
expect(extendTransform('"".sayHi().getOnlyFirstCharacters(2)')!.code).toEqual(
|
||||
'extend(extend("", "sayHi", []), "getOnlyFirstCharacters", [2])',
|
||||
test('Chained transform with .toSnakeCase.toSentenceCase', () => {
|
||||
expect(extendTransform('"".toSnakeCase().toSentenceCase(2)')!.code).toEqual(
|
||||
'extend(extend("", "toSnakeCase", []), "toSentenceCase", [2])',
|
||||
);
|
||||
});
|
||||
|
||||
test('Chained transform with native functions .sayHi.trim.getOnlyFirstCharacters', () => {
|
||||
expect(extendTransform('"aaa ".sayHi().trim().getOnlyFirstCharacters(2)')!.code).toEqual(
|
||||
'extend(extend("aaa ", "sayHi", []).trim(), "getOnlyFirstCharacters", [2])',
|
||||
test('Chained transform with native functions .toSnakeCase.trim.toSentenceCase', () => {
|
||||
expect(extendTransform('"aaa ".toSnakeCase().trim().toSentenceCase(2)')!.code).toEqual(
|
||||
'extend(extend("aaa ", "toSnakeCase", []).trim(), "toSentenceCase", [2])',
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -36,19 +36,21 @@ describe('tmpl Expression Parser', () => {
|
||||
});
|
||||
|
||||
test('Multiple expression', () => {
|
||||
expect(splitExpression('{{ "test".sayHi() }} you have ${{ (100).format() }}.')).toEqual([
|
||||
{ type: 'text', text: '' },
|
||||
{ type: 'code', text: ' "test".sayHi() ', hasClosingBrackets: true },
|
||||
{ type: 'text', text: ' you have $' },
|
||||
{ type: 'code', text: ' (100).format() ', hasClosingBrackets: true },
|
||||
{ type: 'text', text: '.' },
|
||||
]);
|
||||
expect(splitExpression('{{ "test".toSnakeCase() }} you have ${{ (100).format() }}.')).toEqual(
|
||||
[
|
||||
{ type: 'text', text: '' },
|
||||
{ type: 'code', text: ' "test".toSnakeCase() ', hasClosingBrackets: true },
|
||||
{ type: 'text', text: ' you have $' },
|
||||
{ type: 'code', text: ' (100).format() ', hasClosingBrackets: true },
|
||||
{ type: 'text', text: '.' },
|
||||
],
|
||||
);
|
||||
});
|
||||
|
||||
test('Unclosed expression', () => {
|
||||
expect(splitExpression('{{ "test".sayHi() }} you have ${{ (100).format()')).toEqual([
|
||||
expect(splitExpression('{{ "test".toSnakeCase() }} you have ${{ (100).format()')).toEqual([
|
||||
{ type: 'text', text: '' },
|
||||
{ type: 'code', text: ' "test".sayHi() ', hasClosingBrackets: true },
|
||||
{ type: 'code', text: ' "test".toSnakeCase() ', hasClosingBrackets: true },
|
||||
{ type: 'text', text: ' you have $' },
|
||||
{ type: 'code', text: ' (100).format()', hasClosingBrackets: false },
|
||||
]);
|
||||
@@ -75,14 +77,16 @@ describe('tmpl Expression Parser', () => {
|
||||
|
||||
test('Multiple expression', () => {
|
||||
expect(
|
||||
joinExpression(splitExpression('{{ "test".sayHi() }} you have ${{ (100).format() }}.')),
|
||||
).toEqual('{{ "test".sayHi() }} you have ${{ (100).format() }}.');
|
||||
joinExpression(
|
||||
splitExpression('{{ "test".toSnakeCase() }} you have ${{ (100).format() }}.'),
|
||||
),
|
||||
).toEqual('{{ "test".toSnakeCase() }} you have ${{ (100).format() }}.');
|
||||
});
|
||||
|
||||
test('Unclosed expression', () => {
|
||||
expect(
|
||||
joinExpression(splitExpression('{{ "test".sayHi() }} you have ${{ (100).format()')),
|
||||
).toEqual('{{ "test".sayHi() }} you have ${{ (100).format()');
|
||||
joinExpression(splitExpression('{{ "test".toSnakeCase() }} you have ${{ (100).format()')),
|
||||
).toEqual('{{ "test".toSnakeCase() }} you have ${{ (100).format()');
|
||||
});
|
||||
|
||||
test('Escaped opening bracket', () => {
|
||||
|
||||
@@ -2,8 +2,8 @@ import { evaluate } from './Helpers';
|
||||
|
||||
describe('Data Transformation Functions', () => {
|
||||
describe('Genric Data Transformation Functions', () => {
|
||||
test('.isBlank() should work correctly on undefined', () => {
|
||||
expect(evaluate('={{(undefined).isBlank()}}')).toEqual(true);
|
||||
test('.isEmpty() should work correctly on undefined', () => {
|
||||
expect(evaluate('={{(undefined).isEmpty()}}')).toEqual(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -7,20 +7,6 @@ import { evaluate } from './Helpers';
|
||||
|
||||
describe('Data Transformation Functions', () => {
|
||||
describe('Number Data Transformation Functions', () => {
|
||||
test('.random() should work correctly on a number', () => {
|
||||
expect(evaluate('={{ Number(100).random() }}')).not.toBeUndefined();
|
||||
});
|
||||
|
||||
test('.isBlank() should work correctly on a number', () => {
|
||||
expect(evaluate('={{ Number(100).isBlank() }}')).toEqual(false);
|
||||
});
|
||||
|
||||
test('.isPresent() should work correctly on a number', () => {
|
||||
expect(evaluate('={{ Number(100).isPresent() }}')).toEqual(
|
||||
numberExtensions.functions.isPresent(100),
|
||||
);
|
||||
});
|
||||
|
||||
test('.format() should work correctly on a number', () => {
|
||||
expect(evaluate('={{ Number(100).format() }}')).toEqual(
|
||||
numberExtensions.functions.format(100, []),
|
||||
@@ -48,18 +34,6 @@ describe('Data Transformation Functions', () => {
|
||||
expect(evaluate('={{ (NaN).round(3) }}')).toBeNaN();
|
||||
});
|
||||
|
||||
test('.isTrue() should work on a number', () => {
|
||||
expect(evaluate('={{ (1).isTrue() }}')).toEqual(true);
|
||||
expect(evaluate('={{ (0).isTrue() }}')).toEqual(false);
|
||||
expect(evaluate('={{ (NaN).isTrue() }}')).toEqual(false);
|
||||
});
|
||||
|
||||
test('.isFalse() should work on a number', () => {
|
||||
expect(evaluate('={{ (1).isFalse() }}')).toEqual(false);
|
||||
expect(evaluate('={{ (0).isFalse() }}')).toEqual(true);
|
||||
expect(evaluate('={{ (NaN).isFalse() }}')).toEqual(false);
|
||||
});
|
||||
|
||||
test('.isOdd() should work on a number', () => {
|
||||
expect(evaluate('={{ (9).isOdd() }}')).toEqual(true);
|
||||
expect(evaluate('={{ (8).isOdd() }}')).toEqual(false);
|
||||
@@ -77,8 +51,8 @@ describe('Data Transformation Functions', () => {
|
||||
|
||||
describe('Multiple expressions', () => {
|
||||
test('Basic multiple expressions', () => {
|
||||
expect(evaluate('={{ "Test".sayHi() }} you have ${{ (100).format() }}.')).toEqual(
|
||||
'hi Test you have $100.',
|
||||
expect(evaluate('={{ "test abc".toSnakeCase() }} you have ${{ (100).format() }}.')).toEqual(
|
||||
'test_abc you have $100.',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -8,47 +8,25 @@ import { evaluate } from './Helpers';
|
||||
|
||||
describe('Data Transformation Functions', () => {
|
||||
describe('String Data Transformation Functions', () => {
|
||||
test('.isBlank() should work correctly on a string that is not empty', () => {
|
||||
expect(evaluate('={{"NotBlank".isBlank()}}')).toEqual(false);
|
||||
test('.isEmpty() should work correctly on a string that is not empty', () => {
|
||||
expect(evaluate('={{"NotBlank".isEmpty()}}')).toEqual(false);
|
||||
});
|
||||
|
||||
test('.isBlank() should work correctly on a string that is empty', () => {
|
||||
expect(evaluate('={{"".isBlank()}}')).toEqual(true);
|
||||
test('.isEmpty() should work correctly on a string that is empty', () => {
|
||||
expect(evaluate('={{"".isEmpty()}}')).toEqual(true);
|
||||
});
|
||||
|
||||
test('.getOnlyFirstCharacters() should work correctly on a string', () => {
|
||||
expect(evaluate('={{"myNewField".getOnlyFirstCharacters(5)}}')).toEqual('myNew');
|
||||
|
||||
expect(evaluate('={{"myNewField".getOnlyFirstCharacters(10)}}')).toEqual('myNewField');
|
||||
|
||||
expect(
|
||||
evaluate('={{"myNewField".getOnlyFirstCharacters(5).length >= "myNewField".length}}'),
|
||||
).toEqual(false);
|
||||
|
||||
expect(evaluate('={{DateTime.now().toLocaleString().getOnlyFirstCharacters(2)}}')).toEqual(
|
||||
stringExtensions.functions.getOnlyFirstCharacters(
|
||||
// @ts-ignore
|
||||
dateExtensions.functions.toLocaleString(new Date(), []),
|
||||
[2],
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
test('.sayHi() should work correctly on a string', () => {
|
||||
expect(evaluate('={{ "abc".sayHi() }}')).toEqual('hi abc');
|
||||
});
|
||||
|
||||
test('.encrypt() should work correctly on a string', () => {
|
||||
expect(evaluate('={{ "12345".encrypt("sha256") }}')).toEqual(
|
||||
stringExtensions.functions.encrypt('12345', ['sha256']),
|
||||
test('.hash() should work correctly on a string', () => {
|
||||
expect(evaluate('={{ "12345".hash("sha256") }}')).toEqual(
|
||||
stringExtensions.functions.hash('12345', ['sha256']),
|
||||
);
|
||||
|
||||
expect(evaluate('={{ "12345".encrypt("sha256") }}')).not.toEqual(
|
||||
stringExtensions.functions.encrypt('12345', ['MD5']),
|
||||
expect(evaluate('={{ "12345".hash("sha256") }}')).not.toEqual(
|
||||
stringExtensions.functions.hash('12345', ['MD5']),
|
||||
);
|
||||
|
||||
expect(evaluate('={{ "12345".encrypt("MD5") }}')).toEqual(
|
||||
stringExtensions.functions.encrypt('12345', ['MD5']),
|
||||
expect(evaluate('={{ "12345".hash("MD5") }}')).toEqual(
|
||||
stringExtensions.functions.hash('12345', ['MD5']),
|
||||
);
|
||||
|
||||
expect(evaluate('={{ "12345".hash("sha256") }}')).toEqual(
|
||||
@@ -74,8 +52,8 @@ describe('Data Transformation Functions', () => {
|
||||
);
|
||||
});
|
||||
|
||||
test('.stripTags should work correctly on a string', () => {
|
||||
expect(evaluate('={{ "<html><head>test</head></html>".stripTags() }}')).toEqual('test');
|
||||
test('.removeTags should work correctly on a string', () => {
|
||||
expect(evaluate('={{ "<html><head>test</head></html>".removeTags() }}')).toEqual('test');
|
||||
});
|
||||
|
||||
test('.removeMarkdown should work correctly on a string', () => {
|
||||
@@ -92,48 +70,6 @@ describe('Data Transformation Functions', () => {
|
||||
);
|
||||
});
|
||||
|
||||
test('.toBoolean should work correctly on a string', () => {
|
||||
const validTrue = ['y', 'yes', 't', 'true', '1', 'YES'];
|
||||
for (const v of validTrue) {
|
||||
expect(evaluate(`={{ "${v}".toBoolean() }}`)).toEqual(true);
|
||||
}
|
||||
|
||||
const validFalse = ['n', 'no', 'f', 'false', '0', 'NO'];
|
||||
for (const v of validFalse) {
|
||||
expect(evaluate(`={{ "${v}".toBoolean() }}`)).toEqual(false);
|
||||
}
|
||||
|
||||
expect(evaluate('={{ "maybe".toBoolean() }}')).toEqual(false);
|
||||
});
|
||||
|
||||
test('.isTrue should work correctly on a string', () => {
|
||||
const validTrue = ['y', 'yes', 't', 'true', '1', 'YES'];
|
||||
for (const v of validTrue) {
|
||||
expect(evaluate(`={{ "${v}".isTrue() }}`)).toEqual(true);
|
||||
}
|
||||
|
||||
const validFalse = ['n', 'no', 'f', 'false', '0', 'NO'];
|
||||
for (const v of validFalse) {
|
||||
expect(evaluate(`={{ "${v}".isTrue() }}`)).toEqual(false);
|
||||
}
|
||||
|
||||
expect(evaluate('={{ "maybe".isTrue() }}')).toEqual(false);
|
||||
});
|
||||
|
||||
test('.isFalse should work correctly on a string', () => {
|
||||
const validTrue = ['y', 'yes', 't', 'true', '1', 'YES'];
|
||||
for (const v of validTrue) {
|
||||
expect(evaluate(`={{ "${v}".isFalse() }}`)).toEqual(false);
|
||||
}
|
||||
|
||||
const validFalse = ['n', 'no', 'f', 'false', '0', 'NO'];
|
||||
for (const v of validFalse) {
|
||||
expect(evaluate(`={{ "${v}".isFalse() }}`)).toEqual(true);
|
||||
}
|
||||
|
||||
expect(evaluate('={{ "maybe".isFalse() }}')).toEqual(false);
|
||||
});
|
||||
|
||||
test('.toFloat should work correctly on a string', () => {
|
||||
expect(evaluate('={{ "1.1".toFloat() }}')).toEqual(1.1);
|
||||
expect(evaluate('={{ "1.1".toDecimalNumber() }}')).toEqual(1.1);
|
||||
@@ -185,16 +121,6 @@ describe('Data Transformation Functions', () => {
|
||||
expect(evaluate('={{ "i am a test".toSentenceCase() }}')).toEqual('I am a test');
|
||||
});
|
||||
|
||||
test('.toTitleCase should work on a string', () => {
|
||||
expect(
|
||||
evaluate(
|
||||
'={{ "i am a test! i have multiple types of Punctuation. or do i?".toTitleCase() }}',
|
||||
),
|
||||
).toEqual('I Am A Test! I Have Multiple Types Of Punctuation. Or Do I?');
|
||||
expect(evaluate('={{ "i am a test!".toTitleCase() }}')).toEqual('I Am A Test!');
|
||||
expect(evaluate('={{ "i am a test".toTitleCase() }}')).toEqual('I Am A Test');
|
||||
});
|
||||
|
||||
test('.extractUrl should work on a string', () => {
|
||||
expect(
|
||||
evaluate(
|
||||
|
||||
Reference in New Issue
Block a user