javascript - Converting a specific if/else if/else to switch -
i'm struggling bit here, use bit of outside opinion. i've had (and messily) write function full of if/else statements done, have more time spend on , i'd convert switch. been wracking brains on can't seem figure out.
// function, fine is. var between = "..."; var val = x.value.split("&&") val.foreach(function(y){ application.output(y) if(y.search("{")!=-1.0) { y=y.substring(1,y.length) } else { if(y.search("}")!=-1.0){ y=y.substring(0,y.length-1) } } // below im trying build switch out of if(y.search("!")!=-1.0) { if(y.search("%")!=-1.0) { y=" not '"+y.substring(1,y.length)+"'" } else if(y.search(">") !=-1.0) { y=" !> '"+y.substring(2,y.length)+"'" } else if(y.search("<") !=-1.0) { y=" !< '"+y.substring(2,y.length)+"'" } else if(y.search("...")!=-1.0) { // use index & final index of between slice 2 values apart y=" not between '"+y.substring(1,y.indexof(between))+"'"+" , '"+y.substring(y.lastindexof(between)+3,y.length)+"'" } else { y=" != '"+y.substring(1,y.length)+"'" } // if user inputting operators } else if (y.search(">") !=-1.0) { y = " > '"+y.substring(1,y.length)+"'" } else if (y.search("<") !=-1.0) { y = " < '"+y.substring(1,y.length)+"'" } else if (y.search("...")!=-1.0) { y=" between '"+y.substring(1,y.indexof(between))+"'"+" , '"+y.substring(y.lastindexof(between)+5,y.length)+"'" } else { if(y.search("%")!=-1.0) { y=" '"+y+"'" } else { y=" = '"+y+"'" } } y=" , "+x.name.tostring()+" "+y sql+=y
the function takes operators passed in via user input , builds sql query out of it. not sure if it's possible transform switch, there may way.
i think code ok.
if mine.. replace few things:
- replace
search != -1.0
indexof > -1
. see why search -> indexof, ("!= 1.0 -> > -1" readability) - use
;
@ end of each statement. - second parameter in
substring
already string length default, can remove it.
but it's matter of taste , maintain standard across codes..
application.output(y); if (y.indexof("{") > -1) { y = y.substring(1); } if (y.indexof("}") > -1) { y = y.substring(0, y.length - 1); } // below im trying build switch out of if (y.indexof("!") > -1) { if (y.indexof("%") > -1) { y = " not '" + y.substring(1) + "'"; } else if (y.indexof(">") > -1) { y = " !> '" + y.substring(2) + "'"; } else if (y.indexof("<") > -1) { y = " !< '" + y.substring(2) + "'"; } else if (y.indexof("...") > -1) { // use index & final index of between slice 2 values apart y = " not between '" + y.substring(1, y.indexof(between)) + "'" + " , '" + y.substring(y.lastindexof(between) + 3) + "'"; } else { y = " != '" + y.substring(1) + "'"; } // if user inputting operators } else if (y.indexof(">") > -1) { y = " > '" + y.substring(1) + "'"; } else if (y.indexof("<") > -1) { y = " < '" + y.substring(1) + "'"; } else if (y.indexof("...") > -1) { y = " between '" + y.substring(1, y.indexof(between)) + "'" + " , '" + y.substring(y.lastindexof(between) + 5) + "'"; } else if (y.indexof("%") > -1) { y = " '" + y + "'"; } else { y = " = '" + y + "'"; } y = " , " + x.name.tostring() + " " + y; sql += y;
Comments
Post a Comment