hrabal
hrabal

Reputation: 165

How to make the same pattern code more concise

I hope those code can be more concise, but I don't know how to do that.

the function 2~4 has a same pattern, I hope I can reduce the code,

and also if I have too much "or" and "and", is there any method can make it cleaner?



const statusFilter2 = (data1, data2) => {
    if (data1 === 'error' || data2 === 'error') {
        return 'error'
    } else if (data1 === 'C' || C === 'C') {
        return 'A'
    } else if (data1 === 'B' || data2 === 'B') {
        return 'B'
    } else {
        return 'C'
    }
}

const statusFilter3 = (data1, data2, data3) => {

    if (data1 === 'error' || data2 === 'error' || data3 === 'error') {
        return 'error'
    } else if (data1 === 'C' || data2 === 'C' || data3 === 'C') {
        return 'C'
    } else if (data1 === 'B' || data2 === 'B' || data3 === 'B') {
        return 'B'
    } else {
        return 'A'
    }
}


const statusFilter4 = (data1, data2, data3, data4) => {
    if (data1 === 'error' || data2 === 'error' || data3 === 'error' || data4 === 'error') {
        return 'error'
    } else if (data1 === 'C' || data2 === 'C' || data3 === 'C' || data4 === 'C') {
        return 'C'
    } else if (data1 === 'B' || data2 === 'B' || data3 === 'B' || data4 === 'B') {
        return 'B'
    } else {
        return 'A'
    }
}


Upvotes: 1

Views: 89

Answers (5)

Avin Kavish
Avin Kavish

Reputation: 8947

The cleanest way is to have an array of statuses you want to check against ordered by priority based on what you want to return first. In your example, the order seems to be error, c, b, a You can then loop through that array returning that status if a match is found. At the very end, if no matches were found, return the default value, which is a in your case.

// In order of priority
const statuses = [ 'error', 'C', 'B', 'A' ]

const statusFilter = (...args) => {  
  for (let status of statuses) {
     if(args.find(v => v === status))
        return status;
  }
  return 'A'
}

console.log(statusFilter('error', 'B', 'A'))
console.log(statusFilter('C', 'B', 'A'))
console.log(statusFilter('B', 'B', 'A'))
console.log(statusFilter('A', 'A', 'A'))

Upvotes: 0

MH2K9
MH2K9

Reputation: 12039

You can use only one function instead of having three separate functions.

Code steps explanation

  • Step1: Pass Nth number of arguments using spread syntax (in this code it is ...arg) and this arg is an array.
  • Step2: Create an object of all_messages and try to find one key of arg from all_messages using find()
  • Step3: If find from message list, return that keyed value from all_messages.
  • Step4: Set default value A if all_message doesn't include any value of arg.

const status = (...arg) => {
    const all_messages = {error: 'error', C: 'C', B: 'B'};
    const message = arg.find(key => !!all_messages[key])
    return message ? message : 'A'
}

console.log(status('F', 'F'))
console.log(status('C', 'A'))
console.log(status('C', 'C', 'C', 'C'))
console.log(status('error', 'error', 'error', 'error'))
console.log(status('B', 'B', 'B'))

Upvotes: 2

cjmarsh
cjmarsh

Reputation: 292

You could try something like this:

const statusFilter = (...args) => {
  if (args.indexOf('error') !== -1) {
    return 'error';
  } else if (args.indexOf('C') !== -1) {
    return 'C';
  } else if (args.indexOf('B') !== -1) {
    return 'B';
  } else {
    return 'A';
  }
}

Upvotes: 0

pyQueen
pyQueen

Reputation: 241

You could write one function for that instead of three. Something like:

const statusFilter = (dataArray) ...

Compare the values of the Array. If they're all the same, return the value, if not, return A.

Upvotes: 0

Eylon Sultan
Eylon Sultan

Reputation: 1036

just use array as param:

const statusFilters = data => {
  if (Array.isArray(data)) {
    if (data.includes("error")) return "error";
    else if (data.includes("C")) return "C";
    else if (data.includes("B")) return "B";
    else return "A";
  }
};

statusFilters([data1, data2, data3 ...]);

Upvotes: 0

Related Questions