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 🙏

© 2025 – Pkg Stats / Ryan Hefner

more-enum

v1.0.1

Published

zero dependencies enum extension

Downloads

10

Readme

more-enum

zero dependencies enum extension

零依赖的枚举扩展工具

文档

安装

npm i more-enum

定义枚举值

// 可以与接口返回方约定数字、字符串
// 枚举值,用于逻辑判断
// 常量使用pascal命名更易读,灵感来自golang
const statusEnum = {
  // 待支付
  WaitPay: 0,
  // 已完成
  Success: 1,
  // 已取消
  Cancel: 2,
};

// 枚举值配置,用于属性扩展
const statusEnumConfig = [
  {
    value: statusEnum.WaitPay,
    label: "待支付",
    color: "yellow",
    // 支付 取消支付
    actions: ["pay", "cancel"],
  },
  {
    value: statusEnum.Success,
    label: "已完成",
    color: "green",
    // 查看详情 退款
    actions: ["detail", "return"],
  },
  {
    value: statusEnum.Cancel,
    label: "已取消",
    color: "red",
    // 查看详情
    actions: ["detail"],
  },
];

增强枚举配置

import { createEnumObject } from "more-enum";
// import { createEnumObject } from "./src/index.js";

// 枚举值对象,用于数值转换
const statusEnumObject = createEnumObject(statusEnumConfig);

使用

// 通过 value获取配置项
console.log(statusEnumObject.getItem(1));
// {
//   value: 1,
//   label: '已完成',
//   color: 'green',
//   actions: [ 'detail', 'return' ]
// }

// 通过label获取value
console.log(statusEnumObject.getValue("已完成"));
// 1

// 没有对应的值返回undefined
console.log(statusEnumObject.getValue(1));
// undefined

// 通过value 获取 label
// 接口返回的真实数值,转换为显示值
console.log(statusEnumObject.getLabel(1));
// 已完成

// 通过指定key=color,获取label
console.log(statusEnumObject.getLabel('green', 'color'));
// 已完成

// 直接和接口返回的数值,做逻辑判断
let status = 1;
console.log(statusEnum.Success == status);
// true