Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use string "includes()" in switch Javascript case

In Javascript, is there a way to achieve something similar to this ?

const databaseObjectID = "someId"; // like "product/217637"

switch(databaseObjectID) {
    case includes('product'): actionOnProduct(databaseObjectID); break;
    case includes('user'): actionOnUser(databaseObjectID); break;
    // .. a long list of different object types
}

This is more a curiosity question to understand the possibilities of switch / case, as in this particular case I have solved my problem using const type = databaseObjectID.split('/')[0]; and apply the switch case on type

like image 620
Bertrand Engogram Avatar asked Apr 15 '17 07:04

Bertrand Engogram


People also ask

Can we use string in switch case in JavaScript?

The switch case statement is a multiway branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression. Explanation: expression can be of type numbers or strings.

Can you use includes on a string JavaScript?

Definition and UsageThe includes() method returns true if a string contains a specified string. Otherwise it returns false . The includes() method is case sensitive.

Can you use strings with switch statements?

Yes, we can use a switch statement with Strings in Java.

How do you match a string in a switch case?

To ensure that we have a match in a case clause, we will test the original str value (that is provided to the switch statement) against the input property of a successful match . input is a static property of regular expressions that contains the original input string. When match fails it returns null .


4 Answers

This will work, but it shouldn't be used in practice.

const databaseObjectID = "someId"; // like "product/217637"

switch(true) {
    case databaseObjectID.includes('product'): actionOnProduct(databaseObjectID); break;
    case databaseObjectID.includes('user'): actionOnUser(databaseObjectID); break;
    // .. a long list of different object types
}
like image 102
sheunis Avatar answered Oct 18 '22 05:10

sheunis


You usage would be considered an abuse of case.

Instead just use ifs

     if (databaseObjectId.includes('product')) actionOnProduct(databaseObjectID); 
else if (databaseObjectId.includes('user'))    actionOnUser(databaseObjectID); 
// .. a long list of different object types

If the ObjectId contains static content around the product or user, you can remove it and use the user or product as a key:

var actions = {
  "product":actionOnProduct,
  "user"   :actionOnUser
}

actions[databaseObjectId.replace(/..../,"")](databaseObjectId);
like image 35
mplungjan Avatar answered Oct 18 '22 05:10

mplungjan


Sorry, I'm a noob so someone will probably have to clean this up, but here is the idea. Pass to a function to check and return a category then use the switch.

function classify(string){
  var category = categorize(string);
  switch (category) {
    case 'product':
      console.log('this is a product');
      break;
    case 'user':
      console.log('this is a user');
      break;
    default:
      console.log('category undefined');    
  }
}

function categorize(string){
  if (string.includes('product')){
    return 'product';
  }
  if (string.includes('user')){
    return 'user';
  }
}

classify("product789");
classify("user123");
classify("test567");

Sorry, as well, for not matching your example.

like image 21
Lou Bagel Avatar answered Oct 18 '22 04:10

Lou Bagel


Question:

use string “includes()” in switch Javascript case

While the includes() method will work, it is case sensitive, and just matches any characters. I have found a Regex solution that I like much better, and provides a lot of flexibility. For example, you could easily change this to match only WORDS.

var sourceStr = 'Some Text and literaltextforcase2 and more text'

switch (true)  {  // sourceStr

  case (/LiteralTextForCase1/i.test(sourceStr)):
      console.log('Case 1');
      break;

  case (/LiteralTextForCase2/i.test(sourceStr)):
    console.log('Case 2');
    break;

  default:
      console.log('ERROR No Case provided for: ' + sourceStr);
};

//-->Case 2
like image 31
JMichaelTX Avatar answered Oct 18 '22 03:10

JMichaelTX