From 756fc1e250a831ccddaeb8b6adbccac861479c63 Mon Sep 17 00:00:00 2001 From: Narek Hovhannisyan Date: Fri, 11 Oct 2024 20:12:04 +0400 Subject: [PATCH] refactor(lib): prettify the regroup code --- src/if-run/lib/regroup.ts | 127 +++++++++++++++++--------------------- 1 file changed, 58 insertions(+), 69 deletions(-) diff --git a/src/if-run/lib/regroup.ts b/src/if-run/lib/regroup.ts index 7e39b1c89..55d8f8221 100644 --- a/src/if-run/lib/regroup.ts +++ b/src/if-run/lib/regroup.ts @@ -7,93 +7,82 @@ import {validate} from '../../common/util/validations'; import {STRINGS} from '../config'; const {InvalidGroupingError} = ERRORS; - const {INVALID_GROUP_KEY, REGROUP_ERROR} = STRINGS; /** - * Grouping strategy. + * Creates structure to insert inputs by groups. */ -export const Regroup = ( - inputs: PluginParams[], - outputs: PluginParams[], +const appendGroup = ( + value: PluginParams, + object: any, + target: string, groups: string[] -) => { - /** - * Creates structure to insert inputs by groups. - */ - const appendGroup = ( - value: PluginParams, - object: any, - target: string, - groups: string[] - ) => { - if (groups.length > 0) { - const group = groups.shift() as string; - - object.children = object.children ?? {}; - object.children[group] = object.children[group] ?? {}; +): any => { + if (groups.length === 0) { + object[target] = object[target] || []; + object[target].push(value); + return object; + } - if (groups.length === 0) { - if ( - object.children[group][target] && - object.children[group][target].length > 0 - ) { - object.children[group][target].push(value); - } else { - object.children[group][target] = [value]; - } - } + const group = groups.shift()!; + object.children = object.children || {}; + object.children[group] = object.children[group] || {}; - appendGroup(value, object.children[group], target, groups); - } + return appendGroup(value, object.children[group], target, groups); +}; - return object; - }; +/** + * Validates the groups array. + */ +const validateGroups = (groups: string[]): string[] => { + const inputData = {regroup: groups}; + const validationSchema = z.record( + z.string(), + z.array(z.string(), {message: REGROUP_ERROR}).min(1) + ); - /** - * Validates groups array. - */ - const validateGroups = (regroup: string[]) => { - const inputData = {regroup}; - const validationSchema = z.record( - z.string(), - z.array(z.string(), {message: REGROUP_ERROR}).min(1) - ); + validate(validationSchema, inputData); - validate(validationSchema, inputData); + return groups; +}; - return groups; - }; +/** + * Looks up a group key value in the input. + */ +const lookupGroupKey = (input: PluginParams, groupKey: string): string => { + if (!input[groupKey]) { + throw new InvalidGroupingError(INVALID_GROUP_KEY(groupKey)); + } - /** - * Interates over inputs, grabs group values for each one. - * Based on grouping, initializes the structure. - */ + return input[groupKey]; +}; +/** + * Regroups inputs and outputs based on the given group keys. + */ +export const Regroup = ( + inputs: PluginParams[], + outputs: PluginParams[], + groups: string[] +): any => { const validatedGroups = validateGroups(groups); - const lookupGroupKey = (input: PluginParams, groupKey: string) => { - if (!input[groupKey]) { - throw new InvalidGroupingError(INVALID_GROUP_KEY(groupKey)); + const appendToAccumulator = ( + items: PluginParams[], + acc: any, + target: string + ) => { + for (const item of items) { + const groupsWithData = validatedGroups.map(groupKey => + lookupGroupKey(item, groupKey) + ); + appendGroup(item, acc, target, groupsWithData); } - - return input[groupKey]; }; - let acc = {} as any; - for (const input of inputs) { - const groupsWithData = validatedGroups.map(groupKey => - lookupGroupKey(input, groupKey) - ); - acc = appendGroup(input, acc, 'inputs', groupsWithData); - } - - for (const output of outputs) { - const groupsWithData = validatedGroups.map(groupKey => - lookupGroupKey(output, groupKey) - ); - acc = appendGroup(output, acc, 'outputs', groupsWithData); - } + const acc = {} as any; + appendToAccumulator(inputs, acc, 'inputs'); + appendToAccumulator(outputs, acc, 'outputs'); return acc.children; };