mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-13 05:47:31 -08:00
refactor(Item Lists Node): Delete duplicate code for sorting via code (no-changelog) (#7019)
This commit is contained in:
parent
d6e1cf232f
commit
f02f6b659a
|
@ -1,6 +1,3 @@
|
||||||
import type { NodeVMOptions } from 'vm2';
|
|
||||||
import { NodeVM } from 'vm2';
|
|
||||||
|
|
||||||
import type {
|
import type {
|
||||||
IDataObject,
|
IDataObject,
|
||||||
IExecuteFunctions,
|
IExecuteFunctions,
|
||||||
|
@ -61,6 +58,7 @@ const shuffleArray = (array: any[]) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
import * as summarize from './summarize.operation';
|
import * as summarize from './summarize.operation';
|
||||||
|
import { sortByCode } from '../V3/helpers/utils';
|
||||||
|
|
||||||
export class ItemListsV1 implements INodeType {
|
export class ItemListsV1 implements INodeType {
|
||||||
description: INodeTypeDescription;
|
description: INodeTypeDescription;
|
||||||
|
@ -1369,36 +1367,7 @@ return 0;`,
|
||||||
return result;
|
return result;
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
const code = this.getNodeParameter('code', 0) as string;
|
newItems = sortByCode.call(this, newItems);
|
||||||
const regexCheck = /\breturn\b/g.exec(code);
|
|
||||||
|
|
||||||
if (regexCheck?.length) {
|
|
||||||
const sandbox = {
|
|
||||||
newItems,
|
|
||||||
};
|
|
||||||
const mode = this.getMode();
|
|
||||||
const options = {
|
|
||||||
console: mode === 'manual' ? 'redirect' : 'inherit',
|
|
||||||
sandbox,
|
|
||||||
};
|
|
||||||
const vm = new NodeVM(options as unknown as NodeVMOptions);
|
|
||||||
|
|
||||||
newItems = await vm.run(
|
|
||||||
`
|
|
||||||
module.exports = async function() {
|
|
||||||
newItems.sort( (a,b) => {
|
|
||||||
${code}
|
|
||||||
})
|
|
||||||
return newItems;
|
|
||||||
}()`,
|
|
||||||
__dirname,
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
throw new NodeOperationError(
|
|
||||||
this.getNode(),
|
|
||||||
"Sort code doesn't return. Please add a 'return' statement to your code",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return this.prepareOutputData(newItems);
|
return this.prepareOutputData(newItems);
|
||||||
} else if (operation === 'limit') {
|
} else if (operation === 'limit') {
|
||||||
|
|
|
@ -1,6 +1,3 @@
|
||||||
import type { NodeVMOptions } from 'vm2';
|
|
||||||
import { NodeVM } from 'vm2';
|
|
||||||
|
|
||||||
import type {
|
import type {
|
||||||
IDataObject,
|
IDataObject,
|
||||||
IExecuteFunctions,
|
IExecuteFunctions,
|
||||||
|
@ -61,6 +58,7 @@ const shuffleArray = (array: any[]) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
import * as summarize from './summarize.operation';
|
import * as summarize from './summarize.operation';
|
||||||
|
import { sortByCode } from '../V3/helpers/utils';
|
||||||
|
|
||||||
export class ItemListsV2 implements INodeType {
|
export class ItemListsV2 implements INodeType {
|
||||||
description: INodeTypeDescription;
|
description: INodeTypeDescription;
|
||||||
|
@ -1409,36 +1407,7 @@ return 0;`,
|
||||||
return result;
|
return result;
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
const code = this.getNodeParameter('code', 0) as string;
|
newItems = sortByCode.call(this, newItems);
|
||||||
const regexCheck = /\breturn\b/g.exec(code);
|
|
||||||
|
|
||||||
if (regexCheck?.length) {
|
|
||||||
const sandbox = {
|
|
||||||
newItems,
|
|
||||||
};
|
|
||||||
const mode = this.getMode();
|
|
||||||
const options = {
|
|
||||||
console: mode === 'manual' ? 'redirect' : 'inherit',
|
|
||||||
sandbox,
|
|
||||||
};
|
|
||||||
const vm = new NodeVM(options as unknown as NodeVMOptions);
|
|
||||||
|
|
||||||
newItems = await vm.run(
|
|
||||||
`
|
|
||||||
module.exports = async function() {
|
|
||||||
newItems.sort( (a,b) => {
|
|
||||||
${code}
|
|
||||||
})
|
|
||||||
return newItems;
|
|
||||||
}()`,
|
|
||||||
__dirname,
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
throw new NodeOperationError(
|
|
||||||
this.getNode(),
|
|
||||||
"Sort code doesn't return. Please add a 'return' statement to your code",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return this.prepareOutputData(newItems);
|
return this.prepareOutputData(newItems);
|
||||||
} else if (operation === 'limit') {
|
} else if (operation === 'limit') {
|
||||||
|
|
|
@ -7,15 +7,12 @@ import type {
|
||||||
import { NodeOperationError } from 'n8n-workflow';
|
import { NodeOperationError } from 'n8n-workflow';
|
||||||
import { updateDisplayOptions } from '@utils/utilities';
|
import { updateDisplayOptions } from '@utils/utilities';
|
||||||
|
|
||||||
import type { NodeVMOptions } from 'vm2';
|
|
||||||
import { NodeVM } from 'vm2';
|
|
||||||
|
|
||||||
import get from 'lodash/get';
|
import get from 'lodash/get';
|
||||||
|
|
||||||
import isEqual from 'lodash/isEqual';
|
import isEqual from 'lodash/isEqual';
|
||||||
import lt from 'lodash/lt';
|
import lt from 'lodash/lt';
|
||||||
|
|
||||||
import { shuffleArray } from '../../helpers/utils';
|
import { shuffleArray, sortByCode } from '../../helpers/utils';
|
||||||
import { disableDotNotationBoolean } from '../common.descriptions';
|
import { disableDotNotationBoolean } from '../common.descriptions';
|
||||||
|
|
||||||
const properties: INodeProperties[] = [
|
const properties: INodeProperties[] = [
|
||||||
|
@ -272,36 +269,7 @@ export async function execute(
|
||||||
return result;
|
return result;
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
const code = this.getNodeParameter('code', 0) as string;
|
returnData = sortByCode.call(this, returnData);
|
||||||
const regexCheck = /\breturn\b/g.exec(code);
|
|
||||||
|
|
||||||
if (regexCheck?.length) {
|
|
||||||
const sandbox = {
|
|
||||||
newItems: returnData,
|
|
||||||
};
|
|
||||||
const mode = this.getMode();
|
|
||||||
const options = {
|
|
||||||
console: mode === 'manual' ? 'redirect' : 'inherit',
|
|
||||||
sandbox,
|
|
||||||
};
|
|
||||||
const vm = new NodeVM(options as unknown as NodeVMOptions);
|
|
||||||
|
|
||||||
returnData = await vm.run(
|
|
||||||
`
|
|
||||||
module.exports = async function() {
|
|
||||||
newItems.sort( (a,b) => {
|
|
||||||
${code}
|
|
||||||
})
|
|
||||||
return newItems;
|
|
||||||
}()`,
|
|
||||||
__dirname,
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
throw new NodeOperationError(
|
|
||||||
this.getNode(),
|
|
||||||
"Sort code doesn't return. Please add a 'return' statement to your code",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return returnData;
|
return returnData;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 get from 'lodash/get';
|
||||||
import isEqual from 'lodash/isEqual';
|
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.`,
|
`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} })`);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue