Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit a06125b

Browse files
committed
Make ArrayStringMap file
1 parent 1dd6f68 commit a06125b

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

arrayStringMap.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
export default class ArrayStringMap<K extends any[], V> implements Map<K, V> {
2+
private readonly _map: Map<string, V> = new Map<string, V>();
3+
private readonly _converterInfo: Map<string, K> = new Map<string, K>();
4+
private readonly _sep: string;
5+
6+
public constructor(sep: string = '\u200b') {
7+
this._sep = sep;
8+
}
9+
10+
11+
public get sep(): string {
12+
return this._sep;
13+
}
14+
15+
public get [Symbol.toStringTag](): string {
16+
return "ArrayStringMap";
17+
}
18+
19+
public get size(): number {
20+
return this._map.size;
21+
}
22+
23+
private encodeArray(arr: K): string {
24+
return arr.map(x => x.toString()).join(this._sep)
25+
}
26+
27+
28+
[Symbol.iterator](): IterableIterator<[K, V]> {
29+
return this.entries();
30+
}
31+
32+
clear(): void {
33+
this._map.clear();
34+
this._converterInfo.clear();
35+
}
36+
37+
delete(key: K): boolean {
38+
return false;
39+
}
40+
41+
* entries(): IterableIterator<[K, V]> {
42+
for (const [key, value] of this._map.entries()) {
43+
// TypeScript complains that this will be undefined, but the items in
44+
// `this._converterInfo` and `this._map` will always be defined in each other.
45+
const arr: K = this._converterInfo.get(key) as K;
46+
yield [arr, value];
47+
}
48+
}
49+
50+
forEach(callbackfn: (value: V, key: K, map: Map<K, V>) => void, thisArg?: any): void {
51+
this._converterInfo.forEach((value, key) => {
52+
// TypeScript complains that this will be undefined, but the items in
53+
// `this._converterInfo` and `this._map` will always be defined in each other.
54+
return callbackfn(this._map.get(key)!, value, thisArg);
55+
});
56+
}
57+
58+
get(key: K): V | undefined {
59+
return this._map.get(this.encodeArray(key));
60+
}
61+
62+
has(key: K): boolean {
63+
return this._map.has(this.encodeArray(key));
64+
}
65+
66+
keys(): IterableIterator<K> {
67+
return this._converterInfo.values();
68+
}
69+
70+
set(key: K, value: V): this {
71+
const encodedKey = this.encodeArray(key);
72+
this._map.set(encodedKey, value);
73+
this._converterInfo.set(encodedKey, key);
74+
return this;
75+
}
76+
77+
values(): IterableIterator<V> {
78+
return this._map.values();
79+
}
80+
81+
}

0 commit comments

Comments
 (0)