Skip to content

Commit 7eec20b

Browse files
committed
docs(core): update README
1 parent 871a576 commit 7eec20b

File tree

2 files changed

+52
-46
lines changed

2 files changed

+52
-46
lines changed

README.md

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ What other exciting features are there? Please continue to explore! Or you can c
7474
- Compatible with any front-end development framework, including vanilla projects
7575
- TypeScript‑oriented, providing excellent type inference and code completion capabilities
7676
- Zero dependencies
77-
- Lightweight (2KB+ minzipped only)
77+
- Lightweight (min+gzip 2KB+ only)
7878

7979
## Installation
8080

@@ -180,7 +180,7 @@ WeekEnum.items[0].label; // I love Sunday
180180

181181
### 3. Label-Only Format
182182

183-
Create an enumeration that only provides the `key` and `label` fields. If the `value` field is omitted, it defaults to the same value as the key  field.
183+
Create an enumeration that only provides the `key` and `label` fields. If the `value` field is omitted, it defaults to the same value as the `key` field.
184184

185185
```js
186186
import { Enum } from 'enum-plus';
@@ -222,7 +222,7 @@ Create from native enums. It benefits from the native enum's `auto-incrementing`
222222
```ts
223223
import { Enum } from 'enum-plus';
224224

225-
enum init {
225+
enum WeekNative {
226226
Sunday = 0,
227227
Monday,
228228
Tuesday,
@@ -231,7 +231,7 @@ enum init {
231231
Friday,
232232
Saturday,
233233
}
234-
const WeekEnum = Enum(init);
234+
const WeekEnum = Enum(WeekNative);
235235

236236
WeekEnum.Sunday; // 0
237237
WeekEnum.Monday; // 1
@@ -270,7 +270,7 @@ WeekEnum.named.Monday; // { key: 'Monday', value: 1, label: 'Monday' }
270270
Returns a read-only array of all enum items.
271271

272272
```js
273-
WeekEnum.items; // [ { value: 0, label: 'Sunday', key: 'Sunday' }, { value: 1, label: 'Monday', key: 'Monday' }, ... ]
273+
WeekEnum.items; // [ { value: 0, label: 'Sunday', key: 'Sunday' }, { value: 1, label: 'Monday', key: 'Monday' }, ... ]
274274
```
275275

276276
---
@@ -326,7 +326,7 @@ const ColorEnum = Enum({
326326
ColorEnum.meta.hex; // ['#FF0000', '#00FF00', '#0000FF']
327327
```
328328

329-
Aditionally, you can quickly access custom fields of an enum item through the `named` and `raw` property.
329+
Additionally, you can quickly access custom fields of an enum item through the `named` and `raw` properties.
330330

331331
```js
332332
ColorEnum.named.Red.raw.hex; // '#FF0000'
@@ -359,8 +359,8 @@ The `field` parameter can be one of the built-in fields: `key`, `value`, `label`
359359

360360
```js
361361
ColorEnum.findBy('value', 1); // { key: 'Red', value: 1, label: 'Red', hex: '#FF0000' }
362-
WeekEnum.findBy('key', 'Red'); // { key: 'Red', value: 1, label: 'Red' }
363-
WeekEnum.findBy('hex', '#FF0000'); // { key: 'Red', value: 1, label: 'Red', hex: '#FF0000' }
362+
ColorEnum.findBy('key', 'Red'); // { key: 'Red', value: 1, label: 'Red', hex: '#FF0000' }
363+
ColorEnum.findBy('hex', '#FF0000'); // { key: 'Red', value: 1, label: 'Red', hex: '#FF0000' }
364364
```
365365

366366
> If you need to get the meta fields of a known enum item, it is recommended to use the `named` and `raw` property, for example: `ColorEnum.named.Red.raw.hex`.
@@ -431,15 +431,17 @@ Converts the enum items to an array of objects, each containing `value` and `lab
431431
```js
432432
WeekEnum.toList();
433433
// [
434+
// { value: 0, label: 'Sunday' },
434435
// { value: 1, label: 'Monday' },
435-
// { value: 2, label: 'Tuesday' },
436436
// ...
437+
// { value: 6, label: 'Saturday' }
437438
// ]
438439
WeekEnum.toList({ valueField: 'id', labelField: 'name' });
439440
// [
441+
// { id: 0, name: 'Sunday' },
440442
// { id: 1, name: 'Monday' },
441-
// { id: 2, name: 'Tuesday' },
442443
// ...
444+
// { id: 6, name: 'Saturday' }
443445
// ]
444446
```
445447

@@ -456,15 +458,17 @@ Converts the enum items to a key-value map object, where the keys are the enum v
456458
```js
457459
WeekEnum.toMap();
458460
// {
461+
// "0": 'Sunday',
459462
// "1": 'Monday',
460-
// "2": 'Tuesday',
461463
// ...
464+
// "6": 'Saturday'
462465
// }
463466
WeekEnum.toMap({ keySelector: 'key', valueSelector: 'value' });
464467
// {
468+
// "Sunday": 0,
465469
// "Monday": 1,
466-
// "Tuesday": 2,
467470
// ...
471+
// "Saturday": 6
468472
// }
469473
```
470474

@@ -682,7 +686,7 @@ const App = () => {
682686

683687
### 💡 Internationalization for Enum Names and Labels
684688

685-
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.
689+
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.
686690

687691
```js
688692
WeekEnum.label(1); // Monday or 星期一, depending on the current locale
@@ -704,8 +708,8 @@ type WeekValues = typeof WeekEnum.valueType; // 0 | 1 | ... | 5 | 6
704708
const weekValue: WeekValues = 1; // ✅ Correct, 1 is a valid week enum value
705709
const weeks: WeekValues[] = [0, 1]; // ✅ Correct, 0 and 1 are valid week enum values
706710

707-
const badWeekValue: WeekValues = "Weekend"; // ❌ Type error, "Weekend" is not a number
708-
const badWeekValue: WeekValues = 8; // ❌ Error, 8 is not a valid week enum value
711+
const badWeekValue1: WeekValues = 'Weekend'; // ❌ Type error, "Weekend" is not a number
712+
const badWeekValue2: WeekValues = 8; // ❌ Error, 8 is not a valid week enum value
709713
const badWeeks: WeekValues[] = [0, 8]; // ❌ Error, 8 is not a valid week enum value
710714
```
711715

@@ -812,7 +816,7 @@ const WeekEnum = Enum({
812816
WeekEnum.Monday; // Hover over Monday
813817
```
814818

815-
![jsdoc](./public/jsdoc-en.png)
819+
![JSDoc](./public/jsdoc-en.png)
816820

817821
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.
818822

@@ -902,7 +906,7 @@ We can see that both the enumeration value and the description of the enumeratio
902906

903907
### 💡 Custom Field Mapping in Array Format Initialization
904908

905-
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.
909+
In the [4. Array Format](#4-array-format) section, we know that enums can be created from dynamic data arrays. However, the field names in the real world may be different from the default `value`, `label`, and `key`. In such cases, you can pass in a custom option to map these to other field names.
906910

907911
```js
908912
import { Enum } from 'enum-plus';
@@ -1017,7 +1021,7 @@ This plugin also supports custom i18next options, and even allows complete contr
10171021

10181022
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).
10191023

1020-
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.
1024+
If you are using other internationalization libraries, such as `react-intl`, `vue-i18next`, or `ngx-translate`, you can integrate these libraries by overwriting the `Enum.localize` method.
10211025

10221026
_my-extension.js_
10231027

@@ -1187,7 +1191,7 @@ When using `enum-plus`, following these best practices can help ensure consisten
11871191
1. **Enum Type Naming:** Use `PascalCase` and append with the `Enum` suffix (e.g., _WeekEnum_, _ColorEnum_).
11881192
2. **Enum Item Naming:** Use `PascalCase` for enum items (e.g., _WeekEnum.Sunday_, _ColorEnum.Red_). This naming style highlights the immutability and static nature of enum items, and ensures they appear at the top in IDE IntelliSense suggestions for easier selection.
11891193
3. **Semantic Clarity:** Ensure enum and item names have clear semantics. Good semantic naming serves as self-documentation, making code intent explicit and reducing cognitive overhead.
1190-
4. **Single Responsibility Principle:** Each enum type should represent a single, cohesive set of related constants. Avoiding overlapping responsibilities between different enum types.
1194+
4. **Single Responsibility Principle:** Each enum type should represent a single, cohesive set of related constants. Avoid overlapping responsibilities between different enum types.
11911195
5. **Provide JSDoc Comments:** Provide JSDoc comments for each enum item and the enum type itself, explaining their purpose and usage. Comprehensive documentation enables IDE hover tooltips and improves code readability and maintainability.
11921196
6. **Internationalization Architecture:** Plan for internationalization from the outset by leveraging the library's [localization](#localization) features. A well-designed internationalization architecture minimizes future refactoring and facilitates global scalability.
11931197

@@ -1250,7 +1254,7 @@ In Node.js environments, you can import enum-plus using either `require` or `imp
12501254

12511255
### Why do I need this library? TypeScript already has the built-in enums
12521256

1253-
TypeScript's built-in enum only provides the basic functionality of [Enumeration](https://en.wikipedia.org/wiki/Enumerated_type): eliminating magic numbers, and regulating control flow. However, as a front-end engineer, the needs for enumerations are not merely these. We also need:
1257+
TypeScript's built-in enum only provides the basic functionality of [Enumeration](https://en.wikipedia.org/wiki/Enumerated_type): eliminating magic numbers and structuring control flow (e.g. with if / switch). However, as a front-end engineer, the needs for enumerations are not merely these. We also need:
12541258

12551259
1. _Eliminate magic numbers_
12561260
2. _Used in the `if` or `switch` statements for conditional branching_
@@ -1316,7 +1320,7 @@ const WeekEnum = Enum({
13161320

13171321
As you can see, in earlier versions of TypeScript, you may need to use the `as const` type assertion. `as const` allows the enum values to remain their original literal values instead of being converted to `number` or `string` types. Meanwhile, the `enum.valueType` will remain as `0 | 1` instead of becoming `number`. This makes TypeScript's type checking more accurate and enhances code safety. Additionally, please check your `tsconfig.json` file to ensure that the `moduleResolution` option is set to `node` or `node10`, which prevents the type declaration files of `enum-plus` from being automatically switched to the version of 5.0+.
13181322

1319-
If you are using JavaScript, you can leverage `jsdoc` to help the editor accurately recognize types.
1323+
If you are using JavaScript, you can leverage `JSDoc` to help the editor accurately recognize types.
13201324

13211325
```js
13221326
/** @type {{ Sunday: 0; Monday: 1 }} */

README.zh-CN.md

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@
7070
- 支持数据类型约束,提高代码的类型安全性<sup>_&nbsp;&nbsp;TypeScript_</sup>
7171
- 枚举可以生成下拉框等 UI 组件,支持 [Ant Design](https://ant-design.antgroup.com/components/overview-cn)[Element Plus](https://element-plus.org/zh-CN/component/select.html)[Material-UI](https://mui.com/material-ui) 等多种组件库
7272
- 支持 Web浏览器、Node.js、React Native、Taro、小程序等多种环境
73-
- 支持服务端渲染(SSR)
73+
- 支持服务端渲染 (SSR)
7474
- 兼容任何前端开发框架,支持无框架的纯原生项目
7575
- 面向 TypeScript 设计,具有良好的类型推导和代码补全能力
7676
- 零依赖项
77-
- 轻量(gzip压缩后仅 2KB+)
77+
- 轻量(gzip压缩后仅2KB+)
7878

7979
## 安装
8080

@@ -197,9 +197,6 @@ WeekEnum.items[0].label; // 星期日
197197

198198
数组格式在需要动态创建枚举时很有用,例如从 API 获取数据中动态创建一个枚举。
199199

200-
<!-- 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. -->
201-
<!-- 英文翻译 -->
202-
203200
你可以动态映射字段以适应各种不同的数据结构。请参考 [数组格式初始化,设置不同的字段映射](#数组格式初始化设置不同的字段映射) 章节,了解更多详情。
204201

205202
```js
@@ -208,7 +205,7 @@ import { Enum } from 'enum-plus';
208205
const pets = [
209206
{ value: 1, key: 'Dog', label: '' },
210207
{ value: 2, key: 'Cat', label: '' },
211-
{ value: 3, key: 'Rabbit', label: '兔子' },
208+
{ value: 3, key: 'Rabbit', label: '' },
212209
];
213210
const PetEnum = Enum(pets);
214211

@@ -223,7 +220,7 @@ PetEnum.label(1); // 狗
223220
```ts
224221
import { Enum } from 'enum-plus';
225222

226-
enum init {
223+
enum WeekNative {
227224
Sunday = 0,
228225
Monday,
229226
Tuesday,
@@ -232,7 +229,7 @@ enum init {
232229
Friday,
233230
Saturday,
234231
}
235-
const WeekEnum = Enum(init);
232+
const WeekEnum = Enum(WeekNative);
236233

237234
WeekEnum.Sunday; // 0
238235
WeekEnum.Monday; // 1
@@ -320,9 +317,9 @@ WeekEnum.keys; // ['Sunday', 'Monday', ... 'Friday', 'Saturday']
320317

321318
```js
322319
const ColorEnum = Enum({
323-
Red: { value: 1, label: 'Red', hex: '#FF0000' },
324-
Green: { value: 2, label: 'Green', hex: '#00FF00' },
325-
Blue: { value: 3, label: 'Blue', hex: '#0000FF' },
320+
Red: { value: 1, label: '红色', hex: '#FF0000' },
321+
Green: { value: 2, label: '绿色', hex: '#00FF00' },
322+
Blue: { value: 3, label: '蓝色', hex: '#0000FF' },
326323
});
327324
ColorEnum.meta.hex; // ['#FF0000', '#00FF00', '#0000FF']
328325
```
@@ -357,8 +354,9 @@ WeekEnum.has('Birthday'); // false
357354
字段名支持:`key``value``label`或元数据字段
358355

359356
```js
360-
WeekEnum.findBy('value', 1); // { key: 'Monday', value: 1, label: '星期一' }
361-
WeekEnum.findBy('key', 'Monday'); // { key: 'Monday', value: 1, label: '星期一' }
357+
ColorEnum.findBy('value', 1); // { key: 'Red', value: 1, label: '红色', hex: '#FF0000' }
358+
ColorEnum.findBy('key', 'Red'); // { key: 'Red', value: 1, label: '红色', hex: '#FF0000' }
359+
ColorEnum.findBy('hex', '#FF0000'); // { key: 'Red', value: 1, label: '红色', hex: '#FF0000' }
362360
```
363361

364362
---
@@ -427,15 +425,17 @@ WeekEnum.raw(); // { Sunday: { value: 0, label: '星期日', happy: true }, Mond
427425
```js
428426
WeekEnum.toList();
429427
// [
428+
// { value: 0, label: '星期日' },
430429
// { value: 1, label: '星期一' },
431-
// { value: 2, label: '星期二' },
432430
// ...
431+
// { value: 6, label: '星期六' }
433432
// ]
434433
WeekEnum.toList({ valueField: 'id', labelField: 'name' });
435434
// [
435+
// { id: 0, name: '星期日' },
436436
// { id: 1, name: '星期一' },
437-
// { id: 2, name: '星期二' },
438437
// ...
438+
// { id: 6, name: '星期六' }
439439
// ]
440440
```
441441

@@ -452,15 +452,17 @@ WeekEnum.toList({ valueField: 'id', labelField: 'name' });
452452
```js
453453
WeekEnum.toMap();
454454
// {
455+
// "0": '星期日',
455456
// "1": '星期一',
456-
// "2": '星期二',
457457
// ...
458+
// "6": '星期六'
458459
// }
459460
WeekEnum.toMap({ keySelector: 'key', valueSelector: 'value' });
460461
// {
462+
// "Sunday": 0,
461463
// "Monday": 1,
462-
// "Tuesday": 2,
463464
// ...
465+
// "Saturday": 6
464466
// }
465467
```
466468

@@ -697,8 +699,8 @@ type WeekValues = typeof WeekEnum.valueType; // 0 | 1 | ... | 5 | 6
697699
const weekValue: WeekValues = 1; // ✅ 正确,1 是一个有效的周枚举值
698700
const weeks: WeekValues[] = [0, 1]; // ✅ 正确,0 和 1 是有效的周枚举值
699701

700-
const badWeekValue: WeekValues = "Weekend"; // ❌ 类型错误,"Weekend" 不是数字
701-
const badWeekValue: WeekValues = 8; // ❌ 错误,8 不是一个有效的周枚举值
702+
const badWeekValue1: WeekValues = 'Weekend'; // ❌ 类型错误,"Weekend" 不是数字
703+
const badWeekValue2: WeekValues = 8; // ❌ 错误,8 不是一个有效的周枚举值
702704
const badWeeks: WeekValues[] = [0, 8]; // ❌ 错误,8 不是一个有效的周枚举值
703705
```
704706

@@ -790,9 +792,9 @@ const AllColorEnum = Enum({
790792

791793
---
792794

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

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

797799
```js
798800
const WeekEnum = Enum({
@@ -805,7 +807,7 @@ const WeekEnum = Enum({
805807
WeekEnum.Monday; // 将光标悬浮在 Monday 上
806808
```
807809

808-
![jsdoc](./public/jsdoc-chs.png)
810+
![JSDoc](./public/jsdoc-chs.png)
809811

810812
可以看到,当光标悬浮在枚举项上时,可以同时显示枚举值和枚举项的介绍。无需跳转离开当前光标位置,去查看枚举的定义,这在阅读代码时非常方便。
811813

@@ -894,7 +896,7 @@ WeekEnum.Monday; // 将光标悬浮在 Monday 上
894896

895897
---
896898

897-
### 💡 数组格式初始化,设置不同的字段映射
899+
### 💡 在数组初始化方式中,设置不同的字段映射
898900

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

@@ -1180,7 +1182,7 @@ WeekEnum[ITEMS].toList(); // 但可以通过 ITEMS 别名来访问它
11801182
2. **枚举成员命名:** 使用 `PascalCase` 大驼峰命名法,如 _WeekEnum.Sunday__ColorEnum.Red_ 等。此命名方式突显了枚举成员的不可变性与静态特性,且在IDE智能提示中会在顶部显示,更方便拾取。
11811183
3. **语义明确:** 确保枚举和成员名称具有清晰的语义,良好的语义命名能够自解释代码意图,降低理解成本。
11821184
4. **单一职责原则:** 每个枚举类型应专注表达一组高内聚的相关常量,避免不同枚举类型之间的职责重叠。
1183-
5. **提供JSDoc注释:** 为每个枚举项添加 Jsdoc 注释,说明其含义和用途。完善的JSDoc文档能在IDE中提供悬停提示,提升代码阅读体验。同样也建议为枚举类添加注释。
1185+
5. **提供JSDoc注释:** 为每个枚举项添加 JSDoc 注释,说明其含义和用途。完善的JSDoc文档能在IDE中提供悬停提示,提升代码阅读体验。同样也建议为枚举类添加注释。
11841186
6. **国际化架构:** 建议从开始就搭建国际化架构,可集成本库提供的 [本地化](#本地化) 机制。预先设计的国际化方案能够避免后期重构的高成本,并使应用更易于扩展到全球市场。
11851187

11861188
下面是一个示例,展示了如何结合上述最佳实践来定义一个枚举:
@@ -1308,7 +1310,7 @@ const WeekEnum = Enum({
13081310

13091311
可以看到,在较低版本的TypeScript中,你可能需要使用 `as const` 类型断言。`as const` 可以让枚举值保持原始的字面量值,而不会变成 `number``string` 类型,同时 `enum.valueType` 类型也会保持 `0 | 1`,而不会变成 `number` 类型。这让 TypeScript 的类型校验变得更准确,也可以提升代码的安全性。另外,请检查你的 `tsconfig.json` 文件,确保 `moduleResolution` 选项设置为 `node``node10`,避免 `enum-plus` 的类型声明文件被自动切换到 5.0+ 版本。
13101312

1311-
如果你使用的是 JavaScript,那么你可以借助 `jsdoc` 来让编辑器精确识别类型。
1313+
如果你使用的是 JavaScript,那么你可以借助 `JSDoc` 来让编辑器精确识别类型。
13121314

13131315
```js
13141316
/** @type {{ Sunday: 0; Monday: 1 }} */

0 commit comments

Comments
 (0)