टाइपस्क्रिप्ट उपयोगकर्ताओं के लिए दूसरों के उत्तर (विशेषकर czerny ) को एक अच्छा प्रकार-सुरक्षित और पुन: प्रयोज्य आधार के रूप में सामान्यीकृत किया जा सकता है:
/**
* Map that stringifies the key objects in order to leverage
* the javascript native Map and preserve key uniqueness.
*/
abstract class StringifyingMap<K, V> {
private map = new Map<string, V>();
private keyMap = new Map<string, K>();
has(key: K): boolean {
let keyString = this.stringifyKey(key);
return this.map.has(keyString);
}
get(key: K): V {
let keyString = this.stringifyKey(key);
return this.map.get(keyString);
}
set(key: K, value: V): StringifyingMap<K, V> {
let keyString = this.stringifyKey(key);
this.map.set(keyString, value);
this.keyMap.set(keyString, key);
return this;
}
/**
* Puts new key/value if key is absent.
* @param key key
* @param defaultValue default value factory
*/
putIfAbsent(key: K, defaultValue: () => V): boolean {
if (!this.has(key)) {
let value = defaultValue();
this.set(key, value);
return true;
}
return false;
}
keys(): IterableIterator<K> {
return this.keyMap.values();
}
keyList(): K[] {
return [...this.keys()];
}
delete(key: K): boolean {
let keyString = this.stringifyKey(key);
let flag = this.map.delete(keyString);
this.keyMap.delete(keyString);
return flag;
}
clear(): void {
this.map.clear();
this.keyMap.clear();
}
size(): number {
return this.map.size;
}
/**
* Turns the `key` object to a primitive `string` for the underlying `Map`
* @param key key to be stringified
*/
protected abstract stringifyKey(key: K): string;
}
उदाहरण कार्यान्वयन तो यह सरल है: बस stringifyKey
विधि को ओवरराइड करें । मेरे मामले में मैं कुछ uri
संपत्ति को सख्त करता हूं ।
class MyMap extends StringifyingMap<MyKey, MyValue> {
protected stringifyKey(key: MyKey): string {
return key.uri.toString();
}
}
उदाहरण का उपयोग तब होता है जैसे कि यह एक नियमित था Map<K, V>
।
const key1 = new MyKey(1);
const value1 = new MyValue(1);
const value2 = new MyValue(2);
const myMap = new MyMap();
myMap.set(key1, value1);
myMap.set(key1, value2); // native Map would put another key/value pair
myMap.size(); // returns 1, not 2
===
। ES6 सेट ऑब्जेक्ट में कोई तुलना विधि नहीं है।.has()
विधि और.add()
विधि केवल बंद यह एक ही वास्तविक वस्तु या एक आदिम के लिए एक ही मूल्य जा रहा है काम करते हैं।