Repository navigation
Expand file tree
/
Copy pathdictionary.mts
More file actions
58 lines (47 loc) · 1.69 KB
/
Copy pathdictionary.mts
File metadata and controls
58 lines (47 loc) · 1.69 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/** A dictionary of key-value pairs compatible with C# `IDictionary<TKey, TValue>`. */
export class Dictionary<TKey, TValue> {
protected readonly map: Map<TKey, TValue>;
constructor(entries?: Iterable<[TKey, TValue]>) {
this.map = new Map(entries);
}
/** Number of key-value pairs in the dictionary. */
get count(): number {
return this.map.size;
}
/** Associates the specified value with the specified key. */
add(key: TKey, value: TValue): void {
this.map.set(key, value);
}
/** Whether the dictionary contains the specified key. */
containsKey(key: TKey): boolean {
return this.map.has(key);
}
/** Removes the value with the specified key from the dictionary.
* @returns true when the key was removed; false when it wasn't found. */
remove(key: TKey): boolean {
return this.map.delete(key);
}
/** Removes all key-value pairs from the dictionary. */
clear(): void {
this.map.clear();
}
/** Returns the value associated with the specified key. */
getAt(key: TKey): TValue {
return this.map.get(key) as TValue;
}
/** Associates the specified value with the specified key. */
setAt(key: TKey, value: TValue): void {
this.map.set(key, value);
}
/** Returns a fresh array with a snapshot of the current keys. */
getKeys(): TKey[] {
return Array.from(this.map.keys());
}
/** Returns a fresh array with a snapshot of the current values. */
getValues(): TValue[] {
return Array.from(this.map.values());
}
[Symbol.iterator](): IterableIterator<[TKey, TValue]> {
return this.map[Symbol.iterator]();
}
}