I am faced with a challenge of combining two similar functions that have some minor differences:
private filterByOperationType() {
let operationBuffer: IProductOperation[] = [];
if (this.filterConditions[0].selected.length > 0) {
operationBuffer = this.operations.filter(({ operationDetails }) => {
let operationType = false;
for (const details of this.filterConditions) {
if (details.name === 'Type') {
const parsedGroup = details.selected[0].text;
if (parsedGroup === operationDetails) {
operationType = true;
}
}
}
return operationType;
});
}
return operationBuffer;
}
/** Filter the operations by function group when dropdown is used */
private filterByFunctionGroup() {
const filteredData = this.operations.filter(({ functionDetails }) => {
let groupDescriptionMatch = false;
for (const details of this.filterConditions) {
if (!details.selected[0]) {
return;
}
if (details.name === 'Function group') {
const parsedGroup = details.selected[0].text.match(/[a-zA-Z]+/g);
if (parsedGroup?.join(' ') === functionDetails) {
groupDescriptionMatch = true;
}
}
}
return groupDescriptionMatch;
});
return filteredData;
}
}
Although both functions are filtering the same object, they target different keys (type and function group). The function group value undergoes a regex transformation since the dropdown value includes an index number and dash which is not accounted for in the logic. For example:
1 - myFunctionGroup --regex-->myFunctionGroup
This modification is not applied to the type key. How can these two functions be combined effectively?