A.M.K
A.M.K

Reputation: 16785

Check a switch to see if it has a case


This has come up a few times in a project I'm working on, how can I "test" a switch to determine if it has a case in it without actually executing it?

If the case has to be run, is there an efficient way to check?

Thank you in advance.

i.e.

if (runSwitch(switch.hasCase("casename2"))) {
    alert("I've got it!");
}
else {
    alert("nope");
}

function runSwitch(case) {
    switch (case) { // Any way to skip the function?
        case "casename0" : alert("case"); break;
        case "casename1" : alert("case"); break;
        case "casename2" : alert("case"); break;
        case "casename3" : alert("case"); break;
    }
}

Upvotes: 3

Views: 3619

Answers (3)

A.M.K
A.M.K

Reputation: 16785

Since David Schwartz didn't post an answer, I'm going to post my solution (it's hardly a solution) and a demo and explanation of his solution as I understand it.

My solution:

I simply stopped using switches and switched to JSON (arrays) since the only purpose of my switch was to set variables depending on the input.

When using an array, checking to see if a "case" exists is easy (just run arrayVar["casename"], it returns undefined if it doesn't exist) and you don't need to clog up the namespace with extra variables, running code is slightly more difficult as it needs to be provided as a string and evaled but overall this works much better for me.

There's no need to post a demo or code since it really isn't a solution to this issue.

David Schwartz's solution:

Demo: http://jsfiddle.net/SO_AMK/s9MhD/

Code:

function hasCase(caseName) { return switchName(caseName, true); } // A function to check for a case, this passes the case name to switchName, sets "just checking" to true and passes the response along
function runSwitch(caseName) { switchName(caseName); } // This is actually a mostly useless function but it helps clarify things

function switchName(caseName, c) { // And the actual function, c is the "just checking" variable, I shortened it's name to save code
    switch(caseName) { // The switch
        case "casename0" : if (c) return true; alert("case"); break; // If c is true return true otherwise continue with the case, returning from inside a case is the same as calling break;
        case "casename1" : if (c) return true; alert("case"); break;
        case "casename2" : if (c) return true; alert("case"); break;
        case "casename3" : if (c) return true; alert("case"); break;
        default: if (c) return false; break; // If nothing else ran then the case doesn't exist, return false
    }
    return c; // Even I (:D) don't understand where this gets changed, but it works
}

Explanation: The code above is commented as needed. ​

Upvotes: 0

Errol Fitzgerald
Errol Fitzgerald

Reputation: 3008

No matter what you're going to be checking every case regardless, so running it through the switch is optimal. If you just want to check if the case is there before running it, add them to an array and check if the index exists.

var cases = ['case1', 'case2'];
if (cases.IndexOf('case1') >= 0) {
    // the case is there
}

Upvotes: 5

Kyle
Kyle

Reputation: 4449

The only way that I know of to determine if a switch has your case, is to actually run the case statement. If you want to know if the case block actually ran, you can return a boolean value.

function runSwitch(_case) {
    var hasCase = false;
    switch(_case) {
        case 'casename0': hasCase = true; alert('case'); break;
        case 'casename1': hasCase = true; alert('case'); break;
        case 'casename2': hasCase = true; alert('case'); break;
        case 'casename3': hasCase = true; alert('case'); break;
        default: break;
    }
    return hasCase;
}

You can then run the statement by using:

if(runSwitch('casename4')) {
    //The case statement ran
}
else {
    //The case statement did not run
}

Upvotes: 0

Related Questions