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

ton.enum.lib

v1.0.1

Published

A lightweight TypeScript enum library for JS/TS projects

Readme

EnumBase

一个 TypeScript 通用枚举类,支持动态实例化、缓存查找、JSON 序列化、下拉选项生成以及标签和值的映射。


安装

npm install ton.enum.lib

或者直接在项目中引入 EnumBase.ts


核心类

EnumItem

定义枚举项的基本结构:

export interface EnumItem {
  label: string;
  value: string | number;
  [key: string]: any; // 可扩展额外字段
}

EnumBase

核心枚举类:

export class EnumBase<T extends EnumItem = EnumItem> {
  label: string;
  value: string | number;

  constructor({ label, value, ...extra }: T);
  toJSON(): object;

  // 静态方法
  static values<E extends typeof EnumBase>(): readonly InstanceType<E>[];
  static getLabel<E extends typeof EnumBase>(value: string | number): string | null;
  static getValue<E extends typeof EnumBase>(label: string): string | number | null;
  static fromValue<E extends typeof EnumBase>(value: string | number): InstanceType<E> | null;
  static fromLabel<E extends typeof EnumBase>(label: string): InstanceType<E> | null;
  static toSelectOptions<E extends typeof EnumBase>(): { label: string, value: string | number }[];
  static toValueLabelMap<E extends typeof EnumBase>(): Record<string | number, string>;
  static toLabelValueMap<E extends typeof EnumBase>(): Record<string, string | number>;
}

装饰器

EnumClass

自动将静态对象实例化为 EnumBase

import { EnumBase, EnumClass } from './EnumBase';

@EnumClass
class RoleEnum extends EnumBase {
  static ADMIN = { label: "管理员", value: "admin", level: 1 };
  static USER = { label: "用户", value: "user", level: 2 };
}

使用示例

// 获取所有枚举实例
console.log(RoleEnum.values());
// 输出: [ RoleEnum { label: '管理员', value: 'admin', level: 1 }, RoleEnum { label: '用户', value: 'user', level: 2 } ]

// 根据 value 获取 label
console.log(RoleEnum.getLabel("admin")); // "管理员"

// 根据 label 获取 value
console.log(RoleEnum.getValue("用户")); // "user"

// 根据 value 获取完整实例
console.log(RoleEnum.fromValue("user"));
// 输出: RoleEnum { label: '用户', value: 'user', level: 2 }

// 下拉选项
console.log(RoleEnum.toSelectOptions());
// 输出: [ { label: '管理员', value: 'admin' }, { label: '用户', value: 'user' } ]

// 值到标签映射
console.log(RoleEnum.toValueLabelMap());
// 输出: { admin: '管理员', user: '用户' }

// 标签到值映射
console.log(RoleEnum.toLabelValueMap());
// 输出: { 管理员: 'admin', 用户: 'user' }

特性

  • ✅ 动态实例化静态对象
  • ✅ 缓存查找,性能优异
  • ✅ JSON 序列化友好
  • ✅ 支持额外字段扩展
  • ✅ 可生成前端下拉选项或映射表

类型安全

通过泛型约束 EnumItem,支持自定义字段,保持类型安全。


License

MIT