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
+25-21Lines changed: 25 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -74,7 +74,7 @@ What other exciting features are there? Please continue to explore! Or you can c
74
74
- Compatible with any front-end development framework, including vanilla projects
75
75
- TypeScript‑oriented, providing excellent type inference and code completion capabilities
76
76
- Zero dependencies
77
-
- Lightweight (2KB+ minzipped only)
77
+
- Lightweight (min+gzip 2KB+ only)
78
78
79
79
## Installation
80
80
@@ -180,7 +180,7 @@ WeekEnum.items[0].label; // I love Sunday
180
180
181
181
### 3. Label-Only Format
182
182
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.
184
184
185
185
```js
186
186
import { Enum } from'enum-plus';
@@ -222,7 +222,7 @@ Create from native enums. It benefits from the native enum's `auto-incrementing`
> 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
### 💡 Internationalization for Enum Names and Labels
684
688
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.
686
690
687
691
```js
688
692
WeekEnum.label(1); // Monday or 星期一, depending on the current locale
const weekValue:WeekValues=1; // ✅ Correct, 1 is a valid week enum value
705
709
const weeks:WeekValues[] = [0, 1]; // ✅ Correct, 0 and 1 are valid week enum values
706
710
707
-
constbadWeekValue:WeekValues="Weekend"; // ❌ Type error, "Weekend" is not a number
708
-
constbadWeekValue:WeekValues=8; // ❌ Error, 8 is not a valid week enum value
711
+
constbadWeekValue1:WeekValues='Weekend'; // ❌ Type error, "Weekend" is not a number
712
+
constbadWeekValue2:WeekValues=8; // ❌ Error, 8 is not a valid week enum value
709
713
const badWeeks:WeekValues[] = [0, 8]; // ❌ Error, 8 is not a valid week enum value
710
714
```
711
715
@@ -812,7 +816,7 @@ const WeekEnum = Enum({
812
816
WeekEnum.Monday; // Hover over Monday
813
817
```
814
818
815
-

819
+

816
820
817
821
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.
818
822
@@ -902,7 +906,7 @@ We can see that both the enumeration value and the description of the enumeratio
902
906
903
907
### 💡 Custom Field Mapping in Array Format Initialization
904
908
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.
906
910
907
911
```js
908
912
import { Enum } from'enum-plus';
@@ -1017,7 +1021,7 @@ This plugin also supports custom i18next options, and even allows complete contr
1017
1021
1018
1022
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).
1019
1023
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.
1021
1025
1022
1026
_my-extension.js_
1023
1027
@@ -1187,7 +1191,7 @@ When using `enum-plus`, following these best practices can help ensure consisten
1187
1191
1.**Enum Type Naming:** Use `PascalCase` and append with the `Enum` suffix (e.g., _WeekEnum_, _ColorEnum_).
1188
1192
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.
1189
1193
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.
1191
1195
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.
1192
1196
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.
1193
1197
@@ -1250,7 +1254,7 @@ In Node.js environments, you can import enum-plus using either `require` or `imp
1250
1254
1251
1255
### Why do I need this library? TypeScript already has the built-in enums
1252
1256
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:
1254
1258
1255
1259
1._Eliminate magic numbers_
1256
1260
2._Used in the `if` or `switch` statements for conditional branching_
@@ -1316,7 +1320,7 @@ const WeekEnum = Enum({
1316
1320
1317
1321
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+.
1318
1322
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.
<!-- 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. -->
0 commit comments