Skip to content

Commit 85957a8

Browse files
committed
feat: move hasInstance to enum.items
1 parent be1e389 commit 85957a8

File tree

5 files changed

+44
-31
lines changed

5 files changed

+44
-31
lines changed

src/enum-collection.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,8 @@ export class EnumCollectionClass<
5252
* - **CN:** 布尔值,表示这是一个枚举集合实例
5353
*/
5454
readonly [IS_ENUM] = true;
55-
[Symbol.hasInstance](this: EnumCollectionClass<T, K, V>, instance: unknown): boolean {
56-
// intentionally use == to support both number and string format value
57-
return this.items.some(
58-
// eslint-disable-next-line eqeqeq
59-
(i) => instance == i.value || instance === i.key
60-
);
55+
[Symbol.hasInstance]<T>(instance: T): instance is Extract<T, K | V> {
56+
return instance instanceof this.items;
6157
}
6258
/**
6359
* The enum collection name, supports localization. Note that it usually returns a string, but if

src/enum-items.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ export class EnumItemsArray<
5252
super(...items);
5353
this.__raw__ = raw;
5454
}
55+
name?: string | undefined;
56+
[Symbol.hasInstance]<T>(instance: T): instance is Extract<T, K | V> {
57+
// intentionally use == to support both number and string format value
58+
return this.some(
59+
// eslint-disable-next-line eqeqeq
60+
(i) => (instance as unknown as V) == i.value || (instance as unknown as K) === i.key
61+
);
62+
}
5563

5664
label<KV extends V | K | NonNullable<PrimitiveOf<V>> | NonNullable<PrimitiveOf<K>> | undefined>(
5765
keyOrValue: KV
@@ -220,6 +228,12 @@ export interface IEnumItems<
220228
* - **CN:** 布尔值,表示这是一个枚举项数组
221229
*/
222230
[IS_ENUM_ITEMS]: true;
231+
/**
232+
* - **EN:** A method that determines if a constructor object recognizes an object as one of the
233+
* constructor’s instances. Called by the semantics of the `instanceof` operator.
234+
* - **CN:** 一个方法,用于确定构造函数对象是否将对象识别为构造函数的实例之一。由 `instanceof` 运算符的语义调用。
235+
*/
236+
[Symbol.hasInstance]<T>(instance: T): instance is Extract<T, K | V>;
223237
/**
224238
* - **EN:** The enum collection name, supports localization.
225239
*

src/enum.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,6 @@ export type IEnum<
202202
* - **CN:** 布尔值,表示这是一个枚举类
203203
*/
204204
[IS_ENUM]: true;
205-
/**
206-
* - **EN:** A method that determines if a constructor object recognizes an object as one of the
207-
* constructor’s instances. Called by the semantics of the `instanceof` operator.
208-
* - **CN:** 一个方法,用于确定构造函数对象是否将对象识别为构造函数的实例之一。由 `instanceof` 运算符的语义调用。
209-
*/
210-
[Symbol.hasInstance]<T>(instance: T): instance is Extract<T, K | V>;
211205
} & {
212206
// Add enum item values, just like native enums
213207
[key in K]: ValueTypeFromSingleInit<T[key], key, T[K] extends number | undefined ? number : key>;

test/test-suites/enum-collection.ts

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defaultLocalize, IS_ENUM as IS_ENUM_IN_NODE } from '@enum-plus';
1+
import { 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';
@@ -135,7 +135,7 @@ const testEnumCollection = (engine: TestEngineBase) => {
135135
engine.expect(strangeEnum.items.toMenu()).toHaveLength(Object.keys(strangeEnumConfig).length);
136136
engine.expect(strangeEnum.toValueMap).toBe(5);
137137
const map: Record<string, { text: string | undefined }> = {};
138-
Object.entries(strangeEnumConfig).forEach(([key, item]) => {
138+
Object.entries(strangeEnumConfig).forEach(([, item]) => {
139139
map[item.value] = { text: item.label };
140140
});
141141
engine.expect(strangeEnum.items.toValueMap()).toEqual(map);
@@ -173,25 +173,18 @@ const testEnumCollection = (engine: TestEngineBase) => {
173173
);
174174

175175
engine.test(
176-
'enums.value/enums.key/enums.label should be applicable to "instanceof" operator',
177-
({ EnumPlus: { Enum }, WeekConfig: { StandardWeekConfig, lang, getLocales, genSillyLocalizer } }) => {
176+
'Enum should be applicable to "instanceof" operator',
177+
({ EnumPlus: { Enum }, WeekConfig: { StandardWeekConfig } }) => {
178178
const week = Enum(StandardWeekConfig);
179-
return { week, lang, getLocales, genSillyLocalizer };
179+
return { week };
180180
},
181-
({ week, lang, getLocales, genSillyLocalizer }) => {
182-
const sillyLocalize = genSillyLocalizer(lang, getLocales, defaultLocalize);
183-
engine.expect((0 as unknown) instanceof (week as unknown as () => void)).toBeTruthy();
184-
engine.expect(('Sunday' as unknown) instanceof (week as unknown as () => void)).toBeTruthy();
185-
engine
186-
.expect((sillyLocalize('weekday.sunday') as unknown) instanceof (week as unknown as () => void))
187-
.toBeTruthy();
188-
engine.expect((6 as unknown) instanceof (week as unknown as () => void)).toBeTruthy();
189-
engine.expect(('Saturday' as unknown) instanceof (week as unknown as () => void)).toBeTruthy();
190-
engine
191-
.expect((sillyLocalize('weekday.saturday') as unknown) instanceof (week as unknown as () => void))
192-
.toBeTruthy();
193-
engine.expect((7 as unknown) instanceof (week as unknown as () => void)).toBeFalsy();
194-
engine.expect(('[Not Exists]' as unknown) instanceof (week as unknown as () => void)).toBeFalsy();
181+
({ week }) => {
182+
engine.expect((0 as unknown) instanceof week).toBeTruthy();
183+
engine.expect(('Sunday' as unknown) instanceof week).toBeTruthy();
184+
engine.expect((6 as unknown) instanceof week).toBeTruthy();
185+
engine.expect(('Saturday' as unknown) instanceof week).toBeTruthy();
186+
engine.expect((7 as unknown) instanceof week).toBeFalsy();
187+
engine.expect(('[Not Exists]' as unknown) instanceof week).toBeFalsy();
195188
}
196189
);
197190
});

test/test-suites/enum-items.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,22 @@ export function addEnumValuesTestSuite(engine: TestEngineBase) {
257257
}
258258
);
259259

260+
engine.test(
261+
'enum.items should be applicable to "instanceof" operator',
262+
({ EnumPlus: { Enum }, WeekConfig: { StandardWeekConfig } }) => {
263+
const week = Enum(StandardWeekConfig);
264+
return { week };
265+
},
266+
({ week }) => {
267+
engine.expect((0 as unknown) instanceof week.items).toBeTruthy();
268+
engine.expect(('Sunday' as unknown) instanceof week.items).toBeTruthy();
269+
engine.expect((6 as unknown) instanceof week.items).toBeTruthy();
270+
engine.expect(('Saturday' as unknown) instanceof week.items).toBeTruthy();
271+
engine.expect((7 as unknown) instanceof week.items).toBeFalsy();
272+
engine.expect(('[Not Exists]' as unknown) instanceof week.items).toBeFalsy();
273+
}
274+
);
275+
260276
engine.test(
261277
'enums.valueType should throw error when called at runtime',
262278
({ EnumPlus: { Enum }, WeekConfig: { StandardWeekConfig } }) => {

0 commit comments

Comments
 (0)