lundi 30 septembre 2019

If-else Refactor

I am trying to refactor this block of if else statements. What can be the best optimized version of this code?

`const handlePhoneNumberFormat = (rules, value, callback) => {
    if (value && value.match(phoneNumberRegex)) {
      if (value.includes('-')) {
        if (value.indexOf('-') === 2) {
          if (value.length !== 10) {
            callback('Please enter 7 digits after "-".');
          } else {
            callback();
          }
        } else if (value.indexOf('-') === 3) {
          if (value.length !== 14) {
            callback('Please enter 10 digits after "-".');
          } else {
            callback();
          }
        } else {
          callback();
        }
      } else {
        callback();
      }
    } else {
      callback('');
    }
  };`

Aucun commentaire:

Enregistrer un commentaire