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 🙏

© 2026 – Pkg Stats / Ryan Hefner

enumhelper

v0.1.0

Published

helper to implement Java Enum by using TypseScript

Readme

EnumHelper

解决常见的 enum 类型数据的变换形态,这里的 enum 指的是广义的 enum 数据,而不是某个语言的 enum 类型。

TS enum 存在许多问题,比如:无法确定数字值,比如不支持复杂对象。通常我们会写两个枚举,一个是对应的值,即 Record<string, number | string>,一个是对应的文本,即 Record<string, string>。

第一, 在 Select 组件的场景里还需要将 2 者合并转成 options 的数组写法。第二,在根据值获取文本时又需要构造一个 KV 结构的对象便于展示时获取。总之,非常繁琐。

Usage

npm i enumhelper

Demo

import { EnumHelper, ValueOfEnumObj } from 'enumhelper';

// 定义枚举数据的原始数据
const PAY_TYPE_ENUM_OBJ = {
  DEPOSIT: {
    value: '1',
    label: '保证金+尾款'
  },
  FULL: {
    value: 2,
    label: '全额支付'
  }
} as const; // 非常关键

// enumHelper 的实例
const PAY_TYPE = new EnumHelper(PAY_TYPE_ENUM_OBJ);
//利用 ValueOfEnumObj 构建出 enum值 type
type PAY_TYPE_VAL_TYPE = ValueOfEnumObj<typeof PAY_TYPE_ENUM_OBJ>;

// 获取 options 数组
const labeValOptions = PAY_TYPE.labelValArrayList();
console.log('labeValOptions', labeValOptions);

// 根据值,获取对应文本
const valueFromServer = '1';
const text1 = PAY_TYPE.getLabelByValue(valueFromServer);
console.log('text1', text1); //输出 "保证金+尾款"
const text2 = PAY_TYPE.getLabelByValue(100); // 这里会报 ts 编译时错误
console.log('text1', text2); // undefined

// PAY_TYPE_TYPE 约束参数
function typeCondition(type: PAY_TYPE_VAL_TYPE) {
  if (type === 2) {
  }
  if (type === 3) {
  } // 这里会报 ts 编译时错误, 3 不在 PAY_TYPE_TYPE 中

  // 第一种方式获取值方式
  if (type === PAY_TYPE.enumObj.DEPOSIT.value) {
  }
  // 第二种方式获取值比较
  if (type === PAY_TYPE_ENUM_OBJ.DEPOSIT.value) {
  }
}

API

构造函数 EnumHelper

| 属性 | 说明 | 类型 | 默认值 | | ------- | ------------------------------------- | ---------- | ------ | | enumObj | 第一个传入参数,enum 的 labelVal 对象 | IEnumObj | - |

IEnumObj 和 ILabelValue

interface IEnumObj {
  [key: string]: ILabelValue;
}

interface ILabelValue {
  label: string;
  value: string | number;
}

EnumHelper 实例

| 属性 | 说明 | 类型 | 默认值 | | ----------------- | -------------------------------------------------------------- | ---------------- | ------ | | enumObj | 原始传入的 enumObj | IEnumObj | - | | labelValArrayList | 转换为 labelValue 的数组形式,符合 antdoptions 格式要求 | 无法写,请看代码 | - | | getLabelByValue | 根据枚举值返回对应文本 | 无法写,请看代码 | - |

ValueOfEnumObj 类型

type PAY_TYPE_VAL_TYPE = ValueOfEnumObj<typeof PAY_TYPE_ENUM_OBJ>, 可以获取对象中的联合类型。

LabelOfEnumObj 类型

type PAY_TYPE_LABEL_TYPE = ValueOfEnumObj<typeof PAY_TYPE_ENUM_OBJ> , 可以获取对象中文本的联合类型。