Created
October 10, 2019 17:22
-
-
Save NickStrupat/faa3089b98bd1129ff3e8bcb49c4aa11 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
class MapWrapper implements Map<string, any> { | |
constructor(readonly object: any) {} | |
clear(): void { | |
for (let key of this.keys()) | |
delete this.object[key]; | |
} | |
delete(key: string): boolean { | |
if (!this.has(key)) | |
return false; | |
delete this.object[key]; | |
return true; | |
} | |
forEach(callbackfn: (value: any, key: string, map: Map<string, any>) => void, thisArg?: any): void { | |
for (let entry of this.entries()) | |
callbackfn(entry[1], entry[0], this); | |
} | |
get(key: string) { | |
return this.object[key]; | |
} | |
has(key: string): boolean { | |
return typeof this.object[key] !== 'undefined'; | |
} | |
set(key: string, value: any): this { | |
this.object[key] = value; | |
return this; | |
} | |
get size(): number { | |
return Object.keys(this.object).length; | |
} | |
[Symbol.iterator](): IterableIterator<[string, any]> { | |
return this.entries(); | |
} | |
*entries(): IterableIterator<[string, any]> { | |
const entries = Object.entries<any>(this.object); | |
for (let entry of entries) | |
yield entry; | |
} | |
*keys(): IterableIterator<string> { | |
const keys = Object.keys(this.object); | |
for (let key of keys) | |
yield key; | |
} | |
*values(): IterableIterator<any> { | |
const values = Object.values(this.object); | |
for (let value of values) | |
yield value; | |
} | |
[Symbol.toStringTag]: string; | |
} | |
const x: any = {}; | |
x.wat = 'george'; | |
const wrapper = new MapWrapper(x); | |
assert(wrapper.size === 1); | |
wrapper.set('foo', 42); | |
assert(x.foo === 42); | |
for (let entry of wrapper.entries()) | |
console.log(entry); | |
process.exit(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment