Skip to content

Commit c1621dc

Browse files
committed
chore: enhance Enum.extends method to support getter and setter
1 parent d57bebf commit c1621dc

File tree

5 files changed

+43
-8
lines changed

5 files changed

+43
-8
lines changed

CHANGELOG.md

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

33
# enum-plus Changelog
44

5+
## 3.1.3
6+
7+
2025-11-17
8+
9+
### Features
10+
11+
- ✨ Enhance `Enum.extends` method to support getter and setter properties in the extension object.
12+
513
## 3.1.2
614

715
2025-11-7

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@
190190
"father": "^4.6.3",
191191
"glob": "^11.0.3",
192192
"jest": "^29.7.0",
193-
"jsoneo": "^1.0.1",
193+
"jsoneo": "^1.0.2",
194194
"npm-run-all2": "^8.0.4",
195195
"playwright": "^1.55.0",
196196
"rollup": "^4.50.0",

src/enum.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Enum.extends = function (obj: Record<string, unknown> | undefined) {
6767
if (obj !== undefined && Object.prototype.toString.call(obj) !== '[object Object]') {
6868
throw new Error('The extension of Enum must be an object');
6969
}
70-
Object.assign(EnumExtensionClass.prototype, obj);
70+
Object.defineProperties(EnumExtensionClass.prototype, Object.getOwnPropertyDescriptors(obj));
7171
};
7272
// eslint-disable-next-line @typescript-eslint/no-explicit-any
7373
Enum.install = <T extends PluginFunc<any>>(plugin: T, options?: Parameters<T>[0]) => {

test/test-suites/extension.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { EnumItemClass, IEnum } from '@enum-plus';
12
import type TestEngineBase from '../engines/base';
23

34
// import './extension-type';
@@ -33,6 +34,30 @@ const testExtension = (engine: TestEngineBase<'jest' | 'playwright'>) => {
3334
.toEqual(weekEnum.items.map((item) => ({ value: item.value, title: item.label })));
3435
}
3536
);
37+
engine.test(
38+
'Should allow extend getters',
39+
({ EnumPlus: { Enum }, WeekConfig: { StandardWeekConfig } }) => {
40+
const extend = {
41+
get all() {
42+
return (
43+
this as unknown as IEnum<
44+
typeof StandardWeekConfig,
45+
keyof typeof StandardWeekConfig,
46+
(typeof StandardWeekConfig)[keyof typeof StandardWeekConfig]['value']
47+
>
48+
).items;
49+
},
50+
};
51+
Enum.extends(extend);
52+
const weekEnum = Enum(StandardWeekConfig);
53+
return { weekEnum, all: weekEnum.all };
54+
},
55+
({ weekEnum, all }) => {
56+
engine.expect(all).toBeInstanceOf(Array);
57+
engine.expect(all.length).toBe(7);
58+
engine.expect(all).toEqual(weekEnum.items);
59+
}
60+
);
3661
engine.test(
3762
'Should allow clearing global extension',
3863
({ EnumPlus: { Enum }, WeekConfig: { StandardWeekConfig } }) => {
@@ -124,6 +149,8 @@ declare module 'enum-plus/extension' {
124149
interface EnumExtension<T, K, V> {
125150
isWeekend(value: number): boolean;
126151
toMySelect: () => { value: V; title: string }[];
152+
// @ts-expect-error: because want to declare new getter
153+
all: EnumItemClass<T[K], K, V>[];
127154
}
128155
}
129156

0 commit comments

Comments
 (0)