【问题标题】:Convert to 2 functions to one function将 2 个函数转换为 1 个函数
【发布时间】:2020-03-26 17:47:17
【问题描述】:

以下是获取下拉列表中选项的 2 个函数。我想让它成为一个功能而不是两个不同的功能。基于 fromtype/totype 的选项很少。但最像 case A、I、W 等对于这两种功能都是通用的。如何将以下2个函数合为一个函数。

我在想一个函数

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 = [];
  }
}

但是我无法想出更好的解决方案来对不常见的 switch 案例进行分类。

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;
};

【问题讨论】:

  • 案例“E”应该调用getToTypeOptionKey,其toMetaType作为toType
  • @Mr.Polywhirl 我已经编辑了我的问题。您能否检查一下我正在尝试的解决方案。

标签: javascript reactjs function switch-statement


【解决方案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

【讨论】:

    猜你喜欢
    • 2016-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-22
    相关资源
    最近更新 更多