You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+29-26Lines changed: 29 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -60,7 +60,7 @@ What other exciting features are there? Please continue to explore! Or you can c
60
60
61
61
## Features
62
62
63
-
-Fully compatible with native `enum`usage
63
+
-Compatible with the usage of native enums
64
64
- Supports multiple data types such as `number` and `string`
65
65
- Enhanced enum items with display names
66
66
- Internationalization support for display names, can be integrated with any i18n library
@@ -174,7 +174,8 @@ const WeekEnum = Enum({
174
174
});
175
175
176
176
WeekEnum.Sunday; // 0
177
-
WeekEnum.label(0); // I love Sunday
177
+
WeekEnum.items[0].key; // 'Sunday'
178
+
WeekEnum.items[0].label; // I love Sunday
178
179
```
179
180
180
181
### 3. Label-Only Format
@@ -198,7 +199,7 @@ WeekEnum.items[0].label; // I love Sunday
198
199
199
200
The array format is useful when you need to create enums with dynamic data, for example the data from an API.
200
201
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.
202
203
203
204
```js
204
205
import { Enum } from'enum-plus';
@@ -216,7 +217,7 @@ PetEnum.label(1); // Dog
216
217
217
218
### 5. Native Enum Format
218
219
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.
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.
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.
329
330
330
331
```js
331
332
ColorEnum.named.Red.raw.hex; // '#FF0000'
@@ -609,7 +610,7 @@ Enum.install(i18nextPlugin);
609
610
610
611
## User Stories
611
612
612
-
####💡 Prevent Magic Numbers
613
+
### 💡 Prevent Magic Numbers
613
614
614
615
```js
615
616
constWeekEnum=Enum({
@@ -626,7 +627,7 @@ if (today === WeekEnum.Sunday) {
626
627
627
628
---
628
629
629
-
####💡 Check for Valid Enum Values
630
+
### 💡 Check for Valid Enum Values
630
631
631
632
```js
632
633
if (WeekEnum.has(value)) {
@@ -638,7 +639,7 @@ if (WeekEnum.has(value)) {
638
639
639
640
---
640
641
641
-
####💡 Check for Enum Objects
642
+
### 💡 Check for Enum Objects
642
643
643
644
```ts
644
645
let values:number[] |undefined;
@@ -653,9 +654,9 @@ if (Enum.isEnum(data)) {
653
654
654
655
---
655
656
656
-
####💡 Generate UI Controls
657
+
### 💡 Generate UI Controls
657
658
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.
659
660
660
661
```jsx
661
662
import { Menu, Select, Table } from'antd';
@@ -678,9 +679,9 @@ const App = () => {
678
679
679
680
---
680
681
681
-
####💡 Internationalization for Enum Names and Labels
682
+
### 💡 Internationalization for Enum Names and Labels
682
683
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.
684
685
685
686
```js
686
687
WeekEnum.label(1); // Monday or 星期一, depending on the current locale
@@ -690,7 +691,9 @@ WeekEnum.name; // Week or 周, depending on the current locale
690
691
691
692
---
692
693
693
-
#### 💡 Type Narrowing <sup>**_\[TypeScript Only]_**</sup>
694
+
### 💡 Type Narrowing <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.
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.
799
802
800
803
```js
801
804
constWeekEnum=Enum({
@@ -810,13 +813,13 @@ WeekEnum.Monday; // Hover over Monday
810
813
811
814

812
815
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.
814
817
815
818
---
816
819
817
-
####💡 Supports Various Frontend Frameworks
820
+
### 💡 Supports Various Frontend Frameworks
818
821
819
-
`Enum.items` can be consumed as control data sources (using Selectas an example).
822
+
`Enum.items` can be consumed as the data source of UI components. Take `Select` component as examples.
820
823
821
824
-**React-based UI libraries**
822
825
@@ -876,10 +879,10 @@ You can see that this hover functionality reveals both documentation and enum va
@@ -896,7 +899,7 @@ You can see that this hover functionality reveals both documentation and enum va
896
899
897
900
---
898
901
899
-
####💡 Custom Field Mapping in Array Format Initialization
902
+
### 💡 Custom Field Mapping in Array Format Initialization
900
903
901
904
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.
902
905
@@ -1013,7 +1016,7 @@ This plugin also supports custom i18next options, and even allows complete contr
1013
1016
1014
1017
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).
1015
1018
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.
0 commit comments