Skip to content

Commit 8142b2a

Browse files
committed
feat: rename IS_ENUM_COLLECTION symbol
1 parent 9fd0b79 commit 8142b2a

File tree

7 files changed

+32
-25
lines changed

7 files changed

+32
-25
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const MyEnum = Enum({
5353
- `enums.filters`
5454
- `enums.valuesEnum`
5555
- Please use the new methods `Enum.items`, `Enum.toSelect`, `Enum.toMenu`, `Enum.toFilter`, and `Enum.toValueMap` instead, which are introduced since `v2.1.0`.
56-
- `ENUM_COLLECTION` symbol is renamed to `IS_ENUM_COLLECTION`.
56+
- `ENUM_COLLECTION` symbol is renamed to `IS_ENUM`.
5757
- `ENUM_ITEM` symbol is renamed to `IS_ENUM_ITEM`.
5858
- `ENUM_ITEMS` symbol is renamed to `IS_ENUM_ITEMS`.
5959
- `EnumValuesArray` interface is renamed to `EnumItemsArray`.

migrate-from-v2.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44

55
The `enum.values` property was originally planned for removal, but after some discussions, we decided to restore it and change its function: it now represents an array of the member raw values. Therefore, if you are still using the old `values`, please change to the `enum.items` instead.
66

7+
## 🛠 Symbols changed
8+
9+
The following symbols have been renamed to better reflect their purpose:
10+
11+
- `ENUM_COLLECTION` is now `IS_ENUM`
12+
- `ENUM_ITEM` is now `IS_ENUM_ITEM`
13+
- `ENUM_ITEMS` is now `IS_ENUM_ITEMS`
14+
715
## 🛠 AntDesign oriented methods have been moved
816

917
The following methods are removed from the core library and moved to the `enum-plus-plugin-antd` package. Please install the package and use them from there.

src/enum-collection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import type {
1818
ValueTypeFromSingleInit,
1919
} from './types';
2020
import type { IS_ENUM_ITEMS } from './utils';
21-
import { IS_ENUM_COLLECTION, ITEMS, KEYS, LABELS, VALUES } from './utils';
21+
import { IS_ENUM, ITEMS, KEYS, LABELS, VALUES } from './utils';
2222

2323
/**
2424
* - **EN:** Enum collection extension base class, used to extend the Enums
@@ -51,7 +51,7 @@ export class EnumCollectionClass<
5151
* - **EN:** A boolean value indicates that this is an enum collection instance.
5252
* - **CN:** 布尔值,表示这是一个枚举集合实例
5353
*/
54-
readonly [IS_ENUM_COLLECTION] = true;
54+
readonly [IS_ENUM] = true;
5555
[Symbol.hasInstance](this: EnumCollectionClass<T, K, V>, instance: unknown): boolean {
5656
// intentionally use == to support both number and string format value
5757
return this.items.some(

src/enum.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type {
1212
LocalizeInterface,
1313
ValueTypeFromSingleInit,
1414
} from './types';
15-
import type { IS_ENUM_COLLECTION, ITEMS, KEYS, LABELS, VALUES } from './utils';
15+
import type { IS_ENUM, ITEMS, KEYS, LABELS, VALUES } from './utils';
1616

1717
export interface EnumInterface {
1818
/**
@@ -88,6 +88,8 @@ export interface EnumInterface {
8888
* > 根据不同的环境或技术框架,返回值可能是字符串以外类型,例如可能是一个组件以支持动态语言切换,而枚举项的`label`也会随之变化。
8989
*/
9090
localize: LocalizeInterface | undefined;
91+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
92+
isEnum<T>(value: T): value is IEnum<any, any, any>;
9193
/**
9294
* - **EN:** Add global extension methods to the enum, and all enum instances will have these new
9395
* extension methods
@@ -122,7 +124,7 @@ export type IEnum<
122124
* - **EN:** A boolean value indicates that this is an enum collection instance.
123125
* - **CN:** 布尔值,表示这是一个枚举集合实例
124126
*/
125-
[IS_ENUM_COLLECTION]: true;
127+
[IS_ENUM]: true;
126128
} & {
127129
// Add enum item values, just like native enums
128130
[key in K]: ValueTypeFromSingleInit<T[key], key, T[K] extends number | undefined ? number : key>;

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ export type { ToListConfig, IEnumItems } from './enum-items';
1515
export type { EnumItemClass, EnumItemOptions } from './enum-item';
1616
export type { IEnum, EnumInterface, EnumInitOptions } from './enum';
1717

18-
export { KEYS, ITEMS, VALUES, LABELS, IS_ENUM_ITEM, IS_ENUM_ITEMS, IS_ENUM_COLLECTION, defaultLocalize } from './utils';
18+
export { KEYS, ITEMS, VALUES, LABELS, IS_ENUM_ITEM, IS_ENUM_ITEMS, IS_ENUM, defaultLocalize } from './utils';
1919
export type { PluginFunc } from './enum';
2020
export { Enum } from './enum';

src/utils.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,20 @@ export const LABELS = Symbol.for('[labels]');
3636
export const RAW = Symbol.for('[raw]');
3737

3838
/**
39-
* - **EN:** Check whether the object is an instance of `EnumItem`. If the object has this field and
40-
* its value is `true`, it is an `EnumItem` instance.
41-
* - **CN:** 判断对象是否 `EnumItem` 实例,如果对象存在此字段且值为`true`,则是 `EnumItem` 实例
39+
* - **CN:** 判断对象是否 `EnumItem` 实例
40+
* - **EN:** Check whether the object is an `EnumItem` instance
4241
*/
4342
export const IS_ENUM_ITEM = Symbol.for('[IsEnumItem]');
4443

4544
/**
46-
* - **EN:** Check whether the object is an instance of `EnumCollection`. If the object has this field
47-
* and its value is `true`, it is an `EnumCollection` instance.
48-
* - **CN:** 判断对象是否 `EnumCollection` 实例,如果对象存在此字段且值为`true`,则是 `EnumCollection` 实例
45+
* - **EN:** Check whether the object is an enum type
46+
* - **CN:** 判断对象是否枚举类型
4947
*/
50-
export const IS_ENUM_COLLECTION = Symbol.for('[IsEnumCollection]');
48+
export const IS_ENUM = Symbol.for('[IsEnum]');
5149

5250
/**
53-
* - **EN:** Check whether the object is an instance of `EnumItems`. If the object has this field and
54-
* its value is `true`, it is an `EnumItems` instance.
55-
* - **CN:** 判断对象是否 `EnumItems` 实例,如果对象存在此字段且值为`true`,则是 `EnumItems` 实例
51+
* - **EN:** Check whether the object is an `EnumItems` collection array
52+
* - **CN:** 判断对象是否 `EnumItems` 集合数组
5653
*/
5754
export const IS_ENUM_ITEMS = Symbol.for('[IsEnumItems]');
5855

test/test-suites/enum-collection.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defaultLocalize, IS_ENUM_COLLECTION as IS_ENUM_COLLECTION_IN_NODE } from '@enum-plus';
1+
import { defaultLocalize, IS_ENUM as IS_ENUM_IN_NODE } from '@enum-plus';
22
import type TestEngineBase from '../engines/base';
33
import { toPlainEnums } from '../utils/index';
44
import { addEnumValuesTestSuite } from './enum-items';
@@ -159,16 +159,16 @@ const testEnumCollection = (engine: TestEngineBase) => {
159159

160160
engine.test(
161161
'should have [ENUM_COLLECTION] property to indicate that this is an enum collection',
162-
({ EnumPlus: { Enum, IS_ENUM_COLLECTION }, WeekConfig: { StandardWeekConfig } }) => {
162+
({ EnumPlus: { Enum, IS_ENUM }, WeekConfig: { StandardWeekConfig } }) => {
163163
const week = Enum(StandardWeekConfig);
164-
return { week, IS_ENUM_COLLECTION };
164+
return { week, IS_ENUM };
165165
},
166-
({ week, IS_ENUM_COLLECTION }) => {
167-
// @ts-expect-error: because IS_ENUM_COLLECTION is hidden by the interface, but it actually exists
168-
engine.expect(week[IS_ENUM_COLLECTION]).toBe(true);
169-
engine.expect(week[IS_ENUM_COLLECTION_IN_NODE]).toBe(true);
170-
// @ts-expect-error: because IS_ENUM_COLLECTION and Symbol.for('[IsEnumCollection]') are equal
171-
engine.expect(week[Symbol.for('[IsEnumCollection]')]).toBe(true);
166+
({ week, IS_ENUM }) => {
167+
// @ts-expect-error: because IS_ENUM is hidden by the interface, but it actually exists
168+
engine.expect(week[IS_ENUM]).toBe(true);
169+
engine.expect(week[IS_ENUM_IN_NODE]).toBe(true);
170+
// @ts-expect-error: because IS_ENUM and Symbol.for('[IsEnum]') are equal
171+
engine.expect(week[Symbol.for('[IsEnum]')]).toBe(true);
172172
}
173173
);
174174

0 commit comments

Comments
 (0)