Created
December 19, 2020 02:58
-
-
Save mfulton26/cde6ed2a23f59965c5172232256841af to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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