Skip to content

Commit 9902c54

Browse files
committed
docs(core): update README
1 parent 291cdf5 commit 9902c54

File tree

2 files changed

+65
-58
lines changed

2 files changed

+65
-58
lines changed

README.md

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ What other exciting features are there? Please continue to explore! Or you can c
6969
- Plugin system design, extending enum functionality through plugin installations
7070
- Supports type narrowing to enhance type safety<sup>_&nbsp;&nbsp;TypeScript_</sup>
7171
- 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)
7372
- Compatible with various environments including Browsers, Node.js, React Native, Taro, and mini-programs
73+
- Supports server-side rendering (SSR)
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 (only 2KB+ minzipped)
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'
@@ -355,26 +355,27 @@ WeekEnum.has('Birthday'); // false
355355

356356
Find an enum item by a specific field and its value. Returns the enum item object if found, otherwise returns `undefined`.
357357

358-
The `field` parameter can be one of the built-in fields: `key`, `value`, `label`, or any custom field defined in the enum.
358+
The `field` parameter can be one of the built-in fields: `key`, `value`, `label`, or any meta field defined in the enum item.
359359

360360
```js
361-
WeekEnum.findBy('value', 1); // { key: 'Monday', value: 1, label: 'Monday' }
362-
WeekEnum.findBy('key', 'Monday'); // { key: 'Monday', value: 1, label: 'Monday' }
361+
ColorEnum.findBy('value', 1); // { 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' }
363364
```
364365

365-
> 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`.
366367
367368
---
368369

369370
### 💎 &nbsp; label
370371

371372
<sup>**_\[F]_**</sup> &nbsp; `label(keyOrValue?: string | number): string | undefined`
372373

373-
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.
374375

375376
```js
376377
WeekEnum.label(1); // Monday
377-
WeekEnum.label('Monday'); // Monday, here is label, not key
378+
WeekEnum.label('Monday'); // Monday, this is label, not key
378379
```
379380

380381
---
@@ -386,7 +387,7 @@ WeekEnum.label('Monday'); // Monday, here is label, not key
386387
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.
387388

388389
```js
389-
WeekEnum.key(1); // Monday (here is key, not label)
390+
WeekEnum.key(1); // Monday (this is key, not label)
390391
```
391392

392393
---
@@ -430,15 +431,17 @@ Converts the enum items to an array of objects, each containing `value` and `lab
430431
```js
431432
WeekEnum.toList();
432433
// [
434+
// { value: 0, label: 'Sunday' },
433435
// { value: 1, label: 'Monday' },
434-
// { value: 2, label: 'Tuesday' },
435436
// ...
437+
// { value: 6, label: 'Saturday' }
436438
// ]
437439
WeekEnum.toList({ valueField: 'id', labelField: 'name' });
438440
// [
441+
// { id: 0, name: 'Sunday' },
439442
// { id: 1, name: 'Monday' },
440-
// { id: 2, name: 'Tuesday' },
441443
// ...
444+
// { id: 6, name: 'Saturday' }
442445
// ]
443446
```
444447

@@ -455,15 +458,17 @@ Converts the enum items to a key-value map object, where the keys are the enum v
455458
```js
456459
WeekEnum.toMap();
457460
// {
461+
// "0": 'Sunday',
458462
// "1": 'Monday',
459-
// "2": 'Tuesday',
460463
// ...
464+
// "6": 'Saturday'
461465
// }
462466
WeekEnum.toMap({ keySelector: 'key', valueSelector: 'value' });
463467
// {
468+
// "Sunday": 0,
464469
// "Monday": 1,
465-
// "Tuesday": 2,
466470
// ...
471+
// "Saturday": 6
467472
// }
468473
```
469474

@@ -580,7 +585,7 @@ Enum.localize = (key) => i18n.t(key);
580585

581586
### 💎 &nbsp; Enum.extends
582587

583-
<sup>**_\[F]_**</sup> &nbsp; `(obj: Record<string, unknown> | undefined) => void`
588+
<sup>**_\[F]_**</sup> &nbsp; `(obj: Record<string, Function>) => void`
584589

585590
Extend the `Enum` objects with custom methods. More details can be found in the [Extensibility](#extensibility) section.
586591

@@ -681,7 +686,7 @@ const App = () => {
681686

682687
### 💡 Internationalization for Enum Names and Labels
683688

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.
685690

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

706-
const badWeekValue: WeekValues = "Weekend"; // ❌ Type error, "Weekend" is not a number
707-
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
708713
const badWeeks: WeekValues[] = [0, 8]; // ❌ Error, 8 is not a valid week enum value
709714
```
710715

@@ -811,7 +816,7 @@ const WeekEnum = Enum({
811816
WeekEnum.Monday; // Hover over Monday
812817
```
813818

814-
![jsdoc](./public/jsdoc-en.png)
819+
![JSDoc](./public/jsdoc-en.png)
815820

816821
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.
817822

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

902907
### 💡 Custom Field Mapping in Array Format Initialization
903908

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.
905910

906911
```js
907912
import { Enum } from 'enum-plus';
@@ -1016,7 +1021,7 @@ This plugin also supports custom i18next options, and even allows complete contr
10161021

10171022
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).
10181023

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.
10201025

10211026
_my-extension.js_
10221027

@@ -1183,10 +1188,10 @@ WeekEnum[ITEMS].toList(); // But you can access it via the ITEMS alias
11831188

11841189
When using `enum-plus`, following these best practices can help ensure consistency, maintainability, and clarity in your codebase:
11851190

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.
11881193
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.
11901195
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.
11911196
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.
11921197

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

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

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:
12531258

12541259
1. _Eliminate magic numbers_
12551260
2. _Used in the `if` or `switch` statements for conditional branching_
@@ -1315,7 +1320,7 @@ const WeekEnum = Enum({
13151320

13161321
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+.
13171322

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.
13191324

13201325
```js
13211326
/** @type {{ Sunday: 0; Monday: 1 }} */

0 commit comments

Comments
 (0)