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

define-enum

v1.0.1

Published

A utility function to define enum-like objects with TypeScript support.一个帮助定义枚举的工具函数

Readme

defineEnum

defineEnum 一个帮助你友好定义枚举的函数,解决了平时开发过程中枚举和对应的字符映射的问题,以及需要通过枚举生成Select的Options之类的需求

ps: 由于使用了const type parameter 所以ts版本最好在5.0以上(低版本如果遇到ts报错可以在定义的时候加入as const 看看),but AnyScript yes!

Installation

pnpm add define-enum
npm install define-enum

Usage

基础用法

import { defineEnum } from 'define-enum';

const TestStatus = defineEnum([
  { key: 'UNKNOWN', value: 0, label: '未知', disabled: true },
  { key: 'PASS', value: 1, label: '通过' },
  { key: 'FAIL', value: 2, label: '失败' },
])

console.log(TestStatus.UNKNOWN); // 输出: 0
console.log(TestStatus.PASS);    // 输出: 1
console.log(TestStatus.FAIL);    // 输出: 2

获取标签

console.log(TestStatus.getLabel(0)); // 输出: '未知'
console.log(TestStatus.getLabel(TestStatus.UNKNOWN)); // 输出: '未知'
console.log(TestStatus.getLabel(TestStatus.PASS));    // 输出: '通过'
console.log(TestStatus.getLabel(TestStatus.FAIL));    // 输出: '失败'

获取键名

console.log(TestStatus.getKey(0)); // 输出: 'UNKNOWN'
console.log(TestStatus.getKey(TestStatus.UNKNOWN)); // 输出: 'UNKNOWN'
console.log(TestStatus.getKey(TestStatus.PASS));    // 输出: 'PASS'
console.log(TestStatus.getKey(TestStatus.FAIL));    // 输出: 'FAIL'

检查值是否存在

console.log(TestStatus.has(0)); // 输出: true
console.log(TestStatus.has(1)); // 输出: true
console.log(TestStatus.has(2)); // 输出: true
console.log(TestStatus.has(3)); // 输出: false
console.log(TestStatus.has('UNKNOWN')); // 输出: true
console.log(TestStatus.has('PASS'));    // 输出: true
console.log(TestStatus.has('FAIL'));    // 输出: true
console.log(TestStatus.has('UNKNOWN')); // 输出: false

获取所有键名、值和条目

console.log(TestStatus.keys()); // 输出: ['UNKNOWN', 'PASS', 'FAIL']
console.log(TestStatus.values()); // 输出: [0, 1, 2]
console.log(TestStatus.entries()); 
// 输出: [
//   { key: 'UNKNOWN', value: 0, label: '未知', disabled: true },
//   { key: 'PASS', value: 1, label: '通过' },
//   { key: 'FAIL', value: 2, label: '失败' }
// ]

获取选项列表

console.log(TestStatus.getOptions()); 
// 输出: [
//   { label: '未知', value: 0, disabled: true },
//   { label: '通过', value: 1 },
//   { label: '失败', value: 2 }
// ]

禁用选项

console.log(TestStatus.getOptions({ excludeDisabled: true })); 
// 输出: [
//   { label: '通过', value: 1 },
//   { label: '失败', value: 2 }
// ]

自定义标签和值的键名

console.log(TestStatus.getOptions({ labelKey: 'name', valueKey: 'id' })); 
// 输出: [
//   { name: '未知', id: 0, disabled: true },
//   { name: '通过', id: 1 },
//   { name: '失败', id: 2 }
// ]

API

函数定义

const defineEnum: <const T extends EnumItems>(enumItems: T) => DefineEnumResult<T>

可调用的属性和方法

export type DefineEnumResult<T extends EnumItems> = {
  /** 枚举项的键值对 */
  [K in T[number]['key']]: Extract<T[number], { key: K }>['value']
} & {
  /** 获取所有的 key */
  keys: () => T[number]['key'][]
  /** 获取所有的 value */
  values: () => T[number]['value'][]
  /** 根据枚举值获取对应的 key */
  getKey: (value: T[number]['value']) => T[number]['key'] | undefined
  /** 根据枚举值获取对应的 label */
  getLabel: (value: T[number]['value']) => string | number | undefined
  /** 获取选项列表 */
  getOptions: (config?: GetOptionsConfig) => { label: string; value: T[number]['value'] }[]
  /** 判断枚举是否存在 */
  has: (key: unknown) => boolean
  /** 返回完整的原始枚举项数组,也就是你传入defineEnum中的数组 */
  entries: () => T[number][]
}

EnumItem 类型定义

export type EnumItem = {
    key: string;
    value: string | number;
    label: string;
    disabled?: boolean;
};

GetOptionsConfig 类型定义

/**
 * 传给 getOptions 方法的配置项,能够传入labelKey、valueKey、excludeDisabled以及其他任意的属性,方便直接透传给Select组件使用
 */
export type GetOptionsConfig = {
  /**
   * 选项的 label 键名
   * @default 'label'
   */
  labelKey?: string
  /**
   * 选项的 value 键名
   * @default 'value'
   */
  valueKey?: string
  /**
   * 是否过滤禁用的选项,如果为true返回的options中disabled设置为true的选项会被过滤
   * @default false
   */
  excludeDisabled?: boolean
} & {
  [key in string]: any
}

注意事项

  1. 由于使用了const type parameter 所以ts版本最好在5.0以上(低版本如果遇到ts报错可以在定义的时候加入as const 看看),but we know AnyScript Yes!
const TestStatus = defineEnum([
  { key: 'UNKNOWN', value: 0, label: '未知', disabled: true },
  { key: 'PASS', value: 1, label: '通过' },
  { key: 'FAIL', value: 2, label: '失败' },
] as const)