n8n/packages/@n8n/expression-runtime/src/extensions/function-extensions.ts
n8n-assistant[bot] 85b7796434
chore: Bundle 2.x (#28844)
Co-authored-by: Matsu <matias.huhta@n8n.io>
Co-authored-by: Dawid Myslak <dawid.myslak@gmail.com>
Co-authored-by: Bernhard Wittmann <bernhard.wittmann@n8n.io>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: Dimitri Lavrenük <20122620+dlavrenuek@users.noreply.github.com>
Co-authored-by: Benjamin Schroth <68321970+schrothbn@users.noreply.github.com>
Co-authored-by: Danny Martini <danny@n8n.io>
Co-authored-by: RomanDavydchuk <roman.davydchuk@n8n.io>
Co-authored-by: Sandra Zollner <sandra.zollner@n8n.io>
Co-authored-by: Milorad FIlipović <milorad@n8n.io>
Co-authored-by: Iván Ovejero <ivov.src@gmail.com>
2026-04-22 08:51:32 +03:00

94 lines
2.3 KiB
TypeScript

import { average as aAverage } from './array-extensions';
import { ExpressionExtensionError } from './expression-extension-error';
const min = Math.min;
const max = Math.max;
const numberList = (start: number, end: number): number[] => {
const size = Math.abs(start - end) + 1;
const arr = new Array<number>(size);
let curr = start;
for (let i = 0; i < size; i++) {
if (start < end) {
arr[i] = curr++;
} else {
arr[i] = curr--;
}
}
return arr;
};
const zip = (keys: unknown[], values: unknown[]): unknown => {
if (!Array.isArray(keys) || !Array.isArray(values)) {
throw new ExpressionExtensionError('keys and values must be arrays');
}
if (keys.length !== values.length) {
throw new ExpressionExtensionError('keys and values not of equal length');
}
const result: Record<string, unknown> = {};
for (let i = 0; i < keys.length; i++) {
result[keys[i] as string] = values[i];
}
return result;
};
const average = (...args: number[]) => {
return aAverage(args);
};
const not = (value: unknown): boolean => {
return !value;
};
function ifEmpty<T, V>(value: V, defaultValue: T) {
if (arguments.length !== 2) {
// DIVERGENCE from packages/workflow/src/extensions/extended-functions.ts:
// The original throws ExpressionError (from ExecutionBaseError). The runtime
// uses ExpressionExtensionError because the full ExpressionError class is not
// available in the extensions layer — only a minimal shim exists in safe-globals.
throw new ExpressionExtensionError(
'expected two arguments (value, defaultValue) for this function',
);
}
if (value === undefined || value === null || value === '') {
return defaultValue;
}
if (typeof value === 'object') {
if (Array.isArray(value) && !value.length) {
return defaultValue;
}
if (!Object.keys(value).length) {
return defaultValue;
}
}
return value;
}
ifEmpty.doc = {
name: 'ifEmpty',
description:
'Returns the default value if the value is empty. Empty values are undefined, null, empty strings, arrays without elements and objects without keys.',
returnType: 'any',
args: [
{ name: 'value', type: 'any' },
{ name: 'defaultValue', type: 'any' },
],
docURL: 'https://docs.n8n.io/code/builtin/convenience',
};
export const extendedFunctions = {
min,
max,
not,
average,
numberList,
zip,
$min: min,
$max: max,
$average: average,
$not: not,
$ifEmpty: ifEmpty,
};