Skip to content

Commit e30efb1

Browse files
committed
docs: update documentation
1 parent 94fe858 commit e30efb1

File tree

5 files changed

+52
-53
lines changed

5 files changed

+52
-53
lines changed

README.md

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ What other exciting features are there? Please continue to explore! Or you can c
6060

6161
## Features
6262

63-
- Fully compatible with native `enum` usage
63+
- Compatible with the usage of native enums
6464
- Supports multiple data types such as `number` and `string`
6565
- Enhanced enum items with display names
6666
- Internationalization support for display names, can be integrated with any i18n library
@@ -174,7 +174,8 @@ const WeekEnum = Enum({
174174
});
175175

176176
WeekEnum.Sunday; // 0
177-
WeekEnum.label(0); // I love Sunday
177+
WeekEnum.items[0].key; // 'Sunday'
178+
WeekEnum.items[0].label; // I love Sunday
178179
```
179180

180181
### 3. Label-Only Format
@@ -198,7 +199,7 @@ WeekEnum.items[0].label; // I love Sunday
198199

199200
The array format is useful when you need to create enums with dynamic data, for example the data from an API.
200201

201-
You can use dynamic field mapping rules to adapt to various different data structures. Please refer to the [Custom Field Mapping](#-custom-field-mapping-in-array-format-initialization) section for more details.
202+
You can also use dynamic field mapping rules to adapt to various different data structures. Please refer to the [Custom Field Mapping](#-custom-field-mapping-in-array-format-initialization) section for more details.
202203

203204
```js
204205
import { Enum } from 'enum-plus';
@@ -216,7 +217,7 @@ PetEnum.label(1); // Dog
216217

217218
### 5. Native Enum Format
218219

219-
You can also create from native enums. It benefits from the native enum's `auto-incrementing` behavior.
220+
Create from native enums. It benefits from the native enum's `auto-incrementing` behavior.
220221

221222
```ts
222223
import { Enum } from 'enum-plus';
@@ -290,7 +291,7 @@ WeekEnum.values; // [0, 1, 2, 3, 4, 5, 6]
290291

291292
`string[]`
292293

293-
Returns an array of all enum item `label`(s).
294+
Returns an array of all enum item `label`(s). If [localization](#localization) has been enabled, the localized texts of each enum item will be returned.
294295

295296
```js
296297
WeekEnum.labels; // ['Sunday', 'Monday', ... 'Friday', 'Saturday']
@@ -325,7 +326,7 @@ const ColorEnum = Enum({
325326
ColorEnum.meta.hex; // ['#FF0000', '#00FF00', '#0000FF']
326327
```
327328

328-
By the way, you can quickly access custom fields of a single enum item through the `named` property.
329+
Aditionally, you can quickly access custom fields of an enum item through the `named` and `raw` property.
329330

330331
```js
331332
ColorEnum.named.Red.raw.hex; // '#FF0000'
@@ -609,7 +610,7 @@ Enum.install(i18nextPlugin);
609610

610611
## User Stories
611612

612-
#### 💡 Prevent Magic Numbers
613+
### 💡 Prevent Magic Numbers
613614

614615
```js
615616
const WeekEnum = Enum({
@@ -626,7 +627,7 @@ if (today === WeekEnum.Sunday) {
626627

627628
---
628629

629-
#### 💡 Check for Valid Enum Values
630+
### 💡 Check for Valid Enum Values
630631

631632
```js
632633
if (WeekEnum.has(value)) {
@@ -638,7 +639,7 @@ if (WeekEnum.has(value)) {
638639

639640
---
640641

641-
#### 💡 Check for Enum Objects
642+
### 💡 Check for Enum Objects
642643

643644
```ts
644645
let values: number[] | undefined;
@@ -653,9 +654,9 @@ if (Enum.isEnum(data)) {
653654

654655
---
655656

656-
#### 💡 Generate UI Controls
657+
### 💡 Generate UI Controls
657658

658-
Take React and Ant Design as an example. Please refer to the [Supports Various Frontend Frameworks](#-supports-various-frontend-frameworks) section for more examples.
659+
Take React and Ant Design as examples. Please refer to the [Supports Various Frontend Frameworks](#-supports-various-frontend-frameworks) section for more examples.
659660

660661
```jsx
661662
import { Menu, Select, Table } from 'antd';
@@ -678,9 +679,9 @@ const App = () => {
678679
679680
---
680681

681-
#### 💡 Internationalization for Enum Names and Labels
682+
### 💡 Internationalization for Enum Names and Labels
682683

683-
Support internationalization. Set the `label` field to a localization key, so that it displays the corresponding text based on the current language environment. Please refer to the [Localization](#localization) section for more details.
684+
Internationalization support . Set the `label` field to a localization key, so that it displays the corresponding text based on the current language environment. Please refer to the [Localization](#localization) section for more details.
684685

685686
```js
686687
WeekEnum.label(1); // Monday or 星期一, depending on the current locale
@@ -690,7 +691,9 @@ WeekEnum.name; // Week or 周, depending on the current locale
690691

691692
---
692693

693-
#### 💡 Type Narrowing &nbsp; <sup>**_\[TypeScript Only]_**</sup>
694+
### 💡 Type Narrowing &nbsp; <sup>**_\[TypeScript Only]_**</sup>
695+
696+
Type narrowing is a TypeScript-specific feature that allows you to use `valueType` to constrain the types of variables, function parameters, or component props. This prevents invalid assignments and enhances type safety in your code.
694697

695698
- Variables
696699

@@ -734,7 +737,7 @@ const MyComponent = (props: MyComponentProps) => {
734737

735738
---
736739

737-
#### 💡 Support Metadata Fields That Can Serve as a Static Configuration System
740+
### 💡 Support Metadata Fields That Can Serve as a Static Configuration System
738741

739742
```js
740743
const ColorEnum = Enum({
@@ -753,7 +756,7 @@ ColorEnum.named.Red.raw.icon; // '🔥'
753756

754757
---
755758

756-
#### Supports Traversing Enum Items in a Read-Only Manner
759+
### 💡 Supports Traversing Enum Items in a Read-Only Manner
757760

758761
```js
759762
Array.isArray(WeekEnum.items); // true
@@ -772,7 +775,7 @@ WeekEnum.items[0].label = 'foo'; // ❌ Not allowed, read-only
772775

773776
---
774777

775-
#### 💡 Enum Composition (Merging)
778+
### 💡 Enum Composition (Merging)
776779

777780
```js
778781
const PrimaryColorEnum = Enum({
@@ -793,9 +796,9 @@ const AllColorEnum = Enum({
793796

794797
---
795798

796-
#### 💡 Supports JSDoc Comments on Enum Items, Enabling Code Intelligence
799+
### 💡 Supports JSDoc Comments on Enum Items, Enabling Code Intelligence
797800

798-
Supports inline documentation through JSDoc, allowing engineers to view detailed comments by simply hovering over enum values in the editor. Please refer to the [Best Practices](./docs/best-practices.md) section for writing good code.
801+
Supports inline documentation through JSDoc, allowing engineers to view detailed comments by simply hovering over enum values in the editor. Please refer to the [Best Practices](./docs/best-practices.md) section for how to write good code.
799802

800803
```js
801804
const WeekEnum = Enum({
@@ -810,13 +813,13 @@ WeekEnum.Monday; // Hover over Monday
810813

811814
![jsdoc](./public/jsdoc-en.png)
812815

813-
You can see that this hover functionality reveals both documentation and enum values simultaneously, without leaving your current position in the code.
816+
We can see that both the enumeration value and the description of the enumeration item can be displayed at the same time, when the cursor hovers over an enumeration item. There is no need to jump away from the current position in the code to check the definitions.
814817

815818
---
816819

817-
#### 💡 Supports Various Frontend Frameworks
820+
### 💡 Supports Various Frontend Frameworks
818821

819-
`Enum.items` can be consumed as control data sources (using Select as an example).
822+
`Enum.items` can be consumed as the data source of UI components. Take `Select` component as examples.
820823

821824
- **React-based UI libraries**
822825

@@ -876,10 +879,10 @@ You can see that this hover functionality reveals both documentation and enum va
876879

877880
[Angular Material](https://material.angular.io/components/select/overview) Select
878881

879-
```jsx
882+
```html
880883
<mat-select>
881884
@for (item of WeekEnum.items; track item.value) {
882-
<mat-option [value]="item.value">{{ item.label }}</mat-option>
885+
<mat-option [value]="item.value">{{ item.label }}</mat-option>
883886
}
884887
</mat-select>
885888
```
@@ -896,7 +899,7 @@ You can see that this hover functionality reveals both documentation and enum va
896899

897900
---
898901

899-
#### 💡 Custom Field Mapping in Array Format Initialization
902+
### 💡 Custom Field Mapping in Array Format Initialization
900903

901904
In [4. Array Format](#4-array-format) section, we know that you can build an enum from dynamic data from the backend, but it is very likely that the field names of dynamic data are not `value`, `label`, `key`, but other field names. In this case, you can pass in a custom option to map these to other field names.
902905

@@ -1013,7 +1016,7 @@ This plugin also supports custom i18next options, and even allows complete contr
10131016

10141017
If you need to automatically update the UI after switching languages, this requires the capabilities of frameworks like React, Vue, or Angular. Please consider using plugins such as [@enum-plus/plugin-react](https://github.com/shijistar/enum-plus/tree/main/packages/plugin-react) or [@enum-plus/plugin-vue](https://github.com/shijistar/enum-plus/tree/main/packages/plugin-vue).
10151018

1016-
If you are using other internationalization libraries, such as `react-intl`, `vue-i18next`, or `ngx-translate`, you can integrate these libraries through the `Enum.localize` method.
1019+
If you are using other internationalization libraries, such as `react-intl`, `vue-i18next`, or `ngx-translate`, you can integrate these libraries by overwritting the `Enum.localize` method.
10171020

10181021
_my-extension.js_
10191022

README.zh-CN.md

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ const WeekEnum = Enum({
172172
});
173173

174174
WeekEnum.Sunday; // 0
175-
WeekEnum.label(0); // 星期日
175+
WeekEnum.items[0].key; // 'Sunday'
176+
WeekEnum.items[0].label; // 星期日
176177
```
177178

178179
### 3. Key-Label 格式
@@ -326,7 +327,7 @@ const ColorEnum = Enum({
326327
ColorEnum.meta.hex; // ['#FF0000', '#00FF00', '#0000FF']
327328
```
328329

329-
顺便一提,可以通过`named`属性快速访问单个枚举项的自定义字段
330+
顺便一提,可以通过 `named``raw` 属性快速访问单个枚举项的自定义字段
330331

331332
```js
332333
ColorEnum.named.Red.raw.hex; // '#FF0000'
@@ -603,7 +604,7 @@ Enum.install(i18nextPlugin);
603604

604605
## 使用案例
605606

606-
#### 💡 基础用法,消除魔法数字
607+
### 💡 基础用法,消除魔法数字
607608

608609
```js
609610
const WeekEnum = Enum({
@@ -620,7 +621,7 @@ if (today === WeekEnum.Sunday) {
620621

621622
---
622623

623-
#### 💡 检查是否一个有效的枚举值
624+
### 💡 检查是否一个有效的枚举值
624625

625626
```js
626627
if (WeekEnum.has(value)) {
@@ -632,7 +633,7 @@ if (WeekEnum.has(value)) {
632633

633634
---
634635

635-
#### 💡 检查是否一个枚举对象
636+
### 💡 检查是否一个枚举对象
636637

637638
```ts
638639
let values: number[] | undefined;
@@ -647,7 +648,7 @@ if (Enum.isEnum(data)) {
647648

648649
---
649650

650-
#### 💡 生成 UI 组件
651+
### 💡 生成 UI 组件
651652

652653
以 React + Ant Design 为例,更多 UI 组件的案例请参考 [支持多种前端框架](#-支持多种前端框架) 章节
653654

@@ -672,7 +673,7 @@ const App = () => {
672673
673674
---
674675

675-
#### 💡 枚举名称/标签支持国际化
676+
### 💡 枚举名称/标签支持国际化
676677

677678
可以支持多语言环境,将`label`字段设置为一个本地化键值,根据当前语言环境显示对应的文本。请参考 [本地化](#本地化) 章节,了解更多详情。
678679

@@ -684,7 +685,9 @@ WeekEnum.name; // Week 或 周,取决于当前语言环境
684685

685686
---
686687

687-
#### 约束数据类型 (仅 TypeScript)
688+
### 💡 约束数据类型 (仅 TypeScript)
689+
690+
这是一个 TypeScript 特有的特性,可以使用 `valueType` 约束变量、方法参数或组件属性的类型,防止无效赋值,提高代码的类型安全性。
688691

689692
- 变量
690693

@@ -728,7 +731,7 @@ const MyComponent = (props: MyComponentProps) => {
728731

729732
---
730733

731-
#### 💡 添加元数据字段,可以作为静态配置系统使用
734+
### 💡 添加元数据字段,可以作为静态配置系统使用
732735

733736
```js
734737
const ColorEnum = Enum({
@@ -747,7 +750,7 @@ ColorEnum.named.Red.raw.icon; // '🔥'
747750

748751
---
749752

750-
#### 支持遍历枚举项数组,但不可修改
753+
### 💡 支持遍历枚举项数组,但不可修改
751754

752755
```js
753756
Array.isArray(WeekEnum.items); // true
@@ -766,7 +769,7 @@ WeekEnum.items[0].label = 'foo'; // ❌ 不可修改
766769

767770
---
768771

769-
#### 💡 枚举组合(合并)
772+
### 💡 枚举组合(合并)
770773

771774
```js
772775
const PrimaryColorEnum = Enum({
@@ -787,7 +790,7 @@ const AllColorEnum = Enum({
787790

788791
---
789792

790-
#### 枚举项支持 Jsdoc 注释,启用代码智能提示
793+
### 💡 枚举项支持 Jsdoc 注释,启用代码智能提示
791794

792795
在代码编辑器中,将光标悬停在枚举项上,即可显示关于该枚举项的详细 Jsdoc 注释,而不必再转到枚举定义处查看。关于如何编写良好的代码,请参考 [最佳实践](./docs/best-practices.md) 章节。
793796

@@ -804,11 +807,11 @@ WeekEnum.Monday; // 将光标悬浮在 Monday 上
804807

805808
![jsdoc](./public/jsdoc-chs.png)
806809

807-
可以看到,不但提示了枚举项的释义,还有枚举项的值,无需跳转离开当前光标位置,在阅读代码时非常方便
810+
可以看到,当光标悬浮在枚举项上时,可以同时显示枚举值和枚举项的介绍。无需跳转离开当前光标位置,去查看枚举的定义,这在阅读代码时非常方便
808811

809812
---
810813

811-
#### 💡 支持多种前端框架
814+
### 💡 支持多种前端框架
812815

813816
`Enum.items` 可以直接作为组件的数据源(以 Select 组件为例)
814817

@@ -871,10 +874,10 @@ WeekEnum.Monday; // 将光标悬浮在 Monday 上
871874

872875
[Angular Material](https://material.angular.io/components/select/overview) Select
873876

874-
```jsx
877+
```html
875878
<mat-select>
876879
@for (item of WeekEnum.items; track item.value) {
877-
<mat-option [value]="item.value">{{ item.label }}</mat-option>
880+
<mat-option [value]="item.value">{{ item.label }}</mat-option>
878881
}
879882
</mat-select>
880883
```
@@ -891,7 +894,7 @@ WeekEnum.Monday; // 将光标悬浮在 Monday 上
891894

892895
---
893896

894-
#### 数组格式初始化,设置不同的字段映射
897+
### 💡 数组格式初始化,设置不同的字段映射
895898

896899
[4. 数组格式](#4-数组格式) 章节中,介绍了可以通过后端动态数据来构建枚举,但是很可能动态数据的字段名并不是`value``label``key`,而是其它的字段名。这时你可以传入一个自定义选项,把这些映射到其它字段名上
897900

package.json

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"keywords": [
66
"enum",
77
"enumeration",
8-
"tool",
98
"javascript",
109
"typescript",
1110
"front-end",
@@ -14,20 +13,14 @@
1413
"browser",
1514
"mini-program",
1615
"react-native",
17-
"assembly-script",
1816
"ui-binding",
1917
"ssr",
2018
"localization",
2119
"globalization",
2220
"plugin-system",
2321
"react",
2422
"vue",
25-
"angular",
26-
"ant-design",
27-
"arco-design",
28-
"material-ui",
29-
"vuetify",
30-
"ng-zorro"
23+
"angular"
3124
],
3225
"homepage": "https://github.com/shijistar/enum-plus",
3326
"bugs": {

packages/plugin-react/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@enum-plus/react",
2+
"name": "@enum-plus/plugin-react",
33
"version": "1.0.0-beta.0",
44
"description": "A plugin for enum-plus that provides React components and hooks for enum handling",
55
"keywords": [

scripts/add-umd-banner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { existsSync, readFileSync, writeFileSync } from 'fs';
22
import packageJson from '../package.json';
33

44
const headerComment = (legacy: boolean) => `/**
5-
* ${packageJson.name} (${legacy ? 'ES2015' : 'ES2020'} version)
5+
* ${packageJson.name} (${legacy ? 'ES2015' : 'ES2020'} compatible})
66
* ${packageJson.description}
77
*
88
* @version: ${packageJson.version}

0 commit comments

Comments
 (0)