npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cenum

v2.0.3

Published

一个简易的enum库

Downloads

2

Readme

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

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

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

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

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