Newer
Older
parsetest / utils.js
@Mark Mark on 23 Sep 2020 1 KB Make it professional
(function() {
    // helper to get the name of an enum from one of its valuesObj[k] == value) || "INVALID_ENUM_VALUE";
    Object.defineProperty(Object.prototype, 'keyName', {
        enumerable: false,
        value: function(value)
        {
            return Object.keys(this).find(k => this[k] == value) || "INVALID_ENUM_VALUE";
        }
    });

    // helper to map over an object's keys recursively
    Object.defineProperty(Object.prototype, 'mapKey', {
        enumerable: false,
        value: function(keyName, mapFunc)
        {
            let ret = {};
            for (let k in this)
            {
                if (k == keyName)
                {
                    ret[k] = mapFunc(this[k]);
                }
                else if (typeof(this[k]) == 'object')
                {
                    ret[k] = { ...this[k] }.mapKey(keyName, mapFunc);
                }
                else
                {
                    ret[k] = this[k];
                }
            }
            return ret;
        }
    });

    console.format = v => console.dir(v, { depth: null });
})();