Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/beliefgp/ts-enum-object
解决 TypeScript 下枚举值无法为对象的一些应用场景
https://github.com/beliefgp/ts-enum-object
enums objectvalue typescript
Last synced: about 1 month ago
JSON representation
解决 TypeScript 下枚举值无法为对象的一些应用场景
- Host: GitHub
- URL: https://github.com/beliefgp/ts-enum-object
- Owner: beliefgp
- License: mit
- Created: 2021-01-11T07:50:44.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-12-22T02:55:53.000Z (almost 2 years ago)
- Last Synced: 2024-09-18T09:35:42.027Z (3 months ago)
- Topics: enums, objectvalue, typescript
- Language: TypeScript
- Homepage:
- Size: 26.4 KB
- Stars: 8
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-github-star - ts-enum-object
README
# ts-enum-object
解决 TypeScript 下枚举值无法为对象,以及保证枚举值有序性的一些应用场景,并提供了完整的 TypeScript 类型映射。
## 安装
```bash
$ npm i ts-enum-object --save
```## API
* createEnumObject(items: Array<{ name: string; value: any; label?: string; ...other }>)
创建一个枚举对象,`name` 与 `value` 为必填,`name` 代表枚举对象的 key,其他字段可以按业务需求扩展。**TypeScript 下入参的数组【务必】【务必】【务必】加 `as const`,否则无法正常推导出 Key 的类型**
```ts
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 requiredTestEnum.A // 1
TestEnum.B // 2
TestEnum.C // 3
```* .keys()
获取枚举所有 `name`。```ts
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 requiredTestEnum.keys() // ['A', 'B', 'C']
```* .values()
获取枚举所有 `value`。```ts
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 requiredTestEnum.values() // [1, 2, 3]
* .items()
获取枚举列表,也就是 createEnumObject 的入参。```ts
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 requiredTestEnum.items()
// [
// { name: 'A', value: 1, label: 'AA', },
// { name: 'B', value: 2, label: 'BB', },
// { name: 'C', value: 3, label: 'CC', },
// ]
```
* .getItemBy(key, valueOfKey)
根据枚举配置对象中某个字段的名字及其值,获取对应的枚举配置项(如果会出现重复,只返回第一个)。```ts
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 requiredTestEnum.getItemBy('label', 'AA') // { name: 'A', value: 1, label: 'AA', }
```* .getItemByName(valueOfName)
根据枚举配置对象中字段 `name` 的值,获取对应的枚举配置项。```ts
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 requiredTestEnum.getItemByName('A') // { name: 'A', value: 1, label: 'AA', }
```* .getItemByValue(valueOfValue)
根据枚举配置对象中字段 `value` 的值,获取对应的枚举配置项。```ts
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 requiredTestEnum.getItemByValue(1) // { name: 'A', value: 1, label: 'AA', }
```* .getLabel(valueOfNameOrValue)
根据枚举配置对象中字段 `name` 或者 `value` 的值,获取对应的 `label`。```ts
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 requiredTestEnum.getLabel('A') // 'AA'
TestEnum.getLabel(1) // 'AA'
```* EnumObjectNamesType\
Typescript 类型方法,获取枚举对象的所有 Name 类型。```ts
type Names = EnumObjectNamesType; // A | B | C
```* EnumObjectValuesType\
Typescript 类型方法,获取枚举对象的所有 Name 类型。```ts
type Values = EnumObjectValuesType; // 1 | 2 | 3
```* EnumObjectFieldValueType\
Typescript 类型方法,获取枚举对象的所有 Name 类型。```ts
type Names = EnumObjectFieldValueType; // A | B | C
```