Skip to content

Instantly share code, notes, and snippets.

@mfulton26
Created December 19, 2020 02:58
Show Gist options
  • Save mfulton26/cde6ed2a23f59965c5172232256841af to your computer and use it in GitHub Desktop.
Save mfulton26/cde6ed2a23f59965c5172232256841af to your computer and use it in GitHub Desktop.
export default class HashMap extends Map {
delete(key) {
return super.delete(JSON.stringify(key));
}
forEach(callbackFn) {
super.forEach(([key, value], _, thisArg) =>
callbackFn(value, key, thisArg)
);
}
get(key) {
return super.get(JSON.stringify(key))?.[1];
}
has(key) {
return super.has(JSON.stringify(key));
}
set(key, value) {
return super.set(JSON.stringify(key), [key, value]);
}
*entries() {
yield* super.values();
}
*keys() {
for (const [key] of super.values()) {
yield key;
}
}
*values() {
for (const [, value] of super.values()) {
yield value;
}
}
}
Object.defineProperties(HashMap.prototype, {
[Symbol.species]: {
writable: false,
enumerable: false,
configurable: true,
value: HashMap,
},
[Symbol.toStringTag]: {
writable: false,
enumerable: false,
configurable: true,
value: "HashMap",
},
});
HashMap.prototype[Symbol.iterator] = HashMap.prototype.entries;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment