将2个功能转换为一个功能

问题描述 投票:0回答:1

以下是两个功能,可用于获取下拉菜单中的选项。我想使其成为一种功能,而不是两种不同的功能。根据fromtype / totype,几乎没有其他选项。但是大多数像case A,I,W等对于这两个功能都是通用的。如何将以下两个功能变成一个功能。

我正在考虑一个功能

const getOptionKey = (type, metaType) => {
  switch (type) {
    case 'A':
      type = ['A'];
      break;
    case 'W1':
    case 'W':
      type = ['W'];
      break;
    case 'I':
      type = ['I'];
      break;
    case 'E':
      switch (metaType) {
        case 'A':
          type = ['A'];
          break;
        case 'W2':
          fromType = ['W'];
          break;
          default:
          fromType = [
            'A',
            'W',
            'C',
            'I',
            'CK'
          ];
      }
      break;
    default:
      type = [];
  }
}

但是后来我无法提出更好的解决方案,如何对不常见的开关盒进行分类。

const getFromTypeOptionKey = (fromType, fromMetaType) => {
  switch (fromType) {
    case 'A':
      fromType = ['A'];
      break;
    case 'I':
      fromType = ['I'];
      break;
    case 'W1':
    case 'W':
      fromType = ['W'];
      break;
    case 'R':
      fromType = ['R'];
      break;
    case 'E':
      switch (fromMetaType) {
        case 'A1':
          fromType = ['A'];
          break;
        case 'W2':
          fromType = ['W'];
          break;
        case 'I1':
        case 'L':
        case 'CH':
        case 'C1':
        case 'AT':
          fromType = ['C'];
          break;
        default:
          fromType = [
            'A',
            'W',
            'C',
            'I',
            'CK'
          ];
      }
      break;
    default:
      fromType = [];
  }
  return fromType;
};

const getToTypeOptionKey = (toType, toMetaType) => {
  switch (toType) {
    case 'A':
      toType = ['A'];
      break;
    case 'CK':
      toType = ['CK'];
      break;
    case 'I':
      toType = ['I'];
      break;
    case 'W1':
    case 'W':
      toType = ['W'];
      break;
    case 'E':
      switch (toMetaType) {
        case 'A1':
          toType = ['A'];
          break;
        case 'W2':
          toType = ['W'];
          break;
        case 'I1':
        case 'CW':
        case 'C1':
        case 'PD':
        case 'PU':
        case 'AT':
          toType = ['C'];
          break;
        default:
          toType = [
            'A',
            'W',
            'C',
            'I',
            'CK'
          ];
      }
      break;
    default:
      toType = [];
  }
  return toType;
};

javascript reactjs function switch-statement
1个回答
1
投票

switch的情况在这里有点笨拙。如何使用对象和if语句的位,例如:

// Easier to read here since made a tree
let mapping = {
  A: ['A'],
  I: ['I'],
  W: ['W'],
  W1: ['W'],
  R: ['R'],
  default: [],
  E: {
    A1: ['A'],
    W2: ['W'],
    I1: ['C'],
    L: ['C'],
    CH: ['C'],
    C1: ['C'],
    AT: ['C'],
    default: ['A', 'W', 'C', 'I', 'CK'],
  }
}

function get(fromType, fromMetaType) {
  return (fromType === 'E')
    ? mapping['E'][fromMetaType] || mapping['E'].default
    : mapping[fromType] || mapping.default;
}

console.log(get('A')); // [ 'A' ]
console.log(get('INVALID')); // []
console.log(get('E', 'INVALID')); // [ 'A', 'W', 'C', 'I', 'CK' ]
console.log(get('E', 'C1')); // [ 'C' ]
console.log(get('R', 'C1')); // [ 'R' ] note that the `C1` is discarded
© www.soinside.com 2019 - 2024. All rights reserved.