ts-enum-next 1.0.7 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +2 -2
- package/dist/index.js +12 -10
- package/dist/index.test.js +20 -11
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -15,8 +15,8 @@ declare abstract class Enum<T extends string | number = string | number> {
|
|
|
15
15
|
*/
|
|
16
16
|
description?: unknown | undefined);
|
|
17
17
|
static values<T extends Enum>(this: new (...args: any[]) => T): T[];
|
|
18
|
-
static fromValue<T extends Enum>(this: new (...args: any[]) => T, value: T['value']): T;
|
|
19
|
-
static fromName<T extends Enum>(this: new (...args: any[]) => T, name: string): T;
|
|
18
|
+
static fromValue<T extends Enum>(this: new (...args: any[]) => T, value: T['value']): T | undefined;
|
|
19
|
+
static fromName<T extends Enum>(this: new (...args: any[]) => T, name: string): T | undefined;
|
|
20
20
|
static setOf<T extends Enum>(this: new (...args: any[]) => T, ...items: T[]): Set<T>;
|
|
21
21
|
static enumMap<V, T extends Enum = Enum>(this: new (...args: any[]) => T, map: Record<string | number, V>): Map<T, V>;
|
|
22
22
|
toString(): string;
|
package/dist/index.js
CHANGED
|
@@ -25,19 +25,21 @@ class Enum {
|
|
|
25
25
|
}
|
|
26
26
|
// 通过值获取枚举实例
|
|
27
27
|
static fromValue(value) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
if (value === undefined || value === null)
|
|
29
|
+
return undefined;
|
|
30
|
+
const valueMap = Enum._valueMap.get(this);
|
|
31
|
+
if (!valueMap)
|
|
32
|
+
return undefined;
|
|
33
|
+
return valueMap.get(value);
|
|
33
34
|
}
|
|
34
35
|
// 通过名称获取枚举实例
|
|
35
36
|
static fromName(name) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
37
|
+
if (name === undefined || name === null || name === '')
|
|
38
|
+
return undefined;
|
|
39
|
+
const nameMap = Enum._nameMap.get(this);
|
|
40
|
+
if (!nameMap)
|
|
41
|
+
return undefined;
|
|
42
|
+
return nameMap.get(name);
|
|
41
43
|
}
|
|
42
44
|
// 创建枚举集合
|
|
43
45
|
static setOf(...items) {
|
package/dist/index.test.js
CHANGED
|
@@ -88,11 +88,15 @@ describe('Enum', () => {
|
|
|
88
88
|
expect(HttpStatus.fromValue(404)).toBe(HttpStatus.NOT_FOUND);
|
|
89
89
|
expect(HttpStatus.fromValue(500)).toBe(HttpStatus.SERVER_ERROR);
|
|
90
90
|
});
|
|
91
|
-
it('should
|
|
92
|
-
expect(
|
|
91
|
+
it('should return undefined for non-existent numeric value', () => {
|
|
92
|
+
expect(HttpStatus.fromValue(999)).toBeUndefined();
|
|
93
93
|
});
|
|
94
|
-
it('should
|
|
95
|
-
expect(
|
|
94
|
+
it('should return undefined for non-existent string value', () => {
|
|
95
|
+
expect(OrderStatus.fromValue('INVALID')).toBeUndefined();
|
|
96
|
+
});
|
|
97
|
+
it('should return undefined when value is null or undefined', () => {
|
|
98
|
+
expect(HttpStatus.fromValue(undefined)).toBeUndefined();
|
|
99
|
+
expect(HttpStatus.fromValue(null)).toBeUndefined();
|
|
96
100
|
});
|
|
97
101
|
});
|
|
98
102
|
describe('fromName()', () => {
|
|
@@ -111,11 +115,16 @@ describe('Enum', () => {
|
|
|
111
115
|
expect(HttpStatus.fromName('NOT_FOUND')).toBe(HttpStatus.NOT_FOUND);
|
|
112
116
|
expect(HttpStatus.fromName('SERVER_ERROR')).toBe(HttpStatus.SERVER_ERROR);
|
|
113
117
|
});
|
|
114
|
-
it('should
|
|
115
|
-
expect(
|
|
118
|
+
it('should return undefined for non-existent name', () => {
|
|
119
|
+
expect(HttpStatus.fromName('INVALID')).toBeUndefined();
|
|
116
120
|
});
|
|
117
121
|
it('should be case-sensitive', () => {
|
|
118
|
-
expect(
|
|
122
|
+
expect(HttpStatus.fromName('ok')).toBeUndefined();
|
|
123
|
+
});
|
|
124
|
+
it('should return undefined when name is null, undefined or empty string', () => {
|
|
125
|
+
expect(HttpStatus.fromName(undefined)).toBeUndefined();
|
|
126
|
+
expect(HttpStatus.fromName(null)).toBeUndefined();
|
|
127
|
+
expect(HttpStatus.fromName('')).toBeUndefined();
|
|
119
128
|
});
|
|
120
129
|
});
|
|
121
130
|
describe('setOf()', () => {
|
|
@@ -240,12 +249,12 @@ describe('Enum', () => {
|
|
|
240
249
|
});
|
|
241
250
|
it('should not interfere with each other when querying by value', () => {
|
|
242
251
|
// 虽然值相同,但是不同的枚举类
|
|
243
|
-
expect(
|
|
244
|
-
expect(
|
|
252
|
+
expect(HttpStatus.fromValue('PENDING')).toBeUndefined();
|
|
253
|
+
expect(OrderStatus.fromValue(200)).toBeUndefined();
|
|
245
254
|
});
|
|
246
255
|
it('should not interfere with each other when querying by name', () => {
|
|
247
|
-
expect(
|
|
248
|
-
expect(
|
|
256
|
+
expect(HttpStatus.fromName('Pending')).toBeUndefined();
|
|
257
|
+
expect(OrderStatus.fromName('OK')).toBeUndefined();
|
|
249
258
|
});
|
|
250
259
|
});
|
|
251
260
|
describe('Edge cases', () => {
|