mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-18 02:21:13 +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
@@ -12,7 +12,7 @@ import get from 'lodash/get';
|
||||
import { placeholder } from './placeholder';
|
||||
import { getValue } from './utils';
|
||||
import type { IValueData } from './types';
|
||||
import { getResolvables, sanitazeDataPathKey } from '@utils/utilities';
|
||||
import { getResolvables, sanitizeDataPathKey } from '@utils/utilities';
|
||||
|
||||
export const capitalizeHeader = (header: string, capitalize?: boolean) => {
|
||||
if (!capitalize) return header;
|
||||
@@ -516,7 +516,7 @@ export class Html implements INodeType {
|
||||
let htmlArray: string[] | string = [];
|
||||
if (sourceData === 'json') {
|
||||
if (nodeVersion === 1) {
|
||||
const key = sanitazeDataPathKey(item.json, dataPropertyName);
|
||||
const key = sanitizeDataPathKey(item.json, dataPropertyName);
|
||||
if (item.json[key] === undefined) {
|
||||
throw new NodeOperationError(
|
||||
this.getNode(),
|
||||
|
||||
@@ -1,62 +1,22 @@
|
||||
import type {
|
||||
IDataObject,
|
||||
IExecuteFunctions,
|
||||
INode,
|
||||
INodeExecutionData,
|
||||
INodeType,
|
||||
INodeTypeBaseDescription,
|
||||
INodeTypeDescription,
|
||||
} from 'n8n-workflow';
|
||||
import { NodeOperationError, randomInt } from 'n8n-workflow';
|
||||
import { NodeOperationError } from 'n8n-workflow';
|
||||
|
||||
import get from 'lodash/get';
|
||||
import isEmpty from 'lodash/isEmpty';
|
||||
import isEqual from 'lodash/isEqual';
|
||||
import isObject from 'lodash/isObject';
|
||||
import lt from 'lodash/lt';
|
||||
import merge from 'lodash/merge';
|
||||
import pick from 'lodash/pick';
|
||||
import reduce from 'lodash/reduce';
|
||||
import set from 'lodash/set';
|
||||
import unset from 'lodash/unset';
|
||||
|
||||
const compareItems = (
|
||||
obj: INodeExecutionData,
|
||||
obj2: INodeExecutionData,
|
||||
keys: string[],
|
||||
disableDotNotation: boolean,
|
||||
_node: INode,
|
||||
) => {
|
||||
let result = true;
|
||||
for (const key of keys) {
|
||||
if (!disableDotNotation) {
|
||||
if (!isEqual(get(obj.json, key), get(obj2.json, key))) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if (!isEqual(obj.json[key], obj2.json[key])) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
const flattenKeys = (obj: IDataObject, path: string[] = []): IDataObject => {
|
||||
return !isObject(obj)
|
||||
? { [path.join('.')]: obj }
|
||||
: reduce(obj, (cum, next, key) => merge(cum, flattenKeys(next as IDataObject, [...path, key])), {}); //prettier-ignore
|
||||
};
|
||||
|
||||
const shuffleArray = (array: any[]) => {
|
||||
for (let i = array.length - 1; i > 0; i--) {
|
||||
const j = randomInt(i + 1);
|
||||
[array[i], array[j]] = [array[j], array[i]];
|
||||
}
|
||||
};
|
||||
|
||||
import { flattenKeys, shuffleArray, compareItems } from '@utils/utilities';
|
||||
import { sortByCode } from '../V3/helpers/utils';
|
||||
import * as summarize from './summarize.operation';
|
||||
|
||||
@@ -1226,7 +1186,7 @@ return 0;`,
|
||||
const removedIndexes: number[] = [];
|
||||
let temp = newItems[0];
|
||||
for (let index = 1; index < newItems.length; index++) {
|
||||
if (compareItems(newItems[index], temp, keys, disableDotNotation, this.getNode())) {
|
||||
if (compareItems(newItems[index], temp, keys, disableDotNotation)) {
|
||||
removedIndexes.push(newItems[index].json.__INDEX as unknown as number);
|
||||
} else {
|
||||
temp = newItems[index];
|
||||
|
||||
@@ -1,63 +1,23 @@
|
||||
import type {
|
||||
IDataObject,
|
||||
IExecuteFunctions,
|
||||
INode,
|
||||
INodeExecutionData,
|
||||
INodeType,
|
||||
INodeTypeBaseDescription,
|
||||
INodeTypeDescription,
|
||||
IPairedItemData,
|
||||
} from 'n8n-workflow';
|
||||
import { NodeOperationError, deepCopy, randomInt } from 'n8n-workflow';
|
||||
import { NodeOperationError, deepCopy } from 'n8n-workflow';
|
||||
|
||||
import get from 'lodash/get';
|
||||
import isEmpty from 'lodash/isEmpty';
|
||||
import isEqual from 'lodash/isEqual';
|
||||
import isObject from 'lodash/isObject';
|
||||
import lt from 'lodash/lt';
|
||||
import merge from 'lodash/merge';
|
||||
import pick from 'lodash/pick';
|
||||
import reduce from 'lodash/reduce';
|
||||
import set from 'lodash/set';
|
||||
import unset from 'lodash/unset';
|
||||
|
||||
const compareItems = (
|
||||
obj: INodeExecutionData,
|
||||
obj2: INodeExecutionData,
|
||||
keys: string[],
|
||||
disableDotNotation: boolean,
|
||||
_node: INode,
|
||||
) => {
|
||||
let result = true;
|
||||
for (const key of keys) {
|
||||
if (!disableDotNotation) {
|
||||
if (!isEqual(get(obj.json, key), get(obj2.json, key))) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if (!isEqual(obj.json[key], obj2.json[key])) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
const flattenKeys = (obj: IDataObject, path: string[] = []): IDataObject => {
|
||||
return !isObject(obj)
|
||||
? { [path.join('.')]: obj }
|
||||
: reduce(obj, (cum, next, key) => merge(cum, flattenKeys(next as IDataObject, [...path, key])), {}); //prettier-ignore
|
||||
};
|
||||
|
||||
const shuffleArray = (array: any[]) => {
|
||||
for (let i = array.length - 1; i > 0; i--) {
|
||||
const j = randomInt(i + 1);
|
||||
[array[i], array[j]] = [array[j], array[i]];
|
||||
}
|
||||
};
|
||||
|
||||
import { flattenKeys, shuffleArray, compareItems } from '@utils/utilities';
|
||||
import { sortByCode } from '../V3/helpers/utils';
|
||||
import * as summarize from './summarize.operation';
|
||||
|
||||
@@ -1273,7 +1233,7 @@ return 0;`,
|
||||
const removedIndexes: number[] = [];
|
||||
let temp = newItems[0];
|
||||
for (let index = 1; index < newItems.length; index++) {
|
||||
if (compareItems(newItems[index], temp, keys, disableDotNotation, this.getNode())) {
|
||||
if (compareItems(newItems[index], temp, keys, disableDotNotation)) {
|
||||
removedIndexes.push(newItems[index].json.__INDEX as unknown as number);
|
||||
} else {
|
||||
temp = newItems[index];
|
||||
|
||||
@@ -6,9 +6,9 @@ import isEqual from 'lodash/isEqual';
|
||||
import lt from 'lodash/lt';
|
||||
import pick from 'lodash/pick';
|
||||
|
||||
import { compareItems, flattenKeys, prepareFieldsArray, typeToNumber } from '../../helpers/utils';
|
||||
import { compareItems, flattenKeys, updateDisplayOptions } from '@utils/utilities';
|
||||
import { prepareFieldsArray, typeToNumber } from '../../helpers/utils';
|
||||
import { disableDotNotationBoolean } from '../common.descriptions';
|
||||
import { updateDisplayOptions } from '@utils/utilities';
|
||||
|
||||
const properties: INodeProperties[] = [
|
||||
{
|
||||
@@ -229,7 +229,7 @@ export async function execute(
|
||||
const removedIndexes: number[] = [];
|
||||
let temp = newItems[0];
|
||||
for (let index = 1; index < newItems.length; index++) {
|
||||
if (compareItems(newItems[index], temp, keys, disableDotNotation, this.getNode())) {
|
||||
if (compareItems(newItems[index], temp, keys, disableDotNotation)) {
|
||||
removedIndexes.push(newItems[index].json.__INDEX as unknown as number);
|
||||
} else {
|
||||
temp = newItems[index];
|
||||
|
||||
@@ -11,9 +11,9 @@ import get from 'lodash/get';
|
||||
import isEqual from 'lodash/isEqual';
|
||||
import lt from 'lodash/lt';
|
||||
|
||||
import { shuffleArray, sortByCode } from '../../helpers/utils';
|
||||
import { sortByCode } from '../../helpers/utils';
|
||||
import { disableDotNotationBoolean } from '../common.descriptions';
|
||||
import { updateDisplayOptions } from '@utils/utilities';
|
||||
import { shuffleArray, updateDisplayOptions } from '@utils/utilities';
|
||||
|
||||
const properties: INodeProperties[] = [
|
||||
{
|
||||
|
||||
@@ -1,56 +1,11 @@
|
||||
import { NodeVM } from '@n8n/vm2';
|
||||
import type {
|
||||
IDataObject,
|
||||
IExecuteFunctions,
|
||||
IBinaryData,
|
||||
INode,
|
||||
INodeExecutionData,
|
||||
GenericValue,
|
||||
} from 'n8n-workflow';
|
||||
import { ApplicationError, NodeOperationError, randomInt } from 'n8n-workflow';
|
||||
|
||||
import get from 'lodash/get';
|
||||
import isEqual from 'lodash/isEqual';
|
||||
import isObject from 'lodash/isObject';
|
||||
import merge from 'lodash/merge';
|
||||
import reduce from 'lodash/reduce';
|
||||
|
||||
export const compareItems = (
|
||||
obj: INodeExecutionData,
|
||||
obj2: INodeExecutionData,
|
||||
keys: string[],
|
||||
disableDotNotation: boolean,
|
||||
_node: INode,
|
||||
) => {
|
||||
let result = true;
|
||||
for (const key of keys) {
|
||||
if (!disableDotNotation) {
|
||||
if (!isEqual(get(obj.json, key), get(obj2.json, key))) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if (!isEqual(obj.json[key], obj2.json[key])) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
export const flattenKeys = (obj: IDataObject, path: string[] = []): IDataObject => {
|
||||
return !isObject(obj)
|
||||
? { [path.join('.')]: obj }
|
||||
: reduce(obj, (cum, next, key) => merge(cum, flattenKeys(next as IDataObject, [...path, key])), {}); //prettier-ignore
|
||||
};
|
||||
|
||||
export const shuffleArray = (array: any[]) => {
|
||||
for (let i = array.length - 1; i > 0; i--) {
|
||||
const j = randomInt(i + 1);
|
||||
[array[i], array[j]] = [array[j], array[i]];
|
||||
}
|
||||
};
|
||||
import { ApplicationError, NodeOperationError } from 'n8n-workflow';
|
||||
|
||||
export const prepareFieldsArray = (fields: string | string[], fieldName = 'Fields') => {
|
||||
if (typeof fields === 'string') {
|
||||
|
||||
@@ -18,7 +18,7 @@ import get from 'lodash/get';
|
||||
import set from 'lodash/set';
|
||||
import unset from 'lodash/unset';
|
||||
|
||||
import { getResolvables, sanitazeDataPathKey } from '../../../../utils/utilities';
|
||||
import { getResolvables, sanitizeDataPathKey } from '../../../../utils/utilities';
|
||||
import type { SetNodeOptions } from './interfaces';
|
||||
import { INCLUDE } from './interfaces';
|
||||
|
||||
@@ -38,13 +38,13 @@ const configureFieldHelper = (dotNotation?: boolean) => {
|
||||
} else {
|
||||
return {
|
||||
set: (item: IDataObject, key: string, value: IDataObject) => {
|
||||
item[sanitazeDataPathKey(item, key)] = value;
|
||||
item[sanitizeDataPathKey(item, key)] = value;
|
||||
},
|
||||
get: (item: IDataObject, key: string) => {
|
||||
return item[sanitazeDataPathKey(item, key)];
|
||||
return item[sanitizeDataPathKey(item, key)];
|
||||
},
|
||||
unset: (item: IDataObject, key: string) => {
|
||||
delete item[sanitazeDataPathKey(item, key)];
|
||||
delete item[sanitizeDataPathKey(item, key)];
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -10,7 +10,8 @@ import {
|
||||
type INodeTypeDescription,
|
||||
} from 'n8n-workflow';
|
||||
import { prepareFieldsArray } from '../utils/utils';
|
||||
import { compareItems, flattenKeys, validateInputData } from './utils';
|
||||
import { validateInputData } from './utils';
|
||||
import { compareItems, flattenKeys } from '@utils/utilities';
|
||||
|
||||
export class RemoveDuplicates implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
@@ -211,7 +212,7 @@ export class RemoveDuplicates implements INodeType {
|
||||
const removedIndexes: number[] = [];
|
||||
let temp = newItems[0];
|
||||
for (let index = 1; index < newItems.length; index++) {
|
||||
if (compareItems(newItems[index], temp, keys, disableDotNotation, this.getNode())) {
|
||||
if (compareItems(newItems[index], temp, keys, disableDotNotation)) {
|
||||
removedIndexes.push(newItems[index].json.__INDEX as unknown as number);
|
||||
} else {
|
||||
temp = newItems[index];
|
||||
|
||||
@@ -1,44 +1,5 @@
|
||||
import get from 'lodash/get';
|
||||
import isEqual from 'lodash/isEqual';
|
||||
import isObject from 'lodash/isObject';
|
||||
import merge from 'lodash/merge';
|
||||
import reduce from 'lodash/reduce';
|
||||
import {
|
||||
NodeOperationError,
|
||||
type IDataObject,
|
||||
type INode,
|
||||
type INodeExecutionData,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
export const compareItems = (
|
||||
obj: INodeExecutionData,
|
||||
obj2: INodeExecutionData,
|
||||
keys: string[],
|
||||
disableDotNotation: boolean,
|
||||
_node: INode,
|
||||
) => {
|
||||
let result = true;
|
||||
for (const key of keys) {
|
||||
if (!disableDotNotation) {
|
||||
if (!isEqual(get(obj.json, key), get(obj2.json, key))) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if (!isEqual(obj.json[key], obj2.json[key])) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
export const flattenKeys = (obj: IDataObject, path: string[] = []): IDataObject => {
|
||||
return !isObject(obj)
|
||||
? { [path.join('.')]: obj }
|
||||
: reduce(obj, (cum, next, key) => merge(cum, flattenKeys(next as IDataObject, [...path, key])), {}); //prettier-ignore
|
||||
};
|
||||
import { NodeOperationError, type INode, type INodeExecutionData } from 'n8n-workflow';
|
||||
|
||||
export const validateInputData = (
|
||||
node: INode,
|
||||
|
||||
@@ -9,7 +9,8 @@ import {
|
||||
type INodeType,
|
||||
type INodeTypeDescription,
|
||||
} from 'n8n-workflow';
|
||||
import { shuffleArray, sortByCode } from './utils';
|
||||
import { sortByCode } from './utils';
|
||||
import { shuffleArray } from '@utils/utilities';
|
||||
|
||||
export class Sort implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
|
||||
@@ -1,13 +1,6 @@
|
||||
import { NodeVM } from '@n8n/vm2';
|
||||
import type { IExecuteFunctions, INodeExecutionData } from 'n8n-workflow';
|
||||
import { NodeOperationError, randomInt } from 'n8n-workflow';
|
||||
|
||||
export const shuffleArray = (array: any[]) => {
|
||||
for (let i = array.length - 1; i > 0; i--) {
|
||||
const j = randomInt(i + 1);
|
||||
[array[i], array[j]] = [array[j], array[i]];
|
||||
}
|
||||
};
|
||||
import { NodeOperationError } from 'n8n-workflow';
|
||||
|
||||
const returnRegExp = /\breturn\b/g;
|
||||
export function sortByCode(
|
||||
|
||||
Reference in New Issue
Block a user