DEV Community


Discussion on: Convert to 2 functions to one function

dinsmoredesign profile image
Derek D

You could do something like this for your first function, then just combine your options for the second function into the map/set, assuming those options aren't available as from options:

const getFromTypeOptionKey = (fromType, fromMetaType) => {

    const metaMap = new Map([
        [ 'W2', ['W'] ],
        [ 'A1', ['A'] ],
        [ 'I1', ['C'] ],
        [ 'L', ['C'] ],
        [ 'CH', ['C'] ],
        [ 'C1', ['C'] ],
        [ 'AT', ['C'] ]

    if (fromType === 'E') { return metaMap.get(fromMetaType) || [ 'A', 'W', 'C', 'I', 'CK' ] };
    if (fromType === 'W1') { return ['W'] };

    const typeSet = new Set(['A', 'I', 'W', 'R']);

    return typeSet.has(fromType) ? [ fromType ] : [];


P.S. I hate switch statements, IMO this is a much easier to read solution if you understand Maps/Sets ;) It's also half the amount of code (less if you combine the options from the second function into it).

jmjjeena profile image
J E E N A R J U N Author

Thanks Derek :) I will test it out and let you know how it goes.

dinsmoredesign profile image
Derek D

Sure thing! My original idea was actually to just take any of your strings (assuming it's not an option that returns C or the large array from the default of E) and remove any index after 0 and return that inside an array, as something like W, W1 and W2 all map to the same value, but I don't know what the rest of your data looks like in order to do this.

P.S. Sorry you got downvoted on SO, people on there are so dumb :(