2023-12-08 02:40:05 -08:00
|
|
|
import type {
|
|
|
|
IExecuteFunctions,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
2024-08-29 06:55:53 -07:00
|
|
|
import { NodeConnectionType } from 'n8n-workflow';
|
2023-12-08 02:40:05 -08:00
|
|
|
|
|
|
|
export class Limit implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Limit',
|
|
|
|
name: 'limit',
|
|
|
|
icon: 'file:limit.svg',
|
|
|
|
group: ['transform'],
|
|
|
|
subtitle: '',
|
|
|
|
version: 1,
|
|
|
|
description: 'Restrict the number of items',
|
|
|
|
defaults: {
|
|
|
|
name: 'Limit',
|
|
|
|
},
|
2024-08-29 06:55:53 -07:00
|
|
|
inputs: [NodeConnectionType.Main],
|
|
|
|
outputs: [NodeConnectionType.Main],
|
2023-12-08 02:40:05 -08:00
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Max Items',
|
|
|
|
name: 'maxItems',
|
|
|
|
type: 'number',
|
|
|
|
typeOptions: {
|
|
|
|
minValue: 1,
|
|
|
|
},
|
|
|
|
default: 1,
|
|
|
|
description: 'If there are more items than this number, some are removed',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Keep',
|
|
|
|
name: 'keep',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'First Items',
|
|
|
|
value: 'firstItems',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Last Items',
|
|
|
|
value: 'lastItems',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'firstItems',
|
|
|
|
description: 'When removing items, whether to keep the ones at the start or the ending',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
|
|
|
let returnData = items;
|
|
|
|
const maxItems = this.getNodeParameter('maxItems', 0) as number;
|
|
|
|
const keep = this.getNodeParameter('keep', 0) as string;
|
|
|
|
|
|
|
|
if (maxItems > items.length) {
|
|
|
|
return [returnData];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keep === 'firstItems') {
|
|
|
|
returnData = items.slice(0, maxItems);
|
|
|
|
} else {
|
|
|
|
returnData = items.slice(items.length - maxItems, items.length);
|
|
|
|
}
|
|
|
|
return [returnData];
|
|
|
|
}
|
|
|
|
}
|