refactor: role stores (#2478)

* refactor: reduced duplication in role stores

* fix docs

* fix: typo

* most of requested changes

* rest of changes

* ACTUAL rest of changes

* docs

* doooocs

* reason
This commit is contained in:
Isabella
2018-04-24 15:52:00 -05:00
committed by GitHub
parent e12ab7428f
commit 8b83553462
5 changed files with 183 additions and 144 deletions

View File

@@ -1,4 +1,5 @@
const snekfetch = require('snekfetch');
const Collection = require('./Collection');
const { Colors, DefaultOptions, Endpoints } = require('./Constants');
const { Error: DiscordError, RangeError, TypeError } = require('../errors');
const has = (o, k) => Object.prototype.hasOwnProperty.call(o, k);
@@ -413,6 +414,33 @@ class Util {
setTimeout(resolve, ms);
});
}
/**
* Adds methods from collections and maps onto the provided store
* @param {DataStore} store The store to mixin
* @param {string[]} ignored The properties to ignore
* @private
*/
/* eslint-disable func-names */
static mixin(store, ignored) {
Object.getOwnPropertyNames(Collection.prototype)
.concat(Object.getOwnPropertyNames(Map.prototype)).forEach(prop => {
if (ignored.includes(prop)) return;
if (prop === 'size') {
Object.defineProperty(store.prototype, prop, {
get: function() {
return this._filtered[prop];
},
});
return;
}
const func = Collection.prototype[prop];
if (prop === 'constructor' || typeof func !== 'function') return;
store.prototype[prop] = function(...args) {
return func.apply(this._filtered, ...args);
};
});
}
}
module.exports = Util;