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
+35-30Lines changed: 35 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -69,12 +69,12 @@ What other exciting features are there? Please continue to explore! Or you can c
69
69
- Plugin system design, extending enum functionality through plugin installations
70
70
- Supports type narrowing to enhance type safety<sup>_ TypeScript_</sup>
71
71
- Generates dropdowns from enums, compatible with UI libraries like [Ant Design](https://ant.design/components/overview), [Element Plus](https://element-plus.org/en-US/component/overview), [Material-UI](https://mui.com/material-ui) and more
72
-
- Supports server-side rendering (SSR)
73
72
- Compatible with various environments including Browsers, Node.js, React Native, Taro, and mini-programs
73
+
- Supports server-side rendering (SSR)
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 (only 2KB+ minzipped)
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 want to get the custom fields of a known enum item, it is recommended to use the `named` property to access it
366
+
> 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`.
Gets the display name of an enum item according to its value or key. If [localization](#localization)has been enabled, the localized text will be returned.
374
+
Gets the display name of an enum item according to its value or key. If [localization](#localization)is enabled, the localized text will be returned.
374
375
375
376
```js
376
377
WeekEnum.label(1); // Monday
377
-
WeekEnum.label('Monday'); // Monday, here is label, not key
378
+
WeekEnum.label('Monday'); // Monday, this is label, not key
378
379
```
379
380
380
381
---
@@ -386,7 +387,7 @@ WeekEnum.label('Monday'); // Monday, here is label, not key
386
387
Find the key of an enum item by its value. It's also known as [reverse mapping](https://www.typescriptlang.org/docs/handbook/enums.html#reverse-mappings). If not found, `undefined` is returned.
387
388
388
389
```js
389
-
WeekEnum.key(1); // Monday (here is key, not label)
390
+
WeekEnum.key(1); // Monday (this is key, not label)
390
391
```
391
392
392
393
---
@@ -430,15 +431,17 @@ Converts the enum items to an array of objects, each containing `value` and `lab
Extend the `Enum` objects with custom methods. More details can be found in the [Extensibility](#extensibility) section.
586
591
@@ -681,7 +686,7 @@ const App = () => {
681
686
682
687
### 💡 Internationalization for Enum Names and Labels
683
688
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.
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.
685
690
686
691
```js
687
692
WeekEnum.label(1); // Monday or 星期一, depending on the current locale
const weekValue:WeekValues=1; // ✅ Correct, 1 is a valid week enum value
704
709
const weeks:WeekValues[] = [0, 1]; // ✅ Correct, 0 and 1 are valid week enum values
705
710
706
-
constbadWeekValue:WeekValues="Weekend"; // ❌ Type error, "Weekend" is not a number
707
-
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
708
713
const badWeeks:WeekValues[] = [0, 8]; // ❌ Error, 8 is not a valid week enum value
709
714
```
710
715
@@ -811,7 +816,7 @@ const WeekEnum = Enum({
811
816
WeekEnum.Monday; // Hover over Monday
812
817
```
813
818
814
-

819
+

815
820
816
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.
817
822
@@ -901,7 +906,7 @@ We can see that both the enumeration value and the description of the enumeratio
901
906
902
907
### 💡 Custom Field Mapping in Array Format Initialization
903
908
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.
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.
905
910
906
911
```js
907
912
import { Enum } from'enum-plus';
@@ -1016,7 +1021,7 @@ This plugin also supports custom i18next options, and even allows complete contr
1016
1021
1017
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).
1018
1023
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.
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.
1020
1025
1021
1026
_my-extension.js_
1022
1027
@@ -1183,10 +1188,10 @@ WeekEnum[ITEMS].toList(); // But you can access it via the ITEMS alias
1183
1188
1184
1189
When using `enum-plus`, following these best practices can help ensure consistency, maintainability, and clarity in your codebase:
1185
1190
1186
-
1.**Enum Type Naming:** Use `PascalCase` and append with the `Enum` suffix (e.g., _WeekEnum_, _ColorEnum_).
1187
-
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.
1191
+
1.**Enum Type Naming:** Use `PascalCase` and end with `Enum` (e.g., _WeekEnum_, _ColorEnum_).
1192
+
2.**Enum Item Naming:** Use `PascalCase` (e.g., _Sunday_, _Red_). This naming approach highlights the static and immutable nature of enumeration members. Moreover, in the IDE's intelligent prompting, they will be displayed at the top instead of being mixed with other method names, making it more convenient for viewing and selection.
1188
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.
1189
-
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.
1190
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.
1191
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.
1192
1197
@@ -1249,7 +1254,7 @@ In Node.js environments, you can import enum-plus using either `require` or `imp
1249
1254
1250
1255
### Why do I need this library? TypeScript already has the built-in enums
1251
1256
1252
-
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:
1253
1258
1254
1259
1._Eliminate magic numbers_
1255
1260
2._Used in the `if` or `switch` statements for conditional branching_
@@ -1315,7 +1320,7 @@ const WeekEnum = Enum({
1315
1320
1316
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+.
1317
1322
1318
-
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.
0 commit comments