1

I am building a condition script that searches a comma-separated list of email recipients. There can be multiple recipients and the condition script searches for three (3) specific addresses.

This is not a full-fledged script - it can only include a conditional statement. Our issue is that this field has a character limit of 255... and we have reached it.

Here is the current condition:

email.recipients.toLowerCase().indexOf('email_1') >= 0 || email.recipients.toLowerCase().indexOf('email_2') >= 0 || email.recipients.toLowerCase().indexOf('email_3') >= 0

This is within our character limit - and works - but now we need to add another email address to search for. Is there any way of shortening this condition?

2
  • Can't you use regexps??? Should it be on one line (one statement)? Commented Feb 10, 2017 at 15:23
  • 2
    This is not a full-fledged script - it can only include a conditional statement. - can you be more specific about what this means? What is permitted, precisely? Commented Feb 10, 2017 at 15:24

4 Answers 4

1

If you can use ES6 (you don't need to worry about Internet Explorer), you can use an arrow function:

['email_1','email_2','email_3'].some(e=>email.recipients.toLowerCase().indexOf(e)+1);
Sign up to request clarification or add additional context in comments.

Comments

0

Depending on the particular email addresses, a regex solution might be simpler:

/email_[1-3]/i.test(email.recipients)

Comments

0

Not the perfect answer of course, but you can shorten a little bit with using an alias (be careful about var conflicts, because you cannot use the var keyword inside this expression, meaning the var will be global, it also might not work in strict mode i think).

Just made an answer because it was too long for a comment.. hope ite helps.

var email = {}; email.recipients = 'email_1,email_2,email_3';
console.log((a = email.recipients.toLowerCase()) && (a.indexOf('email_1') >= 0 || a.indexOf('email_2') >= 0 || a.indexOf('email_3') >= 0));

Comments

0

use some:-

['email_1', 'email_2', 'email_3'].some(function(e) {
  return email.recipients.toLowerCase().indexOf(e) >= 0;
});

Comments

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.