将特定的if / else if / else转换为switch

时间:2014-03-10 10:59:51

标签: javascript if-statement switch-statement

我在这里苦苦挣扎,所以我可以使用一些外界意见。我不得不快速(并且乱七八糟地)编写一个充满if / else语句的函数来完成某些事情,但现在我有更多的时间花在它上面,我想把它转换成一个开关。一直在绞尽脑汁,但我似乎无法弄明白。

// This is just the function, this is fine as it 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 is what Im trying to build a switch out of
if(y.search("!")!=-1.0) {

            if(y.search("%")!=-1.0) {
                y=" NOT LIKE '"+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 the index & final index of between to slice the two values apart
                y=" NOT BETWEEN '"+y.substring(1,y.indexOf(between))+"'"+" AND '"+y.substring(y.lastIndexOf(between)+3,y.length)+"'"
            } else {
                y=" != '"+y.substring(1,y.length)+"'"
            }
        // If user is 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))+"'"+" AND '"+y.substring(y.lastIndexOf(between)+5,y.length)+"'"
        } else {
            if(y.search("%")!=-1.0) {
                y=" LIKE '"+y+"'"
            } else {
                y=" = '"+y+"'"
            }
        }
        y=" and "+x.name.toString()+" "+y
        sql+=y

该函数接受通过用户输入传入的运算符,并从中构建SQL查询。甚至不确定是否可以将其转换为开关,但可能有一种方法。

2 个答案:

答案 0 :(得分:0)

您可以尝试使用map和switch-case组合,但最好删除所有重复的代码段和&amp;只保留其中一个,同时连接很多字符串,在单独的行中做得更好,这将提高可读性

...样品

function checkChar(c) {
    return y.search(c) != -1.0;
}

var len = y.length;

if (checkChar("!")) {

    if (checkChar("%")) y = " NOT LIKE '" + y.substring(1, len);
    else if (checkChar(">")) y = " !> '" + y.substring(2, len);
    else if (checkChar("<")) y = " !< '" + y.substring(2, len);
    else if (checkChar("...")) {
        y = " NOT BETWEEN '";
        y += y.substring(1, y.indexOf(between));
        y += "' AND '";
        y += y.substring(y.lastIndexOf(between) + 3, len);
    } 
    else y = " != '" + y.substring(1, len);

    y += "'";

} 

答案 1 :(得分:0)

我认为这段代码还可以。

如果是我的......我只会更换一些东西:

但这是一个品味问题,并在您的代码中保持标准..

    application.output(y);
    if (y.indexOf("{") > -1) {
        y = y.substring(1);
    }
    if (y.indexOf("}") > -1) {
        y = y.substring(0, y.length - 1);
    }

    // Below is what Im trying to build a switch out of
    if (y.indexOf("!") > -1) {

        if (y.indexOf("%") > -1) {
            y = " NOT LIKE '" + 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 the index & final index of between to slice the two values apart
            y = " NOT BETWEEN '" + y.substring(1, y.indexOf(between)) + "'" + " AND '" + y.substring(y.lastIndexOf(between) + 3) + "'";
        } else {
            y = " != '" + y.substring(1) + "'";
        }
        // If user is 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)) + "'" + " AND '" + y.substring(y.lastIndexOf(between) + 5) + "'";
    } else if (y.indexOf("%") > -1) {
        y = " LIKE '" + y + "'";
    } else {
        y = " = '" + y + "'";
    }

    y = " AND " + x.name.toString() + " " + y;
    sql += y;