简化JavaScript中的切换案例

问题描述 投票:2回答:7

我有一个相当重复的switch case语句,在我学习最简单的做事方式的过程中,我想转向SO,看看是否有更优雅的解决方案:

        switch(id)
        {
            case 'ib-02a':
                if(direction == 'left')
                    setHash('ib-02b');
                break;
            case 'ib-02b':
                if(direction == 'right')
                    setHash('ib-02a');
                if(direction == 'left')
                    setHash('ib-02c');
                break;
            case 'ib-02c':
                if(direction == 'right')
                    setHash('ib-02b');
                if(direction == 'left')
                    setHash('ib-02d');
                break;
            case 'ib-02d':
                if(direction == 'right')
                    setHash('ib-02c');
            break;

            case 'ib-03a':
                if(direction == 'left')
                    setHash('ib-03b');
                break;
            case 'ib-03b':
                if(direction == 'right')
                    setHash('ib-03a');
                if(direction == 'left')
                    setHash('ib-03c');
                break;
            case 'ib-03c':
                if(direction == 'right')
                    setHash('ib-03b');
                if(direction == 'left')
                    setHash('ib-03d');
                break;
            case 'ib-03d':
                if(direction == 'right')
                    setHash('ib-03c');
            break;

            case 'pb-05a':
                if(direction == 'left')
                    setHash('pb-05b');
                break;
            case 'pb-05b':
                if(direction == 'right')
                    setHash('pb-05a');
                if(direction == 'left')
                    setHash('pb-05c');
                break;
            case 'pb-05c':
                if(direction == 'right')
                    setHash('pb-05b');
                if(direction == 'left')
                    setHash('pb-05d');
                break;
            case 'pb-05d':
                if(direction == 'right')
                    setHash('pb-05c');
            break;
        }

我正在阅读滑动事件,如果我正在滑动的元素的ID与ib-02 *,ib-03 *或pb-05 *匹配,我正在为相应的ID调用setHash函数。如果我在* a上滑动,我向左滑动到* b。如果我在* b上滑动,我向右滑动到* a并向左滑动到* c。所以等等,总是在* a和* d之间。

必须有一个不那么重复的方法来做到这一点,但我不确定最好的方法是什么。

javascript jquery switch-statement swipe
7个回答
2
投票

如何将它们映射到对象?然后只需使用带有检索值的setHash

var ids = {
    'pb-05c' : {
        left : 'pb-05d',
        right : 'pb-05b'
    }
    ...
}

function setHashes(id,direction){
    if(id && ids[id]){
        id = ids[id];
        if(direction && id[direction]){
            setHash(id[direction]);
        }
    }
}

这都是检索,没有条件评估,这可能有利于提高性能。


2
投票

有4个主要案例是abcd,你可以将你的switch语句作为这些字符串的基础,试试这个:

var c = id.slice(0, 5); // "ib-02" or "ib-03" or "ib-04" ...
var which = id.slice(-1); // "a" or "b" or "c" or "d"
switch(which) {
    case 'a':
       if(direction == 'left')
             setHash(c+'b');
          break;
    case 'b':
       if(direction == 'right')
             setHash(c+'a');
       if(direction == 'left')
             setHash(c+'c');
          break;
    case 'c':
       if(direction == 'right')
             setHash(c+'b');
       if(direction == 'left')
             setHash(c+'d');
          break;
    case 'd':
       if(direction == 'right')
            setHash(c+'c');
          break;
}

1
投票

你可以像这样驱动整个数据表:

var logicData = {
    // format is the id first and then an array with the left, then right value for the hash
    // leave an item as an empty string if you don't ever want to go that direction
    'ib-02a': ['ib-02b', ''],
    'ib-02b': ['ib-02c', 'ib-02a'],
    'ib-02c': ['ib-02d', 'ib-02d']
    // fill in the rest of the data table here
};

function setNewHash(id, direction) {
    var hash, data = logicData[id];
    if (data) {
        if (direction == 'left') {
            hash = data[0];
        } else if (direction == 'right') {
            hash = data[1];
        }
        if (hash) {
            setHash(hash);
        }
    }
}

1
投票
id='ib-02a'; //you have string id, this one is for demo
id=[id.slice(0,--id.length), id.charAt(--id.length)];

switch(id[1]){
    case 'a':
        if(direction == 'left'){setHash(id[0]+'b');}
        break;
    case 'b':
        if(direction =='right'){setHash(id[0]+'a');}
        if(direction == 'left'){setHash(id[0]+'c');}
        break;
    case 'c':
        if(direction == 'right'){setHash(id[0]+'b');}
        if(direction == 'left'){setHash(id[0]+'d');}
        break;
    case 'd':
        if(direction == 'right'){setHash(id[0]+'c');}
        break;
}

如果情况b和c只是'左'或'右',你可以在那些if语句中使用else


1
投票

我喜欢undefined和GitaarLab的一般方向,他们实际上解决了算法并且刚刚实现了算法。要回顾一下,算法基本上是left递增最后一个字母而right递减id的最后一个字母,但你不要低于a或高于d。所以,我做了一个紧凑的实现,我将最后一个字母转换为数字并直接递增或递减,而不是使用if / else或case语句:

function setNewHash(id, direction) {
    var base = id.substr(0, 5);
    var tag = id.charCodeAt(5), newTag;
    var nav = {left: 1, right: -1};
    var delta = nav[direction];
    if (delta) {
        tag += delta;
        newTag = String.fromCharCode(tag);
        if (newTag >= 'a' && newTag <= 'd') {
            setHash(base + newTag);
        }
    }
}

工作测试案例:http://jsfiddle.net/jfriend00/gwfLD/


0
投票

你可以把id分成不同的部分,然后在你做setHash之前重建它们。

function chunkId(id) {
    // use a regex or string split or something to convert
    // "ib-05a" to ["ib-05", "a"]
    return ["ib-05", "a"];
}

function next(str) {
    // return the next letter in the alphabet here
    return "b";
}

function prev(str) {
    // return the prev letter in the alphabet here
    return "b";
}

function swipe (id, direction) {
    var array = chunkId(id);
    // you can only swipe left if not on "d"
    if (direction === "left" && id[1] != "d") {
        setHash(id[0] + next(id[1])); // build hash based on next character
    }
    // you can only swipe right if not on "a"
    if (direction === "right" && id[1] != "a") {
        setHash(id[0] + prev(id[1])); // build hash based on prev character
    }
}

0
投票

您可以切换ID和方向检查以使其更清晰:

switch (direction) {
  case 'left':
    switch (id) {
      case 'ib-02a': setHash('ib-02b'); break;
      case 'ib-02b': setHash('ib-02c'); break;
      case 'ib-02c': setHash('ib-02d'); break;
      case 'ib-03a': setHash('ib-03b'); break;
      case 'ib-03b': setHash('ib-03c'); break;
      case 'ib-03c': setHash('ib-03d'); break;
      case 'pb-05a': setHash('pb-05b'); break;
      case 'pb-05b': setHash('pb-05c'); break;
      case 'pb-05c': setHash('pb-05d'); break;
    }
    break;
  case 'right':
    switch (id) {
      case 'ib-02b': setHash('ib-02a'); break;
      case 'ib-02c': setHash('ib-02b'); break;
      case 'ib-02d': setHash('ib-02c'); break;
      case 'ib-03b': setHash('ib-03a'); break;
      case 'ib-03c': setHash('ib-03b'); break;
      case 'ib-03d': setHash('ib-03c'); break;
      case 'pb-05b': setHash('pb-05a'); break;
      case 'pb-05c': setHash('pb-05b'); break;
      case 'pb-05d': setHash('pb-05c'); break;
    }
    break;
}

然后你可以通过分割id来简化:

var first = id.substr(0, 5);
var last = id.substr(6);
switch (direction) {
  case 'left':
    switch (last) {
      case 'a': setHash(first + 'b'); break;
      case 'b': setHash(first + 'c'); break;
      case 'c': setHash(first + 'd'); break;
    }
    break;
  case 'right':
    switch (last) {
      case 'b': setHash(first + 'a'); break;
      case 'c': setHash(first + 'b'); break;
      case 'd': setHash(first + 'c'); break;
    }
    break;
}
© www.soinside.com 2019 - 2024. All rights reserved.