I have two functions that are very similar, with some small differences I want to combine:
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;
}
}
Both of the functions are filtering the same object, but different keys (type and functiongroup). Function groups value is modified with regex as the dropdown value includes a index number and dash which is not used in the logic. Example:
1 - myFunctionGroup --regex-->myFunctionGroup
This is not done in the type key. How to combine these two functions?