ts-enum-next 1.0.0 → 1.0.1
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/package.json +11 -5
- package/src/index.ts +0 -78
- package/tsconfig.json +0 -16
package/package.json
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-enum-next",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Ultimate Enum Enhancement for TypeScript",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"prepublishOnly": "pnpm run build"
|
|
10
|
+
},
|
|
7
11
|
"keywords": [
|
|
8
12
|
"typescript",
|
|
9
13
|
"enum",
|
|
@@ -12,12 +16,14 @@
|
|
|
12
16
|
"enum-next",
|
|
13
17
|
"java"
|
|
14
18
|
],
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"README.md",
|
|
22
|
+
"README.zh-CN.md"
|
|
23
|
+
],
|
|
15
24
|
"author": "rico",
|
|
16
25
|
"license": "ISC",
|
|
17
26
|
"devDependencies": {
|
|
18
27
|
"typescript": "^5.8.3"
|
|
19
|
-
},
|
|
20
|
-
"scripts": {
|
|
21
|
-
"build": "tsc"
|
|
22
28
|
}
|
|
23
|
-
}
|
|
29
|
+
}
|
package/src/index.ts
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
type EnumValueType = number | string;
|
|
2
|
-
|
|
3
|
-
abstract class Enum<T extends EnumValueType> {
|
|
4
|
-
private static _values: Map<typeof Enum, Enum<any>[]> = new Map();
|
|
5
|
-
private static _valueMap: Map<typeof Enum, Map<EnumValueType, Enum<any>>> = new Map();
|
|
6
|
-
private static _nameMap: Map<typeof Enum, Map<string, Enum<any>>> = new Map();
|
|
7
|
-
|
|
8
|
-
constructor(
|
|
9
|
-
public readonly value: T,
|
|
10
|
-
public readonly name: string,
|
|
11
|
-
public readonly description?: string
|
|
12
|
-
) {
|
|
13
|
-
const enumClass = this.constructor as typeof Enum;
|
|
14
|
-
|
|
15
|
-
// 初始化存储结构
|
|
16
|
-
if (!Enum._values.has(enumClass)) {
|
|
17
|
-
Enum._values.set(enumClass, []);
|
|
18
|
-
Enum._valueMap.set(enumClass, new Map());
|
|
19
|
-
Enum._nameMap.set(enumClass, new Map());
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// 注册当前枚举实例
|
|
23
|
-
Enum._values.get(enumClass)!.push(this);
|
|
24
|
-
Enum._valueMap.get(enumClass)!.set(value, this);
|
|
25
|
-
Enum._nameMap.get(enumClass)!.set(name, this);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// 获取所有枚举值
|
|
29
|
-
static values<T extends Enum<any>>(this: typeof Enum): T[] {
|
|
30
|
-
return (this._values.get(this) as T[]) || [];
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// 通过值获取枚举实例
|
|
34
|
-
static fromValue<T extends Enum<any>>(this: any, value: T['value']): T {
|
|
35
|
-
const enumInstance = this._valueMap.get(this)?.get(value);
|
|
36
|
-
if (!enumInstance) {
|
|
37
|
-
console.error(`No enum value ${value} found`);
|
|
38
|
-
}
|
|
39
|
-
return enumInstance as T;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// 通过名称获取枚举实例
|
|
43
|
-
static fromName<T extends Enum<any>>(this: typeof Enum, name: string): T {
|
|
44
|
-
const enumInstance = this._nameMap.get(this)?.get(name);
|
|
45
|
-
if (!enumInstance) {
|
|
46
|
-
console.error(`No enum name ${name} found`);
|
|
47
|
-
}
|
|
48
|
-
return enumInstance as T;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// 创建枚举集合
|
|
52
|
-
static setOf<T extends Enum<any>>(this: new () => T, ...items: T[]): Set<T> {
|
|
53
|
-
return new Set(items);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// 创建枚举映射表
|
|
57
|
-
static enumMap<T extends Enum<any>, V>(this: typeof Enum, map: Record<string | number, V>): Map<T, V> {
|
|
58
|
-
const result = new Map<T, V>();
|
|
59
|
-
for (const [key, value] of Object.entries(map)) {
|
|
60
|
-
const enumKey = isNaN(Number(key)) ? (this.fromName(key) as T) : (this.fromValue(key as any) as T);
|
|
61
|
-
result.set(enumKey, value);
|
|
62
|
-
}
|
|
63
|
-
return result;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// 重写toString方法
|
|
67
|
-
toString(): string {
|
|
68
|
-
return this.name;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// 重写valueOf方法
|
|
72
|
-
valueOf(): T {
|
|
73
|
-
return this.value;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export { Enum };
|
|
78
|
-
export type { EnumValueType };
|
package/tsconfig.json
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2020",
|
|
4
|
-
"module": "ESNext",
|
|
5
|
-
"esModuleInterop": true,
|
|
6
|
-
"strict": true,
|
|
7
|
-
"skipLibCheck": true,
|
|
8
|
-
"rootDir": "./src",
|
|
9
|
-
"outDir": "./dist",
|
|
10
|
-
"declaration": true,
|
|
11
|
-
"incremental": false,
|
|
12
|
-
"baseUrl": "./",
|
|
13
|
-
"moduleResolution": "node"
|
|
14
|
-
},
|
|
15
|
-
"include": ["src"]
|
|
16
|
-
}
|