2023-05-02 02:44:25 -07:00
|
|
|
|
import type {
|
|
|
|
|
IDataObject,
|
|
|
|
|
IDisplayOptions,
|
|
|
|
|
INodeExecutionData,
|
|
|
|
|
INodeProperties,
|
2023-10-06 08:04:33 -07:00
|
|
|
|
IPairedItemData,
|
2023-05-02 02:44:25 -07:00
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
2024-06-20 03:09:23 -07:00
|
|
|
|
import { ApplicationError, jsonParse, randomInt } from 'n8n-workflow';
|
2023-01-24 02:32:31 -08:00
|
|
|
|
|
2024-06-20 03:09:23 -07:00
|
|
|
|
import { isEqual, isNull, merge, isObject, reduce, get } from 'lodash';
|
2023-01-24 02:32:31 -08:00
|
|
|
|
|
2020-07-08 01:00:13 -07:00
|
|
|
|
/**
|
|
|
|
|
* Creates an array of elements split into groups the length of `size`.
|
|
|
|
|
* If `array` can't be split evenly, the final chunk will be the remaining
|
|
|
|
|
* elements.
|
|
|
|
|
*
|
|
|
|
|
* @param {Array} array The array to process.
|
|
|
|
|
* @param {number} [size=1] The length of each chunk
|
|
|
|
|
* @example
|
|
|
|
|
*
|
|
|
|
|
* chunk(['a', 'b', 'c', 'd'], 2)
|
|
|
|
|
* // => [['a', 'b'], ['c', 'd']]
|
|
|
|
|
*
|
|
|
|
|
* chunk(['a', 'b', 'c', 'd'], 3)
|
|
|
|
|
* // => [['a', 'b', 'c'], ['d']]
|
|
|
|
|
*/
|
2022-08-17 08:50:24 -07:00
|
|
|
|
|
2023-07-31 05:20:39 -07:00
|
|
|
|
export function chunk<T>(array: T[], size = 1) {
|
|
|
|
|
const length = array === null ? 0 : array.length;
|
2020-07-08 01:00:13 -07:00
|
|
|
|
if (!length || size < 1) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
let index = 0;
|
|
|
|
|
let resIndex = 0;
|
|
|
|
|
const result = new Array(Math.ceil(length / size));
|
|
|
|
|
|
|
|
|
|
while (index < length) {
|
|
|
|
|
result[resIndex++] = array.slice(index, (index += size));
|
|
|
|
|
}
|
2023-07-31 05:20:39 -07:00
|
|
|
|
return result as T[][];
|
2020-07-08 01:00:13 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-20 03:09:23 -07:00
|
|
|
|
/**
|
|
|
|
|
* Shuffles an array in place using the Fisher-Yates shuffle algorithm
|
|
|
|
|
* @param {Array} array The array to shuffle.
|
|
|
|
|
*/
|
|
|
|
|
export const shuffleArray = <T>(array: T[]): void => {
|
|
|
|
|
for (let i = array.length - 1; i > 0; i--) {
|
|
|
|
|
const j = randomInt(i + 1);
|
|
|
|
|
[array[i], array[j]] = [array[j], array[i]];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Flattens an object with deep data
|
|
|
|
|
* @param {IDataObject} data The object to flatten
|
|
|
|
|
* @param {string[]} prefix The prefix to add to each key in the returned flat object
|
|
|
|
|
*/
|
|
|
|
|
export const flattenKeys = (obj: IDataObject, prefix: string[] = []): IDataObject => {
|
|
|
|
|
return !isObject(obj)
|
|
|
|
|
? { [prefix.join('.')]: obj }
|
|
|
|
|
: reduce(
|
|
|
|
|
obj,
|
|
|
|
|
(cum, next, key) => merge(cum, flattenKeys(next as IDataObject, [...prefix, key])),
|
|
|
|
|
{},
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2020-07-08 01:00:13 -07:00
|
|
|
|
/**
|
|
|
|
|
* Takes a multidimensional array and converts it to a one-dimensional array.
|
|
|
|
|
*
|
|
|
|
|
* @param {Array} nestedArray The array to be flattened.
|
|
|
|
|
* @example
|
|
|
|
|
*
|
|
|
|
|
* flatten([['a', 'b'], ['c', 'd']])
|
|
|
|
|
* // => ['a', 'b', 'c', 'd']
|
|
|
|
|
*
|
|
|
|
|
*/
|
2022-08-17 08:50:24 -07:00
|
|
|
|
|
2023-07-31 05:20:39 -07:00
|
|
|
|
export function flatten<T>(nestedArray: T[][]) {
|
2020-07-08 01:00:13 -07:00
|
|
|
|
const result = [];
|
|
|
|
|
|
2023-07-31 05:20:39 -07:00
|
|
|
|
(function loop(array: T[] | T[][]) {
|
2020-07-14 04:59:37 -07:00
|
|
|
|
for (let i = 0; i < array.length; i++) {
|
2020-07-08 01:00:13 -07:00
|
|
|
|
if (Array.isArray(array[i])) {
|
2023-07-31 05:20:39 -07:00
|
|
|
|
loop(array[i] as T[]);
|
2020-07-08 01:00:13 -07:00
|
|
|
|
} else {
|
|
|
|
|
result.push(array[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})(nestedArray);
|
|
|
|
|
|
2023-07-31 05:20:39 -07:00
|
|
|
|
//TODO: check logic in MicrosoftSql.node.ts
|
2023-08-01 06:32:33 -07:00
|
|
|
|
|
2023-07-31 05:20:39 -07:00
|
|
|
|
return result as any;
|
2020-07-08 01:00:13 -07:00
|
|
|
|
}
|
2023-01-24 02:32:31 -08:00
|
|
|
|
|
2024-06-20 03:09:23 -07:00
|
|
|
|
/**
|
|
|
|
|
* Compares the values of specified keys in two objects.
|
|
|
|
|
*
|
|
|
|
|
* @param {T} obj1 - The first object to compare.
|
|
|
|
|
* @param {T} obj2 - The second object to compare.
|
|
|
|
|
* @param {string[]} keys - An array of keys to compare.
|
|
|
|
|
* @param {boolean} disableDotNotation - Whether to use dot notation to access nested properties.
|
|
|
|
|
* @returns {boolean} - Whether the values of the specified keys are equal in both objects.
|
|
|
|
|
*/
|
|
|
|
|
export const compareItems = <T extends { json: Record<string, unknown> }>(
|
|
|
|
|
obj1: T,
|
|
|
|
|
obj2: T,
|
|
|
|
|
keys: string[],
|
|
|
|
|
disableDotNotation: boolean = false,
|
|
|
|
|
): boolean => {
|
|
|
|
|
let result = true;
|
|
|
|
|
for (const key of keys) {
|
|
|
|
|
if (!disableDotNotation) {
|
|
|
|
|
if (!isEqual(get(obj1.json, key), get(obj2.json, key))) {
|
|
|
|
|
result = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (!isEqual(obj1.json[key], obj2.json[key])) {
|
|
|
|
|
result = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-24 02:32:31 -08:00
|
|
|
|
export function updateDisplayOptions(
|
|
|
|
|
displayOptions: IDisplayOptions,
|
|
|
|
|
properties: INodeProperties[],
|
|
|
|
|
) {
|
|
|
|
|
return properties.map((nodeProperty) => {
|
|
|
|
|
return {
|
|
|
|
|
...nodeProperty,
|
|
|
|
|
displayOptions: merge({}, nodeProperty.displayOptions, displayOptions),
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-02-21 05:42:00 -08:00
|
|
|
|
|
2023-05-02 02:44:25 -07:00
|
|
|
|
export function processJsonInput<T>(jsonData: T, inputName?: string) {
|
|
|
|
|
let values;
|
2023-11-09 08:50:59 -08:00
|
|
|
|
const input = inputName ? `'${inputName}' ` : '';
|
2023-05-02 02:44:25 -07:00
|
|
|
|
|
|
|
|
|
if (typeof jsonData === 'string') {
|
|
|
|
|
try {
|
|
|
|
|
values = jsonParse(jsonData);
|
|
|
|
|
} catch (error) {
|
2023-12-05 02:17:08 -08:00
|
|
|
|
throw new ApplicationError(`Input ${input} must contain a valid JSON`, { level: 'warning' });
|
2023-05-02 02:44:25 -07:00
|
|
|
|
}
|
|
|
|
|
} else if (typeof jsonData === 'object') {
|
|
|
|
|
values = jsonData;
|
|
|
|
|
} else {
|
2023-12-05 02:17:08 -08:00
|
|
|
|
throw new ApplicationError(`Input ${input} must contain a valid JSON`, { level: 'warning' });
|
2023-05-02 02:44:25 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return values;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-21 05:42:00 -08:00
|
|
|
|
function isFalsy<T>(value: T) {
|
|
|
|
|
if (isNull(value)) return true;
|
|
|
|
|
if (typeof value === 'string' && value === '') return true;
|
|
|
|
|
if (Array.isArray(value) && value.length === 0) return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const parseStringAndCompareToObject = (str: string, arr: IDataObject) => {
|
|
|
|
|
try {
|
|
|
|
|
const parsedArray = jsonParse(str);
|
|
|
|
|
return isEqual(parsedArray, arr);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const fuzzyCompare = (useFuzzyCompare: boolean, compareVersion = 1) => {
|
|
|
|
|
if (!useFuzzyCompare) {
|
|
|
|
|
//Fuzzy compare is false we do strict comparison
|
|
|
|
|
return <T, U>(item1: T, item2: U) => isEqual(item1, item2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return <T, U>(item1: T, item2: U) => {
|
|
|
|
|
//Both types are the same, so we do strict comparison
|
|
|
|
|
if (!isNull(item1) && !isNull(item2) && typeof item1 === typeof item2) {
|
|
|
|
|
return isEqual(item1, item2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (compareVersion >= 2) {
|
|
|
|
|
//Null, 0 and "0" treated as equal
|
|
|
|
|
if (isNull(item1) && (isNull(item2) || item2 === 0 || item2 === '0')) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isNull(item2) && (isNull(item1) || item1 === 0 || item1 === '0')) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Null, empty strings, empty arrays all treated as the same
|
|
|
|
|
if (isFalsy(item1) && isFalsy(item2)) return true;
|
|
|
|
|
|
|
|
|
|
//When a field is missing in one branch and isFalsy() in another, treat them as matching
|
|
|
|
|
if (isFalsy(item1) && item2 === undefined) return true;
|
|
|
|
|
if (item1 === undefined && isFalsy(item2)) return true;
|
|
|
|
|
|
|
|
|
|
//Compare numbers and strings representing that number
|
|
|
|
|
if (typeof item1 === 'number' && typeof item2 === 'string') {
|
|
|
|
|
return item1.toString() === item2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (typeof item1 === 'string' && typeof item2 === 'number') {
|
|
|
|
|
return item1 === item2.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Compare objects/arrays and their stringified version
|
|
|
|
|
if (!isNull(item1) && typeof item1 === 'object' && typeof item2 === 'string') {
|
|
|
|
|
return parseStringAndCompareToObject(item2, item1 as IDataObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isNull(item2) && typeof item1 === 'string' && typeof item2 === 'object') {
|
|
|
|
|
return parseStringAndCompareToObject(item1, item2 as IDataObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Compare booleans and strings representing the boolean (’true’, ‘True’, ‘TRUE’)
|
|
|
|
|
if (typeof item1 === 'boolean' && typeof item2 === 'string') {
|
|
|
|
|
if (item1 === true && item2.toLocaleLowerCase() === 'true') return true;
|
|
|
|
|
if (item1 === false && item2.toLocaleLowerCase() === 'false') return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (typeof item2 === 'boolean' && typeof item1 === 'string') {
|
|
|
|
|
if (item2 === true && item1.toLocaleLowerCase() === 'true') return true;
|
|
|
|
|
if (item2 === false && item1.toLocaleLowerCase() === 'false') return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Compare booleans and the numbers/string 0 and 1
|
|
|
|
|
if (typeof item1 === 'boolean' && typeof item2 === 'number') {
|
|
|
|
|
if (item1 === true && item2 === 1) return true;
|
|
|
|
|
if (item1 === false && item2 === 0) return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (typeof item2 === 'boolean' && typeof item1 === 'number') {
|
|
|
|
|
if (item2 === true && item1 === 1) return true;
|
|
|
|
|
if (item2 === false && item1 === 0) return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (typeof item1 === 'boolean' && typeof item2 === 'string') {
|
|
|
|
|
if (item1 === true && item2 === '1') return true;
|
|
|
|
|
if (item1 === false && item2 === '0') return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (typeof item2 === 'boolean' && typeof item1 === 'string') {
|
|
|
|
|
if (item2 === true && item1 === '1') return true;
|
|
|
|
|
if (item2 === false && item1 === '0') return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return isEqual(item1, item2);
|
|
|
|
|
};
|
|
|
|
|
};
|
2023-04-27 03:36:02 -07:00
|
|
|
|
|
2023-05-02 02:44:25 -07:00
|
|
|
|
export function wrapData(data: IDataObject | IDataObject[]): INodeExecutionData[] {
|
|
|
|
|
if (!Array.isArray(data)) {
|
|
|
|
|
return [{ json: data }];
|
|
|
|
|
}
|
|
|
|
|
return data.map((item) => ({
|
|
|
|
|
json: item,
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-27 03:36:02 -07:00
|
|
|
|
export const keysToLowercase = <T>(headers: T) => {
|
|
|
|
|
if (typeof headers !== 'object' || Array.isArray(headers) || headers === null) return headers;
|
|
|
|
|
return Object.entries(headers).reduce((acc, [key, value]) => {
|
2023-07-31 05:20:39 -07:00
|
|
|
|
acc[key.toLowerCase()] = value as IDataObject;
|
2023-04-27 03:36:02 -07:00
|
|
|
|
return acc;
|
|
|
|
|
}, {} as IDataObject);
|
|
|
|
|
};
|
2023-06-22 07:47:28 -07:00
|
|
|
|
|
2023-08-09 04:30:53 -07:00
|
|
|
|
/**
|
|
|
|
|
* Formats a private key by removing unnecessary whitespace and adding line breaks.
|
|
|
|
|
* @param privateKey - The private key to format.
|
|
|
|
|
* @returns The formatted private key.
|
|
|
|
|
*/
|
2024-04-10 03:16:48 -07:00
|
|
|
|
export function formatPrivateKey(privateKey: string, keyIsPublic = false): string {
|
|
|
|
|
let regex = /(PRIVATE KEY|CERTIFICATE)/;
|
|
|
|
|
if (keyIsPublic) {
|
|
|
|
|
regex = /(PUBLIC KEY)/;
|
|
|
|
|
}
|
2023-08-14 07:41:37 -07:00
|
|
|
|
if (!privateKey || /\n/.test(privateKey)) {
|
2023-08-09 04:30:53 -07:00
|
|
|
|
return privateKey;
|
|
|
|
|
}
|
|
|
|
|
let formattedPrivateKey = '';
|
|
|
|
|
const parts = privateKey.split('-----').filter((item) => item !== '');
|
|
|
|
|
parts.forEach((part) => {
|
|
|
|
|
if (regex.test(part)) {
|
|
|
|
|
formattedPrivateKey += `-----${part}-----`;
|
|
|
|
|
} else {
|
|
|
|
|
const passRegex = /Proc-Type|DEK-Info/;
|
|
|
|
|
if (passRegex.test(part)) {
|
|
|
|
|
part = part.replace(/:\s+/g, ':');
|
2023-08-09 08:23:35 -07:00
|
|
|
|
formattedPrivateKey += part.replace(/\\n/g, '\n').replace(/\s+/g, '\n');
|
2023-08-09 04:30:53 -07:00
|
|
|
|
} else {
|
2023-08-09 08:23:35 -07:00
|
|
|
|
formattedPrivateKey += part.replace(/\\n/g, '\n').replace(/\s+/g, '\n');
|
2023-08-09 04:30:53 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return formattedPrivateKey;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-22 07:47:28 -07:00
|
|
|
|
/**
|
|
|
|
|
* @TECH_DEBT Explore replacing with handlebars
|
|
|
|
|
*/
|
|
|
|
|
export function getResolvables(expression: string) {
|
|
|
|
|
if (!expression) return [];
|
|
|
|
|
|
|
|
|
|
const resolvables = [];
|
|
|
|
|
const resolvableRegex = /({{[\s\S]*?}})/g;
|
|
|
|
|
|
|
|
|
|
let match;
|
|
|
|
|
|
|
|
|
|
while ((match = resolvableRegex.exec(expression)) !== null) {
|
|
|
|
|
if (match[1]) {
|
|
|
|
|
resolvables.push(match[1]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resolvables;
|
|
|
|
|
}
|
2023-09-06 02:58:00 -07:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Flattens an object with deep data
|
|
|
|
|
*
|
|
|
|
|
* @param {IDataObject} data The object to flatten
|
|
|
|
|
*/
|
|
|
|
|
export function flattenObject(data: IDataObject) {
|
|
|
|
|
const returnData: IDataObject = {};
|
|
|
|
|
for (const key1 of Object.keys(data)) {
|
|
|
|
|
if (data[key1] !== null && typeof data[key1] === 'object') {
|
|
|
|
|
if (data[key1] instanceof Date) {
|
|
|
|
|
returnData[key1] = data[key1]?.toString();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
const flatObject = flattenObject(data[key1] as IDataObject);
|
|
|
|
|
for (const key2 in flatObject) {
|
|
|
|
|
if (flatObject[key2] === undefined) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
returnData[`${key1}.${key2}`] = flatObject[key2];
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
returnData[key1] = data[key1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return returnData;
|
|
|
|
|
}
|
2023-10-06 08:04:33 -07:00
|
|
|
|
|
|
|
|
|
/**
|
2023-11-08 06:11:23 -08:00
|
|
|
|
* Capitalizes the first letter of a string
|
2023-10-06 08:04:33 -07:00
|
|
|
|
*
|
2023-11-08 06:11:23 -08:00
|
|
|
|
* @param {string} string The string to capitalize
|
2023-10-06 08:04:33 -07:00
|
|
|
|
*/
|
2023-11-08 06:11:23 -08:00
|
|
|
|
export function capitalize(str: string): string {
|
|
|
|
|
if (!str) return str;
|
|
|
|
|
|
|
|
|
|
const chars = str.split('');
|
|
|
|
|
chars[0] = chars[0].toUpperCase();
|
|
|
|
|
|
|
|
|
|
return chars.join('');
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-06 08:04:33 -07:00
|
|
|
|
export function generatePairedItemData(length: number): IPairedItemData[] {
|
|
|
|
|
return Array.from({ length }, (_, item) => ({
|
|
|
|
|
item,
|
|
|
|
|
}));
|
|
|
|
|
}
|
2023-10-10 08:36:20 -07:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Output Paired Item Data Array
|
|
|
|
|
*
|
|
|
|
|
* @param {number | IPairedItemData | IPairedItemData[] | undefined} pairedItem
|
|
|
|
|
*/
|
|
|
|
|
export function preparePairedItemDataArray(
|
|
|
|
|
pairedItem: number | IPairedItemData | IPairedItemData[] | undefined,
|
|
|
|
|
): IPairedItemData[] {
|
|
|
|
|
if (pairedItem === undefined) return [];
|
|
|
|
|
if (typeof pairedItem === 'number') return [{ item: pairedItem }];
|
|
|
|
|
if (Array.isArray(pairedItem)) return pairedItem;
|
|
|
|
|
return [pairedItem];
|
|
|
|
|
}
|
2024-01-26 03:51:03 -08:00
|
|
|
|
|
2024-06-20 03:09:23 -07:00
|
|
|
|
export const sanitizeDataPathKey = (item: IDataObject, key: string) => {
|
2024-01-26 03:51:03 -08:00
|
|
|
|
if (item[key] !== undefined) {
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
(key.startsWith("['") && key.endsWith("']")) ||
|
|
|
|
|
(key.startsWith('["') && key.endsWith('"]'))
|
|
|
|
|
) {
|
|
|
|
|
key = key.slice(2, -2);
|
|
|
|
|
if (item[key] !== undefined) {
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return key;
|
|
|
|
|
};
|