UNPKG

@nodescript/stdlib

Version:
59 lines (58 loc) 1.57 kB
export const module = { version: '1.2.2', moduleName: 'Flow / Scan', description: 'Executes a subgraph for each array item. The subgraph decides whether to continue iterating or not and what result to return.', keywords: ['loop', 'find', 'iterate'], params: { array: { schema: { type: 'array', items: { type: 'any' }, }, }, scope: { schema: { type: 'object', properties: {}, additionalProperties: { type: 'any' }, } }, }, result: { async: true, schema: { type: 'any', }, }, subgraph: { input: { item: { type: 'any' }, index: { type: 'number' }, }, output: { type: 'object', properties: { resume: { type: 'boolean' }, result: { type: 'any' }, additionalProperties: { type: 'any' }, }, }, }, }; export const compute = async (params, ctx, subgraph) => { const { array } = params; const scope = { ...params.scope }; for (let index = 0; index < array.length; index++) { const item = array[index]; const { resume, result, ...newScope } = await subgraph({ item, index, ...scope, }, ctx.newScope()); if (!resume) { return result; } Object.assign(scope, newScope); } return null; };