mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
refactor(Item Lists Node): Delete duplicate code for sorting via code (no-changelog) (#7019)
This commit is contained in:
committed by
GitHub
parent
d6e1cf232f
commit
f02f6b659a
@@ -1,4 +1,11 @@
|
||||
import type { IDataObject, INode, INodeExecutionData } from 'n8n-workflow';
|
||||
import { NodeVM } from 'vm2';
|
||||
import {
|
||||
NodeOperationError,
|
||||
type IDataObject,
|
||||
type IExecuteFunctions,
|
||||
type INode,
|
||||
type INodeExecutionData,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import get from 'lodash/get';
|
||||
import isEqual from 'lodash/isEqual';
|
||||
@@ -57,3 +64,26 @@ export const prepareFieldsArray = (fields: string | string[], fieldName = 'Field
|
||||
`The \'${fieldName}\' parameter must be a string of fields separated by commas or an array of strings.`,
|
||||
);
|
||||
};
|
||||
|
||||
const returnRegExp = /\breturn\b/g;
|
||||
|
||||
export function sortByCode(
|
||||
this: IExecuteFunctions,
|
||||
items: INodeExecutionData[],
|
||||
): INodeExecutionData[] {
|
||||
const code = this.getNodeParameter('code', 0) as string;
|
||||
if (!returnRegExp.test(code)) {
|
||||
throw new NodeOperationError(
|
||||
this.getNode(),
|
||||
"Sort code doesn't return. Please add a 'return' statement to your code",
|
||||
);
|
||||
}
|
||||
|
||||
const mode = this.getMode();
|
||||
const vm = new NodeVM({
|
||||
console: mode === 'manual' ? 'redirect' : 'inherit',
|
||||
sandbox: { items },
|
||||
});
|
||||
|
||||
return vm.run(`module.exports = items.sort((a, b) => { ${code} })`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user