Skip to content

Commit e263eed

Browse files
committed
docs: update documentation
1 parent 2198687 commit e263eed

File tree

2 files changed

+43
-43
lines changed

2 files changed

+43
-43
lines changed

README.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
⬇️  [Introduction](#introduction) | [Features](#features) | [Installation](#installation) | [Enum Initialization](#enum-initialization) | [API](#api) | [Usage](#usage) | [Naming Conventions](#naming-convention-best-practices) | [Localization](#localization) | [Extensibility](#extensibility) | [Compatibility](#compatibility) | [Q&A](#qa)  ⬇️
3131

32+
> `v3.0` is coming soon! Feel free to try the preview version [enum-plus@next](https://www.npmjs.com/package/enum-plus/v/next). The new version will bring more exciting features and improvements. For details, please refer to [v3 Release Notes](https://github.com/shijistar/enum-plus/issues/14).
33+
3234
## Introduction
3335

3436
`enum-plus` is an enhanced enum library that is fully compatible with the native `enum` and extends it with powerful features such as display text, localization, UI control binding, enum members traversal, and more useful extension methods. This lightweight, zero-dependency, TypeScript library works with any front-end framework.
@@ -85,39 +87,33 @@ yarn add enum-plus
8587

8688
This section shows the various ways to initialize enums using the `Enum` function. Understanding these different initialization formats allows you to choose the most convenient approach for your specific use case.
8789

88-
### 1. Simple `key-value` Format
90+
### 1. Simple Key-Value Format
8991

9092
The simplest format is a direct mapping of keys to values. This is similar to the native enum format.
9193

9294
```js
9395
import { Enum } from 'enum-plus';
9496

97+
// With number values
9598
const WeekEnum = Enum({
9699
Sunday: 0,
97100
Monday: 1,
98101
} as const);
99102

100103
WeekEnum.Monday; // 1
101-
```
102-
103-
> The `as const` type assertion is used to ensure that the enum values are treated as `literal` types, otherwise they will be treated as `number` types. If you are using JavaScript, please remove the `as const`.
104-
105-
### 2. Simple `key-value` with String Values
106-
107-
This format is similar to the first one, allowing you to use `string` values.
108-
109-
```js
110-
import { Enum } from 'enum-plus';
111104

112-
const WeekEnum = Enum({
105+
// With string values
106+
const WeekEnum2 = Enum({
113107
Sunday: 'Sun',
114108
Monday: 'Mon',
115109
} as const);
116110

117-
WeekEnum.Monday; // 'Mon'
111+
WeekEnum2.Monday; // 'Mon'
118112
```
119113
120-
### 3. Standard Format (Recommended)
114+
> The `as const` type assertion is used to ensure that the enum values are treated as `literal` types, otherwise they will be treated as `number` types. If you are using JavaScript, please remove the `as const`.
115+
116+
### 2. Standard Format (Recommended)
121117

122118
The standard format includes both a `value` and a `label` for each enum member. This is the most commonly used format and is recommended for most cases. This format allows you to specify a display text for each enum member, which can be used in UI components.
123119

@@ -133,7 +129,7 @@ WeekEnum.Sunday; // 0
133129
WeekEnum.label(0); // I love Sunday
134130
```
135131

136-
### 4. Label-Only Format
132+
### 3. Label-Only Format
137133

138134
This is useful when you want to use the key as the value.
139135

@@ -149,21 +145,25 @@ WeekEnum.Sunday; // 'Sunday'
149145
WeekEnum.label('Sunday'); // I love Sunday
150146
```
151147

152-
### 5. Array Format
148+
### 4. Array Format
153149

154150
The array format is useful when you need to create enums dynamically, such as from API data. This allows for flexibility in [custom field mapping](#custom-field-mapping) to adapt to different data structures.
155151

156152
```js
157153
import { Enum } from 'enum-plus';
158154
159-
const petTypes = await getPetsData();
160-
// [ { value: 1, key: 'dog', label: 'Dog' },
161-
// { value: 2, key: 'cat', label: 'Cat' },
162-
// { value: 3, key: 'rabbit', label: 'Rabbit' } ];
163-
const PetTypes = Enum(petTypes);
155+
const pets = [
156+
{ value: 1, key: 'Dog', label: 'Dog' },
157+
{ value: 2, key: 'Cat', label: 'Cat' },
158+
{ value: 3, key: 'Rabbit', label: 'Rabbit' },
159+
] as const;
160+
const PetEnum = Enum(pets);
161+
162+
PetEnum.Dog; // 1
163+
PetEnum.label(1); // Dog
164164
```
165165

166-
### 6. Native Enum Format
166+
### 5. Native Enum Format
167167

168168
You can also create from native enums. It benefits from the native enum's `auto-incrementing` behavior.
169169

README.zh-CN.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
⬇️  [简介](#简介) | [特性](#特性) | [安装](#安装) | [枚举定义](#枚举定义) | [API](#api) | [用法](#用法) | [命名规范建议](#命名规范建议) | [本地化](#本地化) | [全局扩展](#全局扩展) | [兼容性](#兼容性) | [常见问题](#常见问题) | [贡献](#贡献)  ⬇️
3131

32+
> `v3.0` 即将发布,欢迎试用预览版 [enum-plus@next](https://www.npmjs.com/package/enum-plus/v/next),新版本将带来更多令人兴奋的功能和改进,详情请参考 [v3 版本更新](https://github.com/shijistar/enum-plus/issues/14)
33+
3234
## 简介
3335

3436
`enum-plus`是一个增强版的枚举类库,完全兼容原生`enum`的基本用法,同时支持扩展显示文本、绑定到 UI 组件以及提供丰富的扩展方法,是原生`enum`的一个直接替代品。它是一个轻量级、零依赖、100% TypeScript 实现的工具,适用于多种前端框架,并支持本地化。
@@ -90,32 +92,26 @@ yarn add enum-plus
9092
```js
9193
import { Enum } from 'enum-plus';
9294

95+
// Number 类型
9396
const WeekEnum = Enum({
9497
Sunday: 0,
9598
Monday: 1,
9699
} as const);
97100

98101
WeekEnum.Monday; // 1
99-
```
100-
101-
> `as const` 类型断言用于将枚举值变成字面量类型,类型更精确,否则它们将被作为`number`类型。如果你使用的是JavaScript,请删除`as const`。
102-
103-
### 2. 基础格式,String 类型
104-
105-
与第一种方式类似,只不过枚举值是字符串类型
106-
107-
```js
108-
import { Enum } from 'enum-plus';
109102

110-
const WeekEnum = Enum({
103+
// String 类型
104+
const WeekEnum2 = Enum({
111105
Sunday: 'Sun',
112106
Monday: 'Mon',
113107
} as const);
114108

115-
WeekEnum.Monday; // 'Mon'
109+
WeekEnum2.Monday; // 'Mon'
116110
```
117111
118-
### 3. 标准格式(推荐)
112+
> `as const` 类型断言用于将枚举值变成字面量类型,类型更精确,否则它们将被作为`number`类型。如果你使用的是JavaScript,请删除`as const`。
113+
114+
### 2. 标准格式(推荐)
119115

120116
为每个枚举项指定 `value` (枚举值) 和 `label`(显示文本)字段,这是最常用的格式,也是推荐的格式。这种格式允许你为每个枚举项设置显示文本,这些文本可以在UI组件中使用。
121117

@@ -131,7 +127,7 @@ WeekEnum.Sunday; // 0
131127
WeekEnum.label(0); // 星期日
132128
```
133129

134-
- ### 4. Label-Only 格式
130+
### 3. Label-Only 格式
135131

136132
当你希望使用`key`作为枚举值时,这种方式比较有用,此时`value``key`的值相同,`label`是显示文本
137133

@@ -147,21 +143,25 @@ WeekEnum.Sunday; // 'Sunday'
147143
WeekEnum.label('Sunday'); // 星期日
148144
```
149145

150-
### 5. 数组格式
146+
### 4. 数组格式
151147

152148
数组格式在需要动态创建枚举时很有用,例如从 API 获取数据中动态创建一个枚举。这种方式还允许[自定义字段映射](#自定义字段映射),这增加了灵活性,可以适配不同的数据格式
153149

154150
```js
155151
import { Enum } from 'enum-plus';
156152
157-
const petTypes = await getPetsData();
158-
// [ { value: 1, key: 'dog', label: '狗' },
159-
// { value: 2, key: 'cat', label: '猫' },
160-
// { value: 3, key: 'rabbit', label: '兔子' } ];
161-
const PetTypes = Enum(petTypes);
153+
const pets = [
154+
{ value: 1, key: 'Dog', label: '狗' },
155+
{ value: 2, key: 'Cat', label: '猫' },
156+
{ value: 3, key: 'Rabbit', label: '兔子' },
157+
] as const;
158+
const PetEnum = Enum(pets);
159+
160+
PetEnum.Dog; // 1
161+
PetEnum.label(1); // 狗
162162
```
163163

164-
### 6. 原生枚举格式
164+
### 5. 原生枚举格式
165165

166166
如果你已经有一个原生的枚举,你可以直接传递给`Enum`函数,它会自动转换为增强版的枚举,这样可以借用原生枚举的`枚举值自动递增`特性
167167

0 commit comments

Comments
 (0)