Skip to content

Instantly share code, notes, and snippets.

@felixfbecker
Last active April 19, 2018 07:05
Show Gist options
  • Save felixfbecker/7751a3b30a5a28a1cb4b8149571c99e8 to your computer and use it in GitHub Desktop.
Save felixfbecker/7751a3b30a5a28a1cb4b8149571c99e8 to your computer and use it in GitHub Desktop.

Revisions

  1. felixfbecker revised this gist Apr 19, 2018. No changes.
  2. felixfbecker revised this gist Apr 19, 2018. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions protomap.ts
    Original 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> {
    const map = Object.create(ProtoMap.prototype)
    map._values = { __proto__: this._values, [key]: value }
    return map
    return new ProtoMap({ __proto__: this._values, [key]: value })
    }

    public [Symbol.iterator](): IterableIterator<[string, V]> {
  3. felixfbecker created this gist Apr 19, 2018.
    62 changes: 62 additions & 0 deletions protomap.ts
    Original 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)
    }
    }
    }