Vote for Espruino features!

Posted on
Page
of 2
Prev
/ 2
  • Here is a semi complete map poly of a older project of mine.

    'use strict';
    
    class Map
    {
        constructor (keyVal)
        {
            this.clear();
    
            if (Array.isArray(keyVal)) {
                keyVal.forEach(kv => this._keys.push(kv[0]) && this._values.push(kv[1]));
            }
        }
    
        get size ()
        {
            return this._keys.length;
        }
    
        has (key)
        {
            return this._keys.includes(key);
        }
    
        get (key)
        {
            return this._values[this._keys.indexOf(key)];
        }
    
        set (key, value)
        {
            let i = this._keys.indexOf(key);
    
            if (i !== -1) {
                this._values[i] = value;
            } else {
                this._keys.push(key);
                this._values.push(value);
            }
        }
    
        delete (key)
        {
            let i = this._keys.indexOf(key);
    
            if (i === -1) {
                return false;
            }
    
            this._keys.splice(i, 1);
            this._values.splice(i, 1);
        }
    
        keys ()
        {
            return this._keys;
        }
    
        values ()
        {
            return this._values;
        }
    
        clear ()
        {
            this._keys = [];
            this._values = [];
        }
    
        entries ()
        {
            return this._iterator();
        }
    
        _iterator ()
        {
            let i = 0;
    
            return {
                next: () => i < this._keys.length ? {value: [this._keys[i], this._values[i++]], done: false} : {done: true}
            };
        }
    
        forEach (callback, scope = null)
        {
            this._keys.forEach(key => callback.call(scope, this.get(key), key, this));
        }
    }
    

    Amazingly close to what @Gordon already had :D

  • for..of and getters/setters are now part of Espruino: http://forum.espruino.com/conversations/­321225/

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Vote for Espruino features!

Posted by Avatar for Gordon @Gordon

Actions