admin管理员组

文章数量:1391924

I would like to be able to call a function on a switch case statement but I can't seem to figure it out. Example:

switch(message.toLowerCase()) {
    // the startsWith would be an extension of the message.toLowerCase()
    // so that the case would be checking for message.toLowerCase().startsWith("pay")
    case startsWith("pay"):
        console.log(message)
        break
}

I have tried using case.function(), function(case), and case function(), but none of them work. Thanks!

I would like to be able to call a function on a switch case statement but I can't seem to figure it out. Example:

switch(message.toLowerCase()) {
    // the startsWith would be an extension of the message.toLowerCase()
    // so that the case would be checking for message.toLowerCase().startsWith("pay")
    case startsWith("pay"):
        console.log(message)
        break
}

I have tried using case.function(), function(case), and case function(), but none of them work. Thanks!

Share Improve this question edited Jul 20, 2020 at 19:56 Levi_OP asked Jul 20, 2020 at 19:42 Levi_OPLevi_OP 9691 gold badge11 silver badges24 bronze badges 5
  • How did you define startsWith? – Unmitigated Commented Jul 20, 2020 at 19:44
  • @hev1 I didn't. That is a predefined function in js – Levi_OP Commented Jul 20, 2020 at 19:48
  • 2 Did you mean String#startsWith? – Unmitigated Commented Jul 20, 2020 at 19:49
  • This should work as long as your function returns a bool... – Herman Neple Commented Jul 20, 2020 at 19:50
  • @hev1 Yes. I forgot to specify that the startsWith() would be an extension of the message.toLowerCase() that is in the switch statement. – Levi_OP Commented Jul 20, 2020 at 20:01
Add a ment  | 

2 Answers 2

Reset to default 6

Switch statements in JavaScript don't support pattern matching, they only do simple equality checks (the result of lowerCase() gets pared to the return value of startsWith(...) [if that function would exist]). You could do something like this though:

switch(true) {
  case message.toLowerCase().startsWith("pay"): // if this is true, the case matches
    console.log(message);
    break;
 }

You could also write some helpers to achieve more flexible pattern matching:

  const match = (...patterns) => value=> patterns.find(p => p.match(value))(value);
   const pattern = match => fn => Object.assign(fn, { match });
  const startWith = a => pattern(v => v.startsWith(a));

 match(
    startsWith("b")(console.error),
    startsWith("a")(console.log),
    startsWith("a")(console.error)
 )("abc")

One option is to use switch(true).

var m = message.toLowerCase()
switch(true) {
    case m.startsWith("pay"):
        console.log(message)
        break
}

Read the original thread for more details (e.g. that the case return value must be truely true, e.g. 1 would not work (but would work for if-else.

Also consider to use ordinary if-else, sometimes it might be more readable and maintainable than switch (and especially than this "non-standard" swith(true))

Also consider using regular expression. From the OP it is not really clear what you are looking for.

本文标签: nodejsCan you use functions within a switch case in JavaScriptStack Overflow