Skip to content

Commit

Permalink
Merge pull request #1049 from Green-Software-Foundation/regroup-check
Browse files Browse the repository at this point in the history
Prettify the regroup code
  • Loading branch information
narekhovhannisyan authored Oct 15, 2024
2 parents faa7cfd + 756fc1e commit 3f5f16f
Showing 1 changed file with 58 additions and 69 deletions.
127 changes: 58 additions & 69 deletions src/if-run/lib/regroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

0 comments on commit 3f5f16f

Please sign in to comment.