Socket
Book a DemoInstallSign in
Socket

cenum

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cenum

一个简易的enum库

latest
Source
npmnpm
Version
2.0.3
Version published
Maintainers
1
Created
Source

cenum

安装

Using npm:

$ npm install cenum

or using yarn:

$ yarn add cenum

用法

import Enum from 'cenum';

const status = new Enum([
  {name: 'ACTIVE', value: 1, label: '激活'},
  {name: 'INACTIVE', value: 0, lable: '未激活'}
]);

status.get('ACTIVE').is(1); // true
status.get('ACTIVE').is('ACTIVE'); // true
status.get('ACTIVE').is('激活'); // true

status.has('INACTIVE'); // true
status.has(0); // true
status.has('未激活'); // true

status.get('ACTIVE').in(0, 1) // true
status.get('ACTIVE').in(['ACTIVE', '未激活']) // true

status.get('ACTIVE').label // 激活
status.get('ACTIVE').name // ACTIVE
status.get('ACTIVE').value // 0

除了通过 get 方法获取对应的枚举,还可以通过枚举名称直接获取,前提条件是枚举名称和内部变量不产生冲突,所以建议名称使用大写,因为内部变量都用的小写,保证不冲突

status.ACTIVE.label // 激活

或者使用parse批量转换

import { enums, parse } from 'cenum';

parse({
  role: [
    {name: 'ACTIVE', value: 1, label: '激活'},
    {name: 'INACTIVE', value: 0, lable: '未激活'}
  ],
  gender: [
    ['MALE', 1, '男'],
    ['FEMALE', 0, '女']
  ]
})

enums.role // 角色Enum
enums.gender // 性别Enum

构造方法的参数有四种格式

  • 以对象方式完整定义name, value, label
    [{name, value, label}, ...]
  • 以数组方式完整定义name, value, label
    [[name, value, label], ...]
  • 只提供值, name和labe将和value保持一致
    [value, ...]
  • 以键-值对的方式定义name和value
    {name: value, ...}

当缺失name或者label的时候,将自动用name表示label或者用label表示name
当name和label都没有的时候,将默认等于value

请确保每一条的name, value, label互相之间都是不一样的

Keywords

enum

FAQs

Package last updated on 25 Aug 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts