11type Primitive = string | number | symbol | boolean | null | undefined
22type KeyType = object | Primitive
33
4+ /**
5+ * HybridMap 是一个支持对象和原始类型(如字符串、数字、布尔值等)作为 key 的 Map 实现。
6+ * - 对象类型的 key 使用 WeakMap 存储,原始类型的 key 使用 Map 存储。
7+ * - 这样既可以用对象做 key,也可以用字符串、数字等做 key,且对象 key 不会阻止垃圾回收。
8+ * - 不支持遍历和 size 属性,因为 WeakMap 无法遍历。
9+ *
10+ * 示例用法:
11+ * ```ts
12+ * const map = createHybridMap<any, string>([[{}, 'obj'], ['a', 'str']])
13+ * map.set(123, 'num')
14+ * map.set({}, 'another obj')
15+ * ```
16+ */
417export class HybridMap < K extends KeyType , V > {
518 private objectStore : WeakMap < object , V >
619 private primitiveStore : Map < Primitive , V >
720
21+ /**
22+ * 构造函数,可选初始化 entries
23+ * @param entries 初始键值对数组
24+ */
825 constructor ( entries ?: readonly ( readonly [ K , V ] ) [ ] | null ) {
926 this . objectStore = new WeakMap ( )
1027 this . primitiveStore = new Map ( )
@@ -15,10 +32,16 @@ export class HybridMap<K extends KeyType, V> {
1532 }
1633 }
1734
35+ /** 判断 key 是否为 object 类型 */
1836 private isObjectKey ( key : K ) : key is Record < any , any > {
1937 return typeof key === 'object' && key !== null
2038 }
2139
40+ /**
41+ * 设置键值对
42+ * @param key 键
43+ * @param value 值
44+ */
2245 set ( key : K , value : V ) : this {
2346 if ( this . isObjectKey ( key ) ) {
2447 this . objectStore . set ( key , value )
@@ -29,6 +52,10 @@ export class HybridMap<K extends KeyType, V> {
2952 return this
3053 }
3154
55+ /**
56+ * 获取指定 key 的值
57+ * @param key 键
58+ */
3259 get ( key : K ) : V | undefined {
3360 if ( this . isObjectKey ( key ) ) {
3461 return this . objectStore . get ( key as object )
@@ -38,6 +65,10 @@ export class HybridMap<K extends KeyType, V> {
3865 }
3966 }
4067
68+ /**
69+ * 判断是否存在指定 key
70+ * @param key 键
71+ */
4172 has ( key : K ) : boolean {
4273 if ( this . isObjectKey ( key ) ) {
4374 return this . objectStore . has ( key as object )
@@ -47,6 +78,10 @@ export class HybridMap<K extends KeyType, V> {
4778 }
4879 }
4980
81+ /**
82+ * 删除指定 key
83+ * @param key 键
84+ */
5085 delete ( key : K ) : boolean {
5186 if ( this . isObjectKey ( key ) ) {
5287 return this . objectStore . delete ( key as object )
@@ -56,48 +91,68 @@ export class HybridMap<K extends KeyType, V> {
5691 }
5792 }
5893
94+ /** 清空所有 key */
5995 clear ( ) : void {
6096 this . primitiveStore . clear ( )
6197 this . objectStore = new WeakMap ( )
6298 }
6399
100+ /** 不支持 size 属性,因 WeakMap 无法遍历 */
64101 get size ( ) : number {
65102 throw new Error (
66103 'HybridMap does not support size due to WeakMap limitations.' ,
67104 )
68105 }
69106
107+ /** 不支持 forEach,因 WeakMap 无法遍历 */
70108 forEach ( ) : void {
71109 throw new Error (
72110 'HybridMap does not support forEach due to WeakMap limitations.' ,
73111 )
74112 }
75113
114+ /** 不支持 keys 迭代,因 WeakMap 无法遍历 */
76115 keys ( ) {
77116 throw new Error (
78117 'HybridMap does not support iteration due to WeakMap limitations.' ,
79118 )
80119 }
81120
121+ /** 不支持 values 迭代,因 WeakMap 无法遍历 */
82122 values ( ) {
83123 throw new Error (
84124 'HybridMap does not support iteration due to WeakMap limitations.' ,
85125 )
86126 }
87127
128+ /** 不支持 entries 迭代,因 WeakMap 无法遍历 */
88129 entries ( ) {
89130 throw new Error (
90131 'HybridMap does not support iteration due to WeakMap limitations.' ,
91132 )
92133 }
93134
135+ /** 不支持迭代器,因 WeakMap 无法遍历 */
94136 [ Symbol . iterator ] ( ) {
95137 throw new Error (
96138 'HybridMap does not support iteration due to WeakMap limitations.' ,
97139 )
98140 }
99141}
100142
143+ /**
144+ * createHybridMap 是 HybridMap 的工厂函数。
145+ * 用于创建支持对象和原始类型作为 key 的 Map 实例。
146+ * @param entries 可选的初始键值对数组
147+ * @returns HybridMap 实例
148+ *
149+ * 示例用法:
150+ * ```ts
151+ * const map = createHybridMap<any, string>([[{}, 'obj'], ['a', 'str']])
152+ * map.set(123, 'num')
153+ * map.set({}, 'another obj')
154+ * ```
155+ */
101156export function createHybridMap < K extends KeyType , V > (
102157 entries ?: readonly ( readonly [ K , V ] ) [ ] | null ,
103158) : HybridMap < K , V > {
0 commit comments