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 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
- const enumInstance = Enum._valueMap.get(this)?.get(value);
29
- if (!enumInstance) {
30
- throw new Error(`No enum value ${value} found`);
31
- }
32
- return enumInstance;
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
- const enumInstance = Enum._nameMap.get(this)?.get(name);
37
- if (!enumInstance) {
38
- throw new Error(`No enum name ${name} found`);
39
- }
40
- return enumInstance;
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) {
@@ -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 throw error for non-existent numeric value', () => {
92
- expect(() => HttpStatus.fromValue(999)).toThrow('No enum value 999 found');
91
+ it('should return undefined for non-existent numeric value', () => {
92
+ expect(HttpStatus.fromValue(999)).toBeUndefined();
93
93
  });
94
- it('should throw error for non-existent string value', () => {
95
- expect(() => OrderStatus.fromValue('INVALID')).toThrow('No enum value INVALID found');
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 throw error for non-existent name', () => {
115
- expect(() => HttpStatus.fromName('INVALID')).toThrow('No enum name INVALID found');
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(() => HttpStatus.fromName('ok')).toThrow('No enum name ok found');
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(() => HttpStatus.fromValue('PENDING')).toThrow();
244
- expect(() => OrderStatus.fromValue(200)).toThrow();
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(() => HttpStatus.fromName('Pending')).toThrow();
248
- expect(() => OrderStatus.fromName('OK')).toThrow();
256
+ expect(HttpStatus.fromName('Pending')).toBeUndefined();
257
+ expect(OrderStatus.fromName('OK')).toBeUndefined();
249
258
  });
250
259
  });
251
260
  describe('Edge cases', () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-enum-next",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "description": "Ultimate Enum Enhancement for TypeScript",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",