JS枚举库封装管理

JS枚举库封装

暂只封装了自己常用的场景,可自行增加

class Enum {
  constructor() {
    this.enum = {
      "TASK_STATUS": {
        "INCOMPLETE": {
          "value": 0,
          "title": '待办'
        },
        "INPROCESS": {
          "value": 1,
          "title": '处理中'
        },
        "COMPLETE": {
          "value": 2,
          "title": '已完结'
        },
      }
    }
  }

  /** 返回对象形式枚举
   * @param {String} enumKey
   */
  getEnumItemObject(enumKey) {
    return this.enum[enumKey] || {}
  }

  /** 返回数组形式枚举
   * @param {String} enumKey
   */
  getEnumItemList(enumKey) {
    return this.enum[enumKey] ? Object.values(this.enum[enumKey]) : []
  }

  /** 根据枚举子项 key 查询
   * @param {String} enumKey
   * @param {String} enumChildKey
   */
  getChildItemByKey(enumKey, enumChildKey) {
    return this.enum[enumKey]?.[enumChildKey] || {}
  }

  /** 根据枚举子项 key 查询 value
   * @param {String} enumKey
   * @param {String} enumChildKey
   */
  getChildItemValueByKey(enumKey, enumChildKey) {
    return this.getChildItemByKey(enumKey, enumChildKey).value
  }

  /** 根据枚举子项 key 查询 title
   * @param {String} enumKey
   * @param {String} enumChildKey
   */
  getChildItemTitleByKey(enumKey, enumChildKey) {
    return this.getChildItemByKey(enumKey, enumChildKey).title
  }

  /** 根据 value 查询枚举子项
   * @param {String} enumKey
   * @param {Number} value
   */
  getChildItemByValue(enumKey, value) {
    return this.searchEnumItemChild(enumKey, value, 'value').enumItemChild
  }

  /** 根据 title 查询枚举子项
   * @param {String} enumKey
   * @param {String} title
   */
  getChildItemByTitle(enumKey, title) {
    return this.searchEnumItemChild(enumKey, title, 'title').enumItemChild
  }

  /** 根据 value 查询枚举子项名称
   * @param {String} enumKey
   * @param {Number} value
   */
  getChildItemKeyNameByValue(enumKey, value) {
    return this.searchEnumItemChild(enumKey, value, 'value').keyName
  }

  /** 根据 title 查询枚举子项名称
   * @param {String} enumKey
   * @param {Number} value
   */
  getChildItemKeyNameByTitle(enumKey, title) {
    return this.searchEnumItemChild(enumKey, title, 'title').keyName
  }

  searchEnumItemChild(enumKey, origin, originKey) {
    const enumItem = this.enum[enumKey]
    if (enumItem) {
      for (let keyName in enumItem) {
        let enumItemChild = enumItem[keyName]
        if (enumItemChild[originKey] === origin) {
          return { keyName, enumItemChild }
        }
      }
    }
    return {
      keyName: '',
      enumItemChild: {},
    }
  }
}

结果

const instance = new Enum()
console.log(instance.getEnumItemObject('TASK_STATUS'))
// {
//     "INCOMPLETE": {
//         "value": 0,
//         "title": "待办"
//     },
//     "INPROCESS": {
//         "value": 1,
//         "title": "处理中"
//     },
//     "COMPLETE": {
//         "value": 2,
//         "title": "已完结"
//     }
// }
console.log(instance.getEnumItemList('TASK_STATUS'))
// [
//     {
//         "value": 0,
//         "title": "待办"
//     },
//     {
//         "value": 1,
//         "title": "处理中"
//     },
//     {
//         "value": 2,
//         "title": "已完结"
//     }
// ]
console.log(instance.getChildItemByKey('TASK_STATUS', 'COMPLETE'))
// {
//     "value": 2,
//     "title": "已完结"
// }
console.log(instance.getChildItemValueByKey('TASK_STATUS', 'COMPLETE'))
// 2
console.log(instance.getChildItemTitleByKey('TASK_STATUS', 'COMPLETE'))
// 已完结
console.log(instance.getChildItemByValue('TASK_STATUS', 2))
// {
//     "value": 2,
//     "title": "已完结"
// }
console.log(instance.getChildItemByTitle('TASK_STATUS', '已完结'))
// {
//     "value": 2,
//     "title": "已完结"
// }
console.log(instance.getChildItemKeyNameByValue('TASK_STATUS', 2))
// COMPLETE
console.log(instance.getChildItemKeyNameByTitle('TASK_STATUS', '已完结'))
// COMPLETE
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值