REST metadata fields generation

Posted on

Problem

I wrote a code to fetch the data present and store it in Array format but I thing I have wrote code multiple times can It be possible to minimize the code as its too long

let topicsValue = ["requiredType.*", "Entry.*", "token.*", "RestAPI.*"];

let Topic = [],
  rest = ["required", "unrequired"],
  final = ["createInput", "mustPossible", "finalOutput"];
topicsValue.map((data) => {
  let requiredType, entries, token, restAPI;
  if (data.split(".")[1].includes("*")) {
    if (data.split(".")[0].includes("requiredType")) {
      for (const value of final) {
        requiredType = data
          .split(".")[0]
          .replace("requiredType", "required_type")
          .concat(`.${value}`);
        Topic.push(requiredType);
      }
    }
    if (data.split(".")[0].includes("Entry")) {
      for (const value of final) {
        entries = data
          .split(".")[0]
          .replace("Entry", "entries")
          .concat(`.${value}`);
        Topic.push(entries);
      }
      for (const value of rest) {
        entries = data
          .split(".")[0]
          .replace("Entry", "entries")
          .concat(`.${value}`);
        Topic.push(entries);
      }
    }
    if (data.split(".")[0].includes("token")) {
      for (const value of final) {
        token = data
          .split(".")[0]
          .replace("token", "tokens")
          .concat(`.${value}`);
        Topic.push(token);
      }
      for (const value of rest) {
        token = data
          .split(".")[0]
          .replace("token", "tokens")
          .concat(`.${value}`);
        Topic.push(token);
      }
    }
    if (
      data.split(".")[0].includes("RestAPI") &&
      !data.split(".")[0].includes("RestAPIAction")
    ) {
      restAPI = data
        .split(".")[0]
        .replace("RestAPI", "restAPI")
        .concat(`.deploy`);
      Topic.push(restAPI);
    }
  } else {
    if (data.split(".")[0].includes("requiredType")) {
      if (!rest.includes(data.split(".")[1])) {
        requiredType = data
          .split(".")[0]
          .replace("requiredType", "required_type")
          .concat(`.${data.split(".")[1]}`);
        Topic.push(requiredType);
      }
    }
    if (data.split(".")[0].includes("Entry")) {
      if (rest.includes(data.split(".")[1])) {
        entries = data
          .split(".")[0]
          .replace("Entry", "entries")
          .concat(`.${data.split(".")[1]}`);
        Topic.push(entries);
      } else {
        entries = data
          .split(".")[0]
          .replace("Entry", "entries")
          .concat(`.${data.split(".")[1]}`);
        Topic.push(entries);
      }
    }
    if (data.split(".")[0].includes("token")) {
      if (rest.includes(data.split(".")[1])) {
        token = data
          .split(".")[0]
          .replace("token", "tokens")
          .concat(`.${data.split(".")[1]}`);
        Topic.push(token);
      } else {
        token = data
          .split(".")[0]
          .replace("token", "tokens")
          .concat(`.${data.split(".")[1]}`);
        Topic.push(token);
      }
    }
    if (
      data.split(".")[0].includes("RestAPI") &&
      !data.split(".")[0].includes("RestAPIAction")
    ) {
      restAPI = data
        .split(".")[0]
        .replace("RestAPI", "restAPI")
        .concat(`.deploy`);
      Topic.push(restAPI);
    }
  }
});

console.log(Topic);

Is there any possible way I can reduce the code without effecting the output

As the requirement of the code is like if the topicValue contain * or the other value so I wrote this long code and now I am trying to minimize the code so its look short and effective.

Solution

From a short review;

  • jshint shows zero issues, congratulations 😉
  • topicsValue should be const
  • topicsValue could be topicValues
  • managing entries without a wildcard seems way too complicated
  • a lot of things are copy-pasted instead of declared once (like the mapping)
  • a few more things could be const instead of let (rest, final)

Mandatory rewrite;

const topicValues = ['requiredType.*', 'Entry.*', 'token.*', 'RestAPI.*', 'RestAPIAction.Example'];

function createTopics(topicValues){
  
  const basic = ['createInput', 'mustPossible', 'finalOutput'];
  const extra = ['required', 'unrequired'];
  let topics = [];

  const maps = {
      requiredType: { to: 'required_type', values: basic }, 
      Entry: { to: 'entries', values: basic.concat(extra) }, 
      token: { to: 'tokens', values: basic.concat(extra) }, 
      RestAPI: { to: 'restAPI', values: ['deploy'] }, 
  }

  for(const topicValue of topicValues){
    const parts = topicValue.split(".");
    const topic = parts[0];
    const value = parts[1];
    if(value === "*"){
      topics = topics.concat(maps[topic].values.map(value=>`${maps[topic].to}.${value}`));
    }else{
      topics.push(topicValue);
    }
  }
  

  return topics;
}

console.log(createTopics(topicValues));

Leave a Reply

Your email address will not be published. Required fields are marked *