2
\$\begingroup\$

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.

konijn
34.2k5 gold badges70 silver badges267 bronze badges
asked Feb 9, 2022 at 18:26
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

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));

answered Feb 10, 2022 at 16:45
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.