1

How do I make the named array values conditional?

Here in the below code, I have the params array to which I am adding one more named parameter in the if conditions. In the if condition that says INSERT I want to have the userId and the displayName field and in the condition that says REMOVE I only want the userId field.

The whole point is to make the code look cleaner by not repeating the same code twice but have it at one place and make the displayName conditional depending on it is INSERT or REMOVE condition. My actual code has lot more conditions and looks really long repeating it in each of those if conditions.

 var params = {
        secretArn: 'secretArn',
        resourceArn: 'resourceArn',
        database: 'db',
    };

if (record.eventName == 'INSERT') {
        params.parameters = [{
                  name: "userId",
                        value: {
                            "stringValue": userId
                        }
                    },
                    {
                        name: "displayName",
                        value: {
                            "stringValue": displayName
                        }
                    },
                ];

       }
if (record.eventName == 'REMOVE') {
        params.parameters = [{
                  name: "userId",
                        value: {
                            "stringValue": userId
                        }
                    },
                    {
                        name: "displayName",
                        value: {
                            "stringValue": displayName
                        }
                    },
                ];

       }
8
  • To be honest I am am struggling to follow your Q but based on my best guess why don't you call a function for insert and remove. Something like -> if (record.eventName == 'INSERT') {callInsertFunction()} if (record.eventName == 'REMOVE') {callRemoveFunction()}. Within each function you can deal with the parameters as you need to. Commented Mar 18, 2022 at 22:58
  • Sure I can do that but that was not what I meant. Even if it is a function I have to make the displayName field of the parameters array conditional. That is what I am trying to achieve. Commented Mar 18, 2022 at 23:01
  • Sorry still not following. Conditional how? I only see one variable defined which is displayName. Commented Mar 18, 2022 at 23:04
  • The parameters array has userid and displayName. I want to make the displayName value conditional on some if conditions. Commented Mar 18, 2022 at 23:06
  • You already have if INSERT and if REMOVE as if conditions. Are you saying you have other if conditions? How will different values for displayName be determined? Commented Mar 18, 2022 at 23:09

1 Answer 1

1

After thinking about Q, it seems that the core solution should be an implementation of the dynamic dispatch pattern (I could be incorrect in the name of the pattern). Here is a sample solution:

const getUserId = () => Math.ceil(Math.random() * 100);

const getDisplayName = () => {
  const names = ['Sebastian', 'Farrell', 'Artur', 'Geghard', 'Matevos'];
  return names[Math.floor(Math.random() * names.length)];
};

const makeParametr = (name, stringValue) => () => ({
    name,
    value: { stringValue },
});

const dispatcher = {
  INSERT: [
    { func: makeParametr, args: [() => 'userId', getUserId] },
    { func: makeParametr, args: [() => 'displayName', getDisplayName] },
  ],
  REMOVE: [
    { func: makeParametr, args: [() => 'userId', getUserId] },
  ],
};

const getParameters = (eventName) => {
  if (!dispatcher[eventName]) throw new Error(`No such event: ${eventName}`);

  return dispatcher[eventName].map(({ func, args }) => {
    const currentArgs = args.map((arg) => arg());

    return func(...currentArgs)();
  }) 
};

console.log(getParameters('INSERT'));
console.log(getParameters('REMOVE'));
console.log(getParameters('OTHER_EVENT'));
.as-console-wrapper{min-height: 100%!important; top: 0}

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Alexandr. I have to look into this in detail. Looks like this is the way I have to go rewrite.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.