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

enum-format

v1.0.4

Published

一个用于处理 TypeScript 枚举的工具库,提供了一系列便捷的方法来操作和转换枚举数据。

Downloads

16

Readme

Enum Utils

一个用于处理 TypeScript 枚举的工具库,提供了一系列便捷的方法来操作和转换枚举数据。

功能列表

  • [enumToKeyValueArr] - 将枚举转换为键值对数组
  • [enumToBiMap] - 将枚举转换为双向映射对象
  • [enumToValueArr] - 提取枚举的所有值组成数组
  • [enumToKeyArr] - 提取枚举的所有键(排除反向映射)
  • [isEnumValue] - 判断某个值是否是枚举的有效值
  • [getEnumKeyByValue] - 根据枚举值查找对应的键
  • [enumFilterValuesByKey] - 按 key 的正则筛选枚举的 value 数组
  • [enumFilterKeysByValue] - 按 value 的正则筛选枚举的 key 数组

API 说明

enumToKeyValueArr(e, labelKey?, valueKey?)

将枚举对象转换成键值对数组。

参数:

  • e: 枚举对象
  • labelKey: 自定义键字段名(默认为 "key")
  • valueKey: 自定义值字段名(默认为 "value")

示例:

enum Status {
  Active = 'ACTIVE',
  Inactive = 'INACTIVE'
}

enumToKeyValueArr(Status);
// [{ key: 'Active', value: 'ACTIVE' }, { key: 'Inactive', value: 'INACTIVE' }]

enumToBiMap(e)

将枚举对象转换成双向映射对象,支持通过键或值互相查找。

参数:

  • e: 枚举对象

示例:

enum Status {
  Active = 'ACTIVE',
  Inactive = 'INACTIVE'
}

const biMap = enumToBiMap(Status);
// { Active: 'ACTIVE', ACTIVE: 'Active', Inactive: 'INACTIVE', INACTIVE: 'Inactive' }

enumToValueArr(e)

提取枚举的所有值组成数组。

参数:

  • e: 枚举对象

示例:

enum Status {
  Active = 'ACTIVE',
  Inactive = 'INACTIVE'
}

enumToValueArr(Status);
// ['ACTIVE', 'INACTIVE']

enumToKeyArr(e)

获取枚举的所有键(排除反向映射)。

参数:

  • e: 枚举对象

示例:

enum Status {
  Active = 'ACTIVE',
  Inactive = 'INACTIVE'
}

enumToKeyArr(Status);
// ['Active', 'Inactive']

isEnumValue(e, value)

判断某个值是否是枚举的有效值。

参数:

  • e: 枚举对象
  • value: 待检查的值

示例:

enum Status {
  Active = 'ACTIVE',
  Inactive = 'INACTIVE'
}

isEnumValue(Status, 'ACTIVE'); // true
isEnumValue(Status, 'UNKNOWN'); // false

getEnumKeyByValue(e, value)

根据枚举值查找对应的键。

参数:

  • e: 枚举对象
  • value: 枚举值

示例:

enum Status {
  Active = 'ACTIVE',
  Inactive = 'INACTIVE'
}

getEnumKeyByValue(Status, 'ACTIVE'); // 'Active'
getEnumKeyByValue(Status, 'UNKNOWN'); // null

enumFilterValuesByKey

根据枚举键正则过滤枚举值。

参数:

  • e: 枚举对象
  • regex: key的正则表达式

示例:

enum Status {
  Active_1 = 'ACTIVE_1',
  Active_2 = 'ACTIVE_2',
  Inactive_1 = 'INACTIVE_1',
  Inactive_2 = 'INACTIVE_2',
}

enumFilterValuesByKey(Status, /^Active/); // ['ACTIVE_1', 'ACTIVE_2']
enumFilterValuesByKey(Status, /^UNKNOWN/); // []

enumFilterKeysByValue

根据枚举值正则过滤枚举键。

参数:

  • e: 枚举对象
  • regex: value的正则表达式

示例:

enum Status {
  Active_1 = 'ACTIVE_1',
  Active_2 = 'ACTIVE_2',
  Inactive_1 = 'INACTIVE_1',
  Inactive_2 = 'INACTIVE_2',
}

enumFilterKeysByValue(Status, /^ACTIVE/); // ['Active_1', 'Active_2']
enumFilterKeysByValue(Status, /^UNKNOWN/); // []