-
-
Notifications
You must be signed in to change notification settings - Fork 269
/
test-map.ts
40 lines (37 loc) · 981 Bytes
/
test-map.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// A map of test objects, but make sure that we don't get confused
// if we get the Test proxy or the underlying TestBase.
import { Base } from '@tapjs/core'
const kSerializationKey = Symbol.for('@tapjs/node-serialize.key')
const getKey = (
t: Base & { [kSerializationKey]?: string },
): string => {
const k = t[kSerializationKey]
if (k) return k
const n = String(Math.random())
Object.defineProperty(t, kSerializationKey, {
value: n,
writable: false,
configurable: true,
enumerable: false,
})
return n
}
export class TestMap<T extends {}> extends Map<Base, T> {
constructor(items?: [Base, T][]) {
super()
if (items) {
for (const [t, v] of items) {
this.set(t, v)
}
}
}
get(t: Base): T | undefined {
return super.get(getKey(t) as unknown as Base)
}
has(t: Base) {
return super.has(getKey(t) as unknown as Base)
}
set(t: Base, v: T) {
return super.set(getKey(t) as unknown as Base, v)
}
}