Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prettify the regroup code #1049

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
};