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 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-enum-object

v1.3.2

Published

resolve TypeScript enum value do not support objects

Downloads

31

Readme

ts-enum-object

解决 TypeScript 下枚举值无法为对象,以及保证枚举值有序性的一些应用场景,并提供了完整的 TypeScript 类型映射。

安装

$ npm i ts-enum-object --save

API

  • createEnumObject(items: Array<{ name: string; value: any; label?: string; ...other }>)
    创建一个枚举对象,namevalue 为必填,name 代表枚举对象的 key,其他字段可以按业务需求扩展。

    TypeScript 下入参的数组【务必】【务必】【务必】加 as const,否则无法正常推导出 Key 的类型

    import { createEnumObject } from 'ts-enum-object';
    
    const TestEnum = createEnumObject([
      { name: 'A', value: 1, label: 'AA', },
      { name: 'B', value: 2, label: 'BB', },
      { name: 'C', value: 3, label: 'CC', },
    ] as const); // as const is required
    
    TestEnum.A // 1
    TestEnum.B // 2
    TestEnum.C // 3
  • .keys()
    获取枚举所有 name

    import { createEnumObject } from 'ts-enum-object';
    
    const TestEnum = createEnumObject([
      { name: 'A', value: 1, label: 'AA', },
      { name: 'B', value: 2, label: 'BB', },
      { name: 'C', value: 3, label: 'CC', },
    ] as const); // as const is required
    
    TestEnum.keys() // ['A', 'B', 'C']
  • .values()
    获取枚举所有 value

    import { createEnumObject } from 'ts-enum-object';
    
    const TestEnum = createEnumObject([
      { name: 'A', value: 1, label: 'AA', },
      { name: 'B', value: 2, label: 'BB', },
      { name: 'C', value: 3, label: 'CC', },
    ] as const); // as const is required
    
    TestEnum.values() // [1, 2, 3]
    
  • .items()
    获取枚举列表,也就是 createEnumObject 的入参。

    import { createEnumObject } from 'ts-enum-object';
    
    const TestEnum = createEnumObject([
      { name: 'A', value: 1, label: 'AA', },
      { name: 'B', value: 2, label: 'BB', },
      { name: 'C', value: 3, label: 'CC', },
    ] as const); // as const is required
    
    TestEnum.items()
    // [
    //   { name: 'A', value: 1, label: 'AA', },
    //   { name: 'B', value: 2, label: 'BB', },
    //   { name: 'C', value: 3, label: 'CC', },
    // ]
  • .getItemBy(key, valueOfKey)
    根据枚举配置对象中某个字段的名字及其值,获取对应的枚举配置项(如果会出现重复,只返回第一个)。

    import { createEnumObject } from 'ts-enum-object';
    
    const TestEnum = createEnumObject([
      { name: 'A', value: 1, label: 'AA', },
      { name: 'B', value: 2, label: 'BB', },
      { name: 'C', value: 3, label: 'CC', },
    ] as const); // as const is required
    
    TestEnum.getItemBy('label', 'AA') // { name: 'A', value: 1, label: 'AA', }
  • .getItemByName(valueOfName)
    根据枚举配置对象中字段 name 的值,获取对应的枚举配置项。

    import { createEnumObject } from 'ts-enum-object';
    
    const TestEnum = createEnumObject([
      { name: 'A', value: 1, label: 'AA', },
      { name: 'B', value: 2, label: 'BB', },
      { name: 'C', value: 3, label: 'CC', },
    ] as const); // as const is required
    
    TestEnum.getItemByName('A') // { name: 'A', value: 1, label: 'AA', }
  • .getItemByValue(valueOfValue)
    根据枚举配置对象中字段 value 的值,获取对应的枚举配置项。

    import { createEnumObject } from 'ts-enum-object';
    
    const TestEnum = createEnumObject([
      { name: 'A', value: 1, label: 'AA', },
      { name: 'B', value: 2, label: 'BB', },
      { name: 'C', value: 3, label: 'CC', },
    ] as const); // as const is required
    
    TestEnum.getItemByValue(1) // { name: 'A', value: 1, label: 'AA', }
  • .getLabel(valueOfNameOrValue)
    根据枚举配置对象中字段 name 或者 value 的值,获取对应的 label

    import { createEnumObject } from 'ts-enum-object';
    
    const TestEnum = createEnumObject([
      { name: 'A', value: 1, label: 'AA', },
      { name: 'B', value: 2, label: 'BB', },
      { name: 'C', value: 3, label: 'CC', },
    ] as const); // as const is required
    
    TestEnum.getLabel('A') // 'AA'
    TestEnum.getLabel(1) // 'AA'
  • EnumObjectNamesType<EnumObjectType>
    Typescript 类型方法,获取枚举对象的所有 Name 类型。

    type Names = EnumObjectNamesType<typeof TestEnum>; // A | B | C
  • EnumObjectValuesType<EnumObjectType>
    Typescript 类型方法,获取枚举对象的所有 Name 类型。

    type Values = EnumObjectValuesType<typeof TestEnum>; // 1 | 2 | 3
  • EnumObjectFieldValueType<EnumObjectType, FiledNameType>
    Typescript 类型方法,获取枚举对象的所有 Name 类型。

    type Names = EnumObjectFieldValueType<typeof TestEnum, 'name'>; // A | B | C