Following are 2 functions to get the options in the dropdown. I want to make it to one function rather than 2 different function. There are few options different based on fromtype/totype. But most like case A, I, W etc are common for both the functions. How to make the following 2 functions into one function.
I was thinking of a function
const getOptionKey = (type, metaType) => {
switch (type) {
case 'A':
type = ['A'];
break;
case 'W1':
case 'W':
type = ['W'];
break;
case 'I':
type = ['I'];
break;
case 'E':
switch (metaType) {
case 'A':
type = ['A'];
break;
case 'W2':
fromType = ['W'];
break;
default:
fromType = [
'A',
'W',
'C',
'I',
'CK'
];
}
break;
default:
type = [];
}
}
but then I am not able to come up with a better solution how to categorize the switch cases that are not common.
const getFromTypeOptionKey = (fromType, fromMetaType) => {
switch (fromType) {
case 'A':
fromType = ['A'];
break;
case 'I':
fromType = ['I'];
break;
case 'W1':
case 'W':
fromType = ['W'];
break;
case 'R':
fromType = ['R'];
break;
case 'E':
switch (fromMetaType) {
case 'A1':
fromType = ['A'];
break;
case 'W2':
fromType = ['W'];
break;
case 'I1':
case 'L':
case 'CH':
case 'C1':
case 'AT':
fromType = ['C'];
break;
default:
fromType = [
'A',
'W',
'C',
'I',
'CK'
];
}
break;
default:
fromType = [];
}
return fromType;
};
const getToTypeOptionKey = (toType, toMetaType) => {
switch (toType) {
case 'A':
toType = ['A'];
break;
case 'CK':
toType = ['CK'];
break;
case 'I':
toType = ['I'];
break;
case 'W1':
case 'W':
toType = ['W'];
break;
case 'E':
switch (toMetaType) {
case 'A1':
toType = ['A'];
break;
case 'W2':
toType = ['W'];
break;
case 'I1':
case 'CW':
case 'C1':
case 'PD':
case 'PU':
case 'AT':
toType = ['C'];
break;
default:
toType = [
'A',
'W',
'C',
'I',
'CK'
];
}
break;
default:
toType = [];
}
return toType;
};
getToTypeOptionKey
with itstoMetaType
as thetoType
. – Mr. Polywhirl Mar 26 '20 at 18:03