Skip to content

Commit fa181bf

Browse files
committed
feat: tmp
1 parent 8a973c3 commit fa181bf

File tree

5 files changed

+140
-68
lines changed

5 files changed

+140
-68
lines changed

enum-plus-v3.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252
- `enum.valuesEnum` method is removed, use `enum.toValueMap` instead.
5353
- The behavior of `enum.values` is changed. Use `enum.items` for the old behavior.
5454

55+
## Misc
56+
57+
- The warning message for trying to modify enum items has been removed. First, in order to avoid circular references within enum items (which would affect serialization), we removed the internal `proxy` and used `getter/setter` instead. However, this brought about another problem: when printing enum items in the browser console or node.js, the `key`, `value`, and `label` cannot display their values, but show `[Getter/Setter]` instead. This somewhat affects the debugging experience. Sorry @yyz945947732, you introduced this feature, which is very good, but after weighing the pros and cons, we still had to remove this feature.
58+
5559
## Bug Fixes
5660

5761
- Fix the issue where sourcemap files under the `lib` directory could not be parsed.

src/enum-collection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class EnumCollectionClass<
4242
implements
4343
Omit<IEnumItems<T, K, V>, typeof IS_ENUM_ITEMS | typeof ITEMS | typeof KEYS | typeof VALUES | 'labels' | 'meta'>
4444
{
45-
private __options__: EnumItemOptions | undefined;
45+
private readonly __options__: EnumItemOptions | undefined;
4646
// used for e2e serialization
4747
private readonly __items__!: EnumItemsArray<T, K, V>;
4848

src/enum-items.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,18 @@ export class EnumItemsArray<
114114
this[VALUES] = values;
115115
Object.freeze(values);
116116

117-
this._runtimeError = undefined!;
118117
// Generate labels array
119118
Object.defineProperty(this, 'labels', {
120-
enumerable: true,
121-
configurable: false,
122119
get: function (this: EnumItemsArray<T, K, V>) {
123120
// Cannot save to static array because labels may be localized contents
124121
// Should not use `items` in the closure because the getter function cannot be fully serialized
125-
return this.map((item) => item.label);
122+
return Array.from(this).map((item) => item.label);
126123
},
124+
enumerable: true,
125+
configurable: false,
127126
});
128127

128+
this._runtimeError = undefined!;
129129
Object.defineProperty(this, '_runtimeError', {
130130
value: function (this: EnumItemsArray<T, K, V>, name: string) {
131131
return `The ${name} property of the enumeration is only allowed to be used to declare the ts type, and cannot be accessed at runtime! Please use the typeof operator in the ts type, for example: typeof Week.${name}`;
@@ -269,9 +269,9 @@ export class EnumItemsArray<
269269
>[] {
270270
const { valueField = 'value' as FOV, labelField = 'label' as FOL } = config ?? {};
271271
if (valueField === 'value' && labelField === 'label') {
272-
return this;
272+
return Array.from(this);
273273
} else {
274-
return this.map((item) => {
274+
return Array.from(this).map((item) => {
275275
const valueFieldName = typeof valueField === 'function' ? valueField(item) : (valueField as string);
276276
const labelFieldName = typeof labelField === 'function' ? labelField(item) : (labelField as string);
277277
const listItem = {
@@ -310,11 +310,11 @@ export class EnumItemsArray<
310310
}
311311

312312
toMenu(): MenuItemOption<V>[] {
313-
return this.map(({ value, label }) => ({ key: value, label }));
313+
return Array.from(this).map(({ value, label }) => ({ key: value, label }));
314314
}
315315

316316
toFilter(): ColumnFilterItem<V>[] {
317-
return this.map(({ value, label }) => ({ text: label, value }));
317+
return Array.from(this).map(({ value, label }) => ({ text: label, value }));
318318
}
319319

320320
/** Stub method, only for typing usages, not for runtime calling */

test/test-suites/enum-collection.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ const testEnumCollection = (engine: TestEngineBase) => {
171171
return { week };
172172
},
173173
({ week }) => {
174+
// console.log(week.__items__);
174175
engine.expect((0 as unknown) instanceof week).toBeTruthy();
175176
engine.expect(('Sunday' as unknown) instanceof week).toBeTruthy();
176177
engine.expect((6 as unknown) instanceof week).toBeTruthy();

0 commit comments

Comments
 (0)