Last active
April 19, 2018 07:05
-
-
Save felixfbecker/7751a3b30a5a28a1cb4b8149571c99e8 to your computer and use it in GitHub Desktop.
Revisions
-
felixfbecker revised this gist
Apr 19, 2018 . No changes.There are no files selected for viewing
-
felixfbecker revised this gist
Apr 19, 2018 . 1 changed file with 1 addition and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -27,9 +27,7 @@ export class ProtoMap<V> { } public with(key: string, value: V): ProtoMap<V> { return new ProtoMap({ __proto__: this._values, [key]: value }) } public [Symbol.iterator](): IterableIterator<[string, V]> { -
felixfbecker created this gist
Apr 19, 2018 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,62 @@ // tslint:disable:forin export class ProtoMap<V> { private _values: any; public [Symbol.toStringTag]: 'ProtoMap' = 'ProtoMap' constructor(init?: Iterable<[string, V]> | { [key: string]: V }) { this._values = { __proto__: null } if (init) { if (typeof (init as any)[Symbol.iterator] === 'function') { for (const [key, value] of init as Iterable<[string, V]>) { this._values[key] = value } } else { this._values = init } } } public get(key: string): V | undefined { return this._values[key] } public has(key: string): boolean { return key in this._values } public with(key: string, value: V): ProtoMap<V> { const map = Object.create(ProtoMap.prototype) map._values = { __proto__: this._values, [key]: value } return map } public [Symbol.iterator](): IterableIterator<[string, V]> { return this.entries() } public *entries(): IterableIterator<[string, V]> { for (const key in this._values) { yield [key, this._values[key]] } } public *values(): IterableIterator<V> { for (const key in this._values) { yield this._values[key] } } public *keys(): IterableIterator<string> { for (const key in this._values) { yield key } } public forEach(callback: (value: V, key: string, map: ProtoMap<V>) => void): void { for (const key in this._values) { callback(this._values[key], key, this) } } }