Skip to content

Commit 3ef307b

Browse files
committed
docs: update documentation
1 parent 9febfbb commit 3ef307b

File tree

4 files changed

+151
-102
lines changed

4 files changed

+151
-102
lines changed

.github/actions/replace-img-url/action.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ runs:
66
- run: |
77
sed -i "s|./public/enum-plus.svg|https://cdn.jsdelivr.net/gh/shijistar/enum-plus/public/enum-plus.svg|g" README.md
88
sed -i "s|./public/enum-plus.svg|https://cdn.jsdelivr.net/gh/shijistar/enum-plus/public/enum-plus.svg|g" README.zh-CN.md
9-
sed -i "s|./public/usage-screenshot.gif|https://cdn.jsdelivr.net/gh/shijistar/enum-plus/public/usage-screenshot.gif|g" README.md
10-
sed -i "s|./public/usage-screenshot.gif|https://cdn.jsdelivr.net/gh/shijistar/enum-plus/public/usage-screenshot.gif|g" README.zh-CN.md
9+
sed -i "s|./public/usage-screenshot-v3.gif|https://cdn.jsdelivr.net/gh/shijistar/enum-plus/public/usage-screenshot-v3.gif|g" README.md
10+
sed -i "s|./public/usage-screenshot-v3.gif|https://cdn.jsdelivr.net/gh/shijistar/enum-plus/public/usage-screenshot-v3.gif|g" README.zh-CN.md
1111
sed -i "s|./public/jsdoc-en.png|https://cdn.jsdelivr.net/gh/shijistar/enum-plus/public/jsdoc-en.png|g" README.md
1212
sed -i "s|./public/jsdoc-chs.png|https://cdn.jsdelivr.net/gh/shijistar/enum-plus/public/jsdoc-chs.png|g" README.zh-CN.md
1313
shell: bash

.github/actions/replace-path-url/action.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ runs:
88
using: 'composite'
99
steps:
1010
- run: |
11-
sed -i "s|https://cdn.jsdelivr.net/npm/enum-plus@.*/|https://cdn.jsdelivr.net/npm/enum-plus@${{ inputs.version }}/|g" README.md
12-
sed -i "s|https://cdn.jsdelivr.net/npm/enum-plus@.*/|https://cdn.jsdelivr.net/npm/enum-plus@${{ inputs.version }}/|g" README.zh-CN.md
13-
sed -i "s|https://github.com/shijistar/enum-plus/releases/download/v.*/|https://github.com/shijistar/enum-plus/releases/download/${{ inputs.version }}/|g" README.md
14-
sed -i "s|https://github.com/shijistar/enum-plus/releases/download/v.*/|https://github.com/shijistar/enum-plus/releases/download/${{ inputs.version }}/|g" README.zh-CN.md
15-
sed -i "s|https://github.com/shijistar/enum-plus/releases/tag/v.*/|https://github.com/shijistar/enum-plus/releases/tag/v${{ inputs.version }}/|g" README.md
11+
sed -i "s|https://cdn.jsdelivr.net/npm/enum-plus@[0-9]\+\.[0-9]\+\.[0-9]\+(-[A-Za-z0-9]\+\.[0-9]\+)?/|https://cdn.jsdelivr.net/npm/enum-plus@${{ inputs.version }}/|g" README.md
12+
sed -i "s|https://cdn.jsdelivr.net/npm/enum-plus@[0-9]\+\.[0-9]\+\.[0-9]\+(-[A-Za-z0-9]\+\.[0-9]\+)?/|https://cdn.jsdelivr.net/npm/enum-plus@${{ inputs.version }}/|g" README.zh-CN.md
13+
sed -i "s|https://github.com/shijistar/enum-plus/releases/download/v[0-9]\+\.[0-9]\+\.[0-9]\+(-[A-Za-z0-9]\+\.[0-9]\+)?/|https://github.com/shijistar/enum-plus/releases/download/${{ inputs.version }}/|g" README.md
14+
sed -i "s|https://github.com/shijistar/enum-plus/releases/download/v[0-9]\+\.[0-9]\+\.[0-9]\+(-[A-Za-z0-9]\+\.[0-9]\+)?/|https://github.com/shijistar/enum-plus/releases/download/${{ inputs.version }}/|g" README.zh-CN.md
15+
sed -i "s|https://github.com/shijistar/enum-plus/releases/tag/v[0-9]\+\.[0-9]\+\.[0-9]\+(-[A-Za-z0-9]\+\.[0-9]\+)?/|https://github.com/shijistar/enum-plus/releases/tag/v${{ inputs.version }}/|g" README.md
1616
shell: bash

README.md

Lines changed: 73 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
[![MiniProgram](https://img.shields.io/badge/MiniProgram-2185D0?logo=wechat)](https://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yY4YyAxkxWaCizvPhKLsxqbpkIVmYRWvqSfF8cw4FkM5oeiLmXhNl/efN3qLieM5fCMjkBGbSf5GBfGNHNhs/HGjopPgPxP86w8TLLu5GsqeugAAAABJRU5ErkJggg==)](https://docs.taro.zone/en/docs)
2929

30-
⬇️  [Introduction](#introduction) | [Features](#features) | [Installation](#installation) | [Enum Initialization](#enum-initialization) | [API](#api) | [Static Methods](#static-methods) | [User Stories](#user-stories) | [Plugin System](#plugin-system) | [Localization](#localization) | [Extensibility](#extensibility) | [Naming Conflicts](#naming-conflicts) | [Best Practices](#best-practices) | [Compatibility](#compatibility) | [Q&A](#qa) | [Contributing](#contributing)  ⬇️
30+
⬇️  [Introduction](#introduction) | [Features](#features) | [Installation](#installation) | [Enum Initialization](#enum-initialization) | [API](#api) | [Static Methods](#static-methods) | [User Stories](#user-stories) | [Plugin System](#plugin-system) | [Localization](#localization) | [Extensibility](#extensibility) | [Naming Conflicts](#naming-conflicts) | [Best Practices](#best-practices) | [Compatibility](#compatibility) | [Q&A](#qa) | [Support](#support)  ⬇️
3131

3232
> **🎉 v3.0 is Released!**
3333
>
@@ -102,7 +102,7 @@ Or using yarn:
102102
yarn add enum-plus
103103
```
104104

105-
**Zero‑Install Usage**:
105+
**Runs in the Browser**:
106106

107107
- The specific version:
108108

@@ -135,7 +135,7 @@ yarn add enum-plus
135135

136136
This section shows the various ways to initialize enums using the `Enum` function. Understanding these different initialization formats allows you to choose the most convenient approach for your specific use case.
137137

138-
### 1. Simple Key-Value Format
138+
### 1. Key-Value Format
139139

140140
The simplest format is a direct mapping of keys to values. This is similar to the native enum format.
141141

@@ -179,7 +179,7 @@ WeekEnum.label(0); // I love Sunday
179179

180180
### 3. Label-Only Format
181181

182-
This is useful when you want to use the key as the value.
182+
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.
183183

184184
```js
185185
import { Enum } from 'enum-plus';
@@ -190,12 +190,15 @@ const WeekEnum = Enum({
190190
});
191191

192192
WeekEnum.Sunday; // 'Sunday'
193-
WeekEnum.label('Sunday'); // I love Sunday
193+
WeekEnum.items[0].key; // 'Sunday'
194+
WeekEnum.items[0].label; // I love Sunday
194195
```
195196

196197
### 4. Array Format
197198

198-
The array format is useful when you need to create enums dynamically, such as from API data. This allows for flexibility in [Custom Field Mapping](#-custom-field-mapping-in-array-format-initialization) to adapt to different data structures.
199+
The array format is useful when you need to create enums with dynamic data, for example the data from an API.
200+
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.
199202

200203
```js
201204
import { Enum } from 'enum-plus';
@@ -236,7 +239,7 @@ WeekEnum.Saturday; // 6
236239

237240
## API
238241

239-
### 💎   Picks an enum value
242+
### 💎   Pick enum values
240243

241244
`Enum.XXX`
242245

@@ -334,7 +337,7 @@ ColorEnum.named.Red.raw.hex; // '#FF0000'
334337

335338
<sup>**_\[F]_**</sup> &nbsp; `has(keyOrValue?: string | number): boolean`
336339

337-
Determine whether a certain enum item (value or key) exists.
340+
Check if the value or key of an enum item exists.
338341

339342
```js
340343
WeekEnum.has(1); // true
@@ -366,7 +369,7 @@ WeekEnum.findBy('key', 'Monday'); // { key: 'Monday', value: 1, label: 'Monday'
366369

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

369-
Gets the display name of an enum item based on a certain value or key. If localization has been set up, the localized text will be returned.
372+
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.
370373

371374
```js
372375
WeekEnum.label(1); // Monday
@@ -379,7 +382,7 @@ WeekEnum.label('Monday'); // Monday, here is label, not key
379382

380383
<sup>**_\[F]_**</sup> &nbsp; `key(value?: string | number): string | undefined`
381384

382-
Get the key of an enum item based on the enum value, if the key is not found, return `undefined`.
385+
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.
383386

384387
```js
385388
WeekEnum.key(1); // Monday (here is key, not label)
@@ -393,11 +396,11 @@ WeekEnum.key(1); // Monday (here is key, not label)
393396
<br/>
394397
<sup>**_\[F^2]_**</sup> &nbsp; `raw(keyOrValue: V | K): T[K]`
395398

396-
The `raw` method is used to return the original initialization object of the enum collection, which is the object used to create the enum.
399+
The `raw` method has two overloads. The first one is to return the original initialization object of the whole enum collection, i.e. the first parameter of the `Enum` method.
397400

398-
The second overload method is used to return the original initialization object of a single enum item.
401+
The second one is to return the original initialization object of a single enum item, i.e. the sub-object of the corresponding field in the first parameter of the `Enum` method.
399402

400-
The main purpose of the `raw` method is to get the extended custom fields of the enum items. Unlimited number of custom fields are allowed.
403+
The main purpose of the `raw` method is to get the extended custom fields of the enum items.
401404

402405
```js
403406
const WeekEnum = Enum({
@@ -411,6 +414,8 @@ WeekEnum.raw('Monday'); // { value: 1, label: 'Monday', happy: false }
411414
WeekEnum.raw(); // { Sunday: { value: 0, label: 'Sunday', happy: true }, Monday: { value: 1, label: 'Monday', happy: false } }
412415
```
413416

417+
> Tips: If you want to access the metadata fields of a known enum item, using `enum.named.XXX.raw` is a good option, for example: `WeekEnum.named.Sunday.raw.happy`.
418+
414419
---
415420

416421
### 💎 &nbsp; toList
@@ -467,76 +472,79 @@ WeekEnum.toMap({ keySelector: 'key', valueSelector: 'value' });
467472

468473
`string`
469474

470-
Display name for Enum types. When creating an enum, you can assign it a display name using the optional `name` parameter. This name can be either a plain string or a localization key to support internationalized text. Please refer to the [Localization](#localization) section for more details.
471-
472-
> Usually Enums are used to generate dropdown menus in forms, or show item text in table cells. The display name of the enum type often serves as the form field label or table caption. By utilizing the `name` property, you can centralize the management of both the enum type's display name and its items' labels, simplifying maintenance and ensuring consistency across your application.
475+
The display name of the whole enum collection. It's optional and can be set as the second parameter of the `Enum` method. This name can be either a plain string or a localization key to support internationalized text. Please refer to the [Localization](#localization) section for more details.
473476

474477
```js
475478
const WeekEnum = Enum(
476479
{
477-
Sunday: { value: 0, label: 'Sunday', happy: true },
478-
Monday: { value: 1, label: 'Monday', happy: false },
480+
Sunday: { value: 0, label: 'Sunday' },
481+
Monday: { value: 1, label: 'Monday' },
479482
},
480483
{
481-
name: 'i18n.enums.week', // A localization key
484+
name: 'i18n.enums.week', // A regular string or localization key
482485
}
483486
);
484487

485-
WeekEnum.name; // Week
486-
WeekEnum.label(0); // Sunday
487-
WeekEnum.label(1); // Monday
488+
WeekEnum.name; // Week or 周, depending on the current language setting
488489
```
489490

491+
> Enums are usually used to generate dropdown menus in forms, or show item text in table cells. The display name of the enum type often serves as the form field label or table caption. By utilizing the `name` property, you can centralize the management of both the enum type's display name and its items' labels, simplifying maintenance and ensuring consistency across your application.
492+
490493
---
491494

492495
### ⚡️ &nbsp; valueType &nbsp; <sup>**_\[TypeScript Only]_**</sup>
493496

494497
`value1 | value2 | ...`
495498

496-
In TypeScript, provides a union type containing all enum values, enabling precise type constraints for variables and component properties. This replaces broad primitive types like `number` or `string` with exact value sets, preventing invalid assignments while enhancing both code readability and compile-time type safety.
499+
In TypeScript, provides a union type containing all enum values. It's usually used for type constraints on variables and method parameters, or used in component properties.
497500

498-
```typescript
499-
type WeekValues = typeof WeekEnum.valueType; // 0 | 1
501+
```ts
502+
const weekValue: typeof WeekEnum.valueType = 1;
503+
504+
function setToday(day: typeof WeekEnum.valueType) {
505+
// ...
506+
}
500507

501-
const weekValue: typeof WeekEnum.valueType = 1; // ✅ Type correct, 1 is a valid week enum value
502-
const weeks: (typeof WeekEnum.valueType)[] = [0, 1]; // ✅ Type correct, 0 and 1 are valid week enum values
503-
const badWeekValue: typeof WeekEnum.valueType = 8; // ❌ Type error, 8 is not a valid week enum value
504-
const badWeeks: (typeof WeekEnum.valueType)[] = [0, 8]; // ❌ Type error, 8 is not a valid week enum value
508+
const MyComponent = (props: { day: typeof WeekEnum.valueType }) => {
509+
// ...
510+
};
505511
```
506512

507-
> Note: This is a TypeScript type only and cannot be called at runtime. Calling it at runtime will throw an exception.
513+
> Note: This is a TypeScript type and cannot be called at runtime. Calling it at runtime will throw an error.
508514
509515
---
510516

511517
### ⚡️ &nbsp; keyType &nbsp; <sup>**_\[TypeScript Only]_**</sup>
512518

513519
`key1 | key2 | ...`
514520

515-
Similar to `valueType`, provides a union type of all enum `key`(s)
521+
Similar to `valueType`, provides a union type of all enum keys in TypeScript.
516522

517-
```typescript
523+
```ts
518524
type WeekKeys = typeof WeekEnum.keyType; // 'Sunday' | 'Monday'
519525

520526
const weekKey: typeof WeekEnum.keyType = 'Monday';
521527
const weekKeys: (typeof WeekEnum.keyType)[] = ['Sunday', 'Monday'];
522528
```
523529

524-
> Note: This is a TypeScript type only and cannot be called at runtime. Calling it at runtime will throw an exception.
530+
> Note: This is a TypeScript type and cannot be called at runtime. Calling it at runtime will throw an error.
525531
526532
---
527533

528534
### ⚡️ &nbsp; rawType &nbsp; <sup>**_\[TypeScript Only]_**</sup>
529535

530536
`{ value: V, label: string, [...] }`
531537

532-
Provides a type of the original initialization object of the Enum collection.
538+
Provides a type of the original initialization object of the whole enum collection, i.e. the type of the first parameter of the `Enum` method.
539+
540+
Do not confuse it with the `raw` method. The `raw` method is a runtime method, while `rawType` is a TypeScript type.
533541

534-
```typescript
542+
```ts
535543
type WeekRaw = typeof WeekEnum.rawType;
536544
// { Sunday: { value: 0, label: string }, Monday: { value: 1, label: string } }
537545
```
538546

539-
> Note: This is a TypeScript type only and cannot be called at runtime. Calling it at runtime will throw an exception.
547+
> Note: This is a TypeScript type and cannot be called at runtime. Calling it at runtime will throw an error.
540548
541549
---
542550

@@ -684,29 +692,46 @@ WeekEnum.name; // Week or 周, depending on the current locale
684692

685693
#### 💡 Type Narrowing &nbsp; <sup>**_\[TypeScript Only]_**</sup>
686694

695+
- Variables
696+
687697
```ts
688-
type MyComponentProps = {
689-
day: typeof WeekEnum.valueType; // 0 | 1 | ... | 5 | 6
690-
};
691-
const MyComponent = (props: MyComponentProps) => {
692-
return <div>Today is {WeekEnum.label(props.day)}</div>;
693-
};
698+
type WeekValues = typeof WeekEnum.valueType; // 0 | 1 | ... | 5 | 6
694699

695-
<MyComponent day={1} />; // ✅ Type correct
696-
<MyComponent day="Monday" />; // ❌ Type error
697-
<MyComponent day={8} />; // ❌ Error, 8 is not a valid enum value
700+
const weekValue: WeekValues = 1; // ✅ Correct, 1 is a valid week enum value
701+
const weeks: WeekValues[] = [0, 1]; // ✅ Correct, 0 and 1 are valid week enum values
702+
703+
const badWeekValue: WeekValues = "Weekend"; // ❌ Type error, "Weekend" is not a number
704+
const badWeekValue: WeekValues = 8; // ❌ Error, 8 is not a valid week enum value
705+
const badWeeks: WeekValues[] = [0, 8]; // ❌ Error, 8 is not a valid week enum value
698706
```
699707

708+
- Function Parameters
709+
700710
```ts
701711
function setDay(day: typeof WeekEnum.valueType) {
702712
// The type of day is narrowed to 0 | 1 | ... | 5 | 6
703713
}
704714

705-
setDay(1); //Type correct
715+
setDay(1); //Correct
706716
setDay('Monday'); // ❌ Type error
707717
setDay(8); // ❌ Error, 8 is not a valid enum value
708718
```
709719

720+
- Component Props
721+
722+
```ts
723+
type MyComponentProps = {
724+
day: typeof WeekEnum.valueType; // 0 | 1 | ... | 5 | 6
725+
};
726+
const MyComponent = (props: MyComponentProps) => {
727+
return <div>Today is {WeekEnum.label(props.day)}</div>;
728+
};
729+
730+
<MyComponent day={1} />; // ✅ Correct
731+
<MyComponent day="Monday" />; // ❌ Type error, "Monday" is not a number
732+
<MyComponent day={8} />; // ❌ Error, 8 is not a valid enum value
733+
```
734+
710735
---
711736

712737
#### 💡 Support Metadata Fields That Can Serve as a Static Configuration System
@@ -1320,6 +1345,6 @@ Thanks to the high flexibility of [Plugin System](#plugin-system), it is quite e
13201345

13211346
If you find a security issue, please follow the [Security Policy](SECURITY.md) to report it responsibly.
13221347

1323-
## Support & Star
1348+
## Support
13241349

1325-
**If you find this project useful, please consider giving it a [STAR](https://github.com/shijistar/enum-plus) ⭐️ on GitHub. It helps more people discover the project and encourages us to continue development.**
1350+
If you find this project useful, please consider giving it a [STAR](https://github.com/shijistar/enum-plus) ⭐️ on GitHub. It helps more people discover the project and encourages us to continue development.

0 commit comments

Comments
 (0)