Skip to content

Commit 97181ce

Browse files
committed
docs: add naming convention
1 parent 0f27e43 commit 97181ce

File tree

2 files changed

+58
-34
lines changed

2 files changed

+58
-34
lines changed

README.md

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,19 @@
1313
</p>
1414
<br/>
1515

16-
[![npm version](https://img.shields.io/npm/v/enum-plus.svg?color=red&cacheSeconds=86400)](https://www.npmjs.com/package/enum-plus)
17-
[![npm bundle size](https://img.shields.io/bundlejs/size/enum-plus?cacheSeconds=86400&label=minzipped%20size)](https://bundlephobia.com/result?p=enum-plus)
18-
[![npm downloads](https://img.shields.io/npm/dm/enum-plus.svg?cacheSeconds=86400)](https://www.npmjs.com/package/enum-plus)
16+
[![npm latest version](https://img.shields.io/npm/v/enum-plus.svg?cacheSeconds=86400)](https://www.npmjs.com/package/enum-plus)
17+
[![npm package minimized gzipped size](https://img.shields.io/bundlejs/size/enum-plus?label=minzipped%20size&color=44cc11&cacheSeconds=86400)](https://www.npmjs.com/package/enum-plus?activeTab=code)
18+
[![code coverage](https://codecov.io/gh/shijistar/enum-plus/graph/badge.svg?token=JMCDJKLT0B)](https://codecov.io/gh/shijistar/enum-plus)
19+
[![npm downloads](https://img.shields.io/npm/dm/enum-plus.svg?color=007ec6&cacheSeconds=86400)](https://www.npmjs.com/package/enum-plus)
1920
![GitHub License](https://img.shields.io/github/license/shijistar/enum-plus?label=License&color=%23F68F1E&cacheSeconds=86400)
2021

21-
⬇️ &nbsp;&nbsp; [Introduction](#introduction) | [Features](#features) | [Installation](#installation) | [Enum Initialization](#enum-initialization) | [API](#api) | [Usage](#usage) | [Localization](#localization) | [Extensibility](#extensibility) | [Q&A](#qa) &nbsp;&nbsp; ⬇️
22+
⬇️ &nbsp;[Introduction](#introduction) | [Features](#features) | [Installation](#installation) | [Enum Initialization](#enum-initialization) | [API](#api) | [Usage](#usage) | [Naming Conventions](#naming-convention-best-practices) | [Localization](#localization) | [Extensibility](#extensibility) | [Q&A](#qa)&nbsp; ⬇️
2223

2324
## Introduction
2425

25-
`enum-plus` is an enhanced enum library that is fully compatible with the native `enum` and extends it with powerful features such as display text, localization, UI control binding, enum items traversal, and more useful extension methods. This lightweight, zero-dependency, TypeScript library works with any front-end framework.
26+
`enum-plus` is an enhanced enum library that is fully compatible with the native `enum` and extends it with powerful features such as display text, localization, UI control binding, enum members traversal, and more useful extension methods. This lightweight, zero-dependency, TypeScript library works with any front-end framework.
2627

27-
With the extended display text capability, enum items can be used to generate dropdowns, checkboxes, and other UI controls with a single line of code. By using the extension methods of the enum, you can easily traverse the array of enum items, get the display text of a certain enum value, determine whether a value exists, etc. The display text of the enum item supports localization, which can return the corresponding text according to the current language environment, making the display text of the enum item more flexible and more in line with user needs.
28+
With the extended display text capability, enum members can be used to generate dropdowns, checkboxes, and other UI controls with a single line of code. By using the extension methods of the enum, you can easily traverse the array of enum members, get the display text of a certain enum value, determine whether a value exists, etc. The display text of the enum member supports localization, which can return the corresponding text according to the current language environment, making the display text of the enum member more flexible and more in line with user needs.
2829

2930
What other exciting features are there? Please continue to explore! Or you can check out this usage video first.
3031

@@ -36,10 +37,10 @@ What other exciting features are there? Please continue to explore! Or you can c
3637

3738
- Full compatibility with native `enum` behavior
3839
- Support for multiple data types including `number` and `string`
39-
- Enhanced enum items with customizable display text
40+
- Enhanced enum members with customizable display text
4041
- Built-in localization capabilities that integrate with any i18n library
4142
- Streamlined conversion from enum values to human-readable display text
42-
- Extensible design allowing unlimited custom fields on enum items
43+
- Extensible design allowing unlimited custom fields on enum members
4344
- Seamless integration with any UI libraries like [Ant Design](https://ant.design/components/overview), [ElementPlus](https://element-plus.org/en-US/component/overview.html), [Material-UI](https://mui.com/material-ui), in a single line of code
4445
- Complete Node.js compatibility with SSR support
4546
- Zero dependencies - pure JavaScript implementation usable in any front-end framework
@@ -110,7 +111,7 @@ Week.Monday; // 'Mon'
110111

111112
### 3. Standard Format (Recommended)
112113

113-
The standard format includes both a `value` and a `label` for each enum item. This is the most commonly used format and is recommended for most cases. This format allows you to specify a display text for each enum item, which can be used in UI components.
114+
The standard format includes both a `value` and a `label` for each enum member. This is the most commonly used format and is recommended for most cases. This format allows you to specify a display text for each enum member, which can be used in UI components.
114115

115116
```js
116117
import { Enum } from 'enum-plus';
@@ -197,23 +198,23 @@ Week.Monday; // 1
197198
198199
`{ value, label, key, raw }[]`
199200
200-
Returns a read-only array of all enum items. The array structure conforms to [Ant Design](https://ant.design/components/select#usage-upgrade) component specifications, which makes it possible to generate dropdown menus, checkboxes, and other UI controls with just one line of code. For more details, please refer to the usage examples below.
201+
Returns a read-only array of all enum members. The array structure conforms to [Ant Design](https://ant.design/components/select#usage-upgrade) component specifications, which makes it possible to generate dropdown menus, checkboxes, and other UI controls with just one line of code. For more details, please refer to the usage examples below.
201202
202203
---
203204
204205
### 💎 &nbsp; keys
205206
206207
`string[]`
207208
208-
Returns a read-only array of all enum item `key`(s)
209+
Returns a read-only array of all enum member `key`(s)
209210
210211
---
211212
212213
### 💎 &nbsp; label
213214
214215
<sup>**_\[Function]_**</sup> &nbsp; `label(keyOrValue?: string | number): string | undefined`
215216
216-
Gets the display text of an enum item based on a certain value or key. If localization has been set up, the localized text will be returned.
217+
Gets the display text of an enum member based on a certain value or key. If localization has been set up, the localized text will be returned.
217218
218219
```js
219220
Week.label(1); // Monday
@@ -227,7 +228,7 @@ Week.label('Monday'); // 星期日, or show localized text if localization is se
227228
228229
<sup>**_\[Function]_**</sup> &nbsp; `key(value?: string | number): string | undefined`
229230
230-
Get the key of an enum item based on the enum value, if the key is not found, return `undefined`.
231+
Get the key of an enum member based on the enum value, if the key is not found, return `undefined`.
231232
232233
```js
233234
Week.key(1); // Monday (here is key, not label)
@@ -239,7 +240,7 @@ Week.key(1); // Monday (here is key, not label)
239240
240241
<sup>**_\[Function]_**</sup> &nbsp; `has(keyOrValue?: string | number): boolean`
241242
242-
Determine whether a certain enum item (value or key) exists.
243+
Determine whether a certain enum member (value or key) exists.
243244
244245
```js
245246
Week.has(1); // true
@@ -254,15 +255,15 @@ Week.has('Birthday'); // false
254255
255256
<sup>**_\[Function]_**</sup> &nbsp; `toSelect(config?: OptionsConfig): {value, label}[]`
256257
257-
`toSelect` is similar to `items`, both return an array of all enum items. The difference is that the elements returned by `toSelect` only contain the `label` and `value` fields, no other extra fields. At the same time, the `toSelect` method allows inserting a default element at the beginning of the array, which is generally used for the default option (which means _all_, _none_, or _unlimited_, etc) of select control. Of course, you can customize this default option.
258+
`toSelect` is similar to `items`, both return an array of all enum members. The difference is that the elements returned by `toSelect` only contain the `label` and `value` fields, no other extra fields. At the same time, the `toSelect` method allows inserting a default element at the beginning of the array, which is generally used for the default option (which means _all_, _none_, or _unlimited_, etc) of select control. Of course, you can customize this default option.
258259
259260
---
260261
261262
### 💎 &nbsp; toMenu
262263
263264
<sup>**_\[Function]_**</sup> &nbsp; `toMenu(): { key, label }[]`
264265
265-
Returns an array of all enum items that conforms to [Ant Design](https://ant.design/components/menu) specifications. It's used to generate the `Menu`, `Dropdown` controls, in a single line of code.
266+
Returns an array of all enum members that conforms to [Ant Design](https://ant.design/components/menu) specifications. It's used to generate the `Menu`, `Dropdown` controls, in a single line of code.
266267

267268
```js
268269
import { Menu } from 'antd';
@@ -285,7 +286,7 @@ The data format is:
285286

286287
<sup>**_\[Function]_**</sup> &nbsp; `toFilter(): { text, value }[]`
287288

288-
Returns an array of enum items that can pass directly to the [Ant Design](https://ant.design/components/table#table-demo-head) Table component as `filters` property of a column. This is used to add a dropdown filter box in the table header to filter table data.
289+
Returns an array of enum members that can pass directly to the [Ant Design](https://ant.design/components/table#table-demo-head) Table component as `filters` property of a column. This is used to add a dropdown filter box in the table header to filter table data.
289290

290291
The data format is:
291292

@@ -323,9 +324,9 @@ The data format is:
323324

324325
The `raw` method is used to return the original initialization object of the enum collection, which is the object used to create the enum.
325326

326-
The second overload method is used to return the original initialization object of a single enum item.
327+
The second overload method is used to return the original initialization object of a single enum member.
327328

328-
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.
329+
The main purpose of the `raw` method is to get the extended custom fields of the enum members. Unlimited number of custom fields are allowed.
329330

330331
```js
331332
const Week = Enum({
@@ -403,7 +404,7 @@ Week.Monday; // 1
403404

404405
---
405406

406-
#### Supports JSDoc comments on enum items
407+
#### Supports JSDoc comments on enum members
407408

408409
Supports inline documentation through JSDoc, allowing developers to view detailed comments by simply hovering over enum values in the editor.
409410

@@ -424,7 +425,7 @@ You can see that this hover functionality reveals both documentation and enum va
424425

425426
---
426427

427-
#### Gets a read-only enum items array
428+
#### Gets a read-only enum members array
428429

429430
```js
430431
Week.items; // The output is:
@@ -464,7 +465,7 @@ Week.items.some((item) => item.value === 1); // true
464465

465466
---
466467

467-
#### Supports traversing enum items array
468+
#### Supports traversing enum members array
468469

469470
```js
470471
Week.items.length; // 2
@@ -716,9 +717,9 @@ const PetTypes = Enum(petTypes, {
716717

717718
#### Handling Name Conflicts?
718719

719-
When working with enums, a common edge case occurs when an enum item's key conflicts with built-in method names. While we typically access enum values through `Week.XXX` notation, complications arise when these keys overlap with enum methods.
720+
When working with enums, a common edge case occurs when an enum member's key conflicts with built-in method names. While we typically access enum values through `Week.XXX` notation, complications arise when these keys overlap with enum methods.
720721

721-
The enum library provides several utility methods like `label`, `key`, and `toSelect`. When an enum item shares a name with these methods, the enum item takes precedence, effectively overriding the utility method. However, this doesn't mean you lose access to those methods - they remain available through the `items` collection, ensuring all functionality is preserved regardless of naming conflicts. Please refer to the code example below:
722+
The enum library provides several utility methods like `label`, `key`, and `toSelect`. When an enum member shares a name with these methods, the enum member takes precedence, effectively overriding the utility method. However, this doesn't mean you lose access to those methods - they remain available through the `items` collection, ensuring all functionality is preserved regardless of naming conflicts. Please refer to the code example below:
722723

723724
```js
724725
const Week = Enum({
@@ -728,14 +729,14 @@ const Week = Enum({
728729
label: { value: 4 }, // Naming conflict
729730
} as const);
730731

731-
Week.keys; // 3, enum item has higher priority and will override the method
732-
Week.label; // 4, enum item has higher priority and will override the method
732+
Week.keys; // 3, enum member has higher priority and will override the method
733+
Week.label; // 4, enum member has higher priority and will override the method
733734
// You can still access these methods through Enum.items 🙂
734735
Week.items.keys; // ['foo', 'bar', 'keys', 'label']
735736
Week.items.label(1); // 'foo'
736737
```
737738
738-
For an even more extreme edge case where the items property itself conflicts with an enum item name, a solution is still available. In such scenarios, you can access the items array through a Symbol-based alias field that guarantees access regardless of naming conflicts. Consider the following example:
739+
For an even more extreme edge case where the items property itself conflicts with an enum member name, a solution is still available. In such scenarios, you can access the items array through a Symbol-based alias field that guarantees access regardless of naming conflicts. Consider the following example:
739740
740741
```js
741742
import { ITEMS } from 'enum-plus';
@@ -746,7 +747,7 @@ const Week = Enum({
746747
items: { value: 3 }, // Naming conflict
747748
} as const);
748749

749-
Week.items; // 3, enum item has higher priority and will override items
750+
Week.items; // 3, enum member has higher priority and will override items
750751
Week[ITEMS]; // ITEMS is an alias Symbol
751752
// [
752753
// { value: 1, key: 'foo', label: 'foo' },
@@ -758,6 +759,17 @@ Week[ITEMS]; // ITEMS is an alias Symbol
758759
759760
---
760761
762+
## Naming Convention Best Practices
763+
764+
1. **Enum Type Naming:** Use `PascalCase` and append with the `Enum` suffix (e.g., _WeekEnum_, _ColorEnum_).
765+
2. **Enum Member Naming:** Use `PascalCase` for enum members(e.g., _WeekEnum.Sunday_, _ColorEnum.Red_). This naming style highlights the immutability and static nature of enum members, and ensures they appear at the top in IDE IntelliSense suggestions for easier selection.
766+
3. **Semantic Clarity:** Ensure enum and member names have clear semantics. Good semantic naming serves as self-documentation, making code intent explicit and reducing cognitive overhead.
767+
4. **Single Responsibility Principle:** Each enum type should represent a single, cohesive set of related constants. Avoiding overlapping responsibilities between different enum types.
768+
5. **Provide JSDoc Comments:** Provide JSDoc comments for each enum member and the enum type itself, explaining their purpose and usage. Comprehensive documentation enables IDE hover tooltips and improves code readability and maintainability.
769+
6. **Internationalization Architecture:** Plan for internationalization from the outset by leveraging the library’s built-in [localization](#localization) features. A well-designed internationalization architecture minimizes future refactoring and facilitates global scalability.
770+
771+
---
772+
761773
## Localization
762774
763775
While `enum-plus` doesn't include built-in internationalization capabilities, it offers flexible localization through the optional `localize` parameter. This allows you to implement a custom localization function that transforms enum `label` values into appropriate translated text based on the current language context. The language state management remains your responsibility, with your `localize` method determining which localized text to return. For production applications, we strongly recommend leveraging established internationalization libraries such as `i18next` rather than creating custom solutions.
@@ -870,8 +882,8 @@ Please note that you are not required to import types such as `EnumItemInit` and
870882
`EnumExtension` is a generic interface that accepts three type parameters, which are:
871883
872884
- `T`: Initialization object of the enum type (e.g., the object passed to `Enum()`)
873-
- `K`: Key of the enum item (e.g., Sunday, Monday)
874-
- `V`: Value of the enum items
885+
- `K`: Key of the enum member (e.g., Sunday, Monday)
886+
- `V`: Value of the enum members
875887
876888
If you want to provide more friendly type hints in the extension methods, you may need to use these type parameters. However these are all optional, if you don't need them, you can omit them.
877889

0 commit comments

Comments
 (0)