mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
refactor(core): Extract duplicate utility functions and add unit tests (no-changelog) (#9814)
This commit is contained in:
committed by
GitHub
parent
283d1ca583
commit
e4463c62b4
@@ -6,9 +6,9 @@ import type {
|
||||
IPairedItemData,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { ApplicationError, jsonParse } from 'n8n-workflow';
|
||||
import { ApplicationError, jsonParse, randomInt } from 'n8n-workflow';
|
||||
|
||||
import { isEqual, isNull, merge } from 'lodash';
|
||||
import { isEqual, isNull, merge, isObject, reduce, get } from 'lodash';
|
||||
|
||||
/**
|
||||
* Creates an array of elements split into groups the length of `size`.
|
||||
@@ -41,6 +41,32 @@ export function chunk<T>(array: T[], size = 1) {
|
||||
return result as T[][];
|
||||
}
|
||||
|
||||
/**
|
||||
* 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])),
|
||||
{},
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Takes a multidimensional array and converts it to a one-dimensional array.
|
||||
*
|
||||
@@ -70,6 +96,38 @@ export function flatten<T>(nestedArray: T[][]) {
|
||||
return result as any;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
};
|
||||
|
||||
export function updateDisplayOptions(
|
||||
displayOptions: IDisplayOptions,
|
||||
properties: INodeProperties[],
|
||||
@@ -330,7 +388,7 @@ export function preparePairedItemDataArray(
|
||||
return [pairedItem];
|
||||
}
|
||||
|
||||
export const sanitazeDataPathKey = (item: IDataObject, key: string) => {
|
||||
export const sanitizeDataPathKey = (item: IDataObject, key: string) => {
|
||||
if (item[key] !== undefined) {
|
||||
return key;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user