Replace Structures.extend with set

This commit is contained in:
Schuyler Cebulskie
2017-11-20 01:11:19 -05:00
parent a2a4c3c196
commit 1e0ee2f8fa
2 changed files with 15 additions and 36 deletions

View File

@@ -10,7 +10,7 @@ class DataStore extends Collection {
super();
if (!Structures) Structures = require('../util/Structures');
Object.defineProperty(this, 'client', { value: client });
Object.defineProperty(this, 'holds', { value: Structures.get(holds) });
Object.defineProperty(this, 'holds', { value: Structures.get(holds.name) });
if (iterable) for (const item of iterable) this.create(item);
}

View File

@@ -8,51 +8,30 @@ class Structures {
/**
* Retrieves a structure class.
* @param {string|Function} structure Name of the structure or a class/prototype function to use the name of
* @param {string} structure Name of the structure to retrieve
* @returns {Function}
*/
static get(structure) {
if (typeof structure === 'string') return structures[structure];
if (typeof structure === 'function') return structures[structure.name];
throw new TypeError(`Structure to retrieve must be a string or class/prototype function, not ${typeof structure}.`);
throw new TypeError(`"structure" argument must be a string (received ${typeof structure})`);
}
/**
* Extends a structure.
* @param {string} name Name of the structure class to extend
* @param {Function} extender Function that takes the base class to extend as its only parameter and returns the
* extended class/prototype
* @returns {Function} Extended class/prototype returned from the extender
* @example
* const { Structures } = require('discord.js');
*
* Structures.extend('Guild', Guild =>
* class CoolGuild extends Guild {
* constructor(client, data) {
* super(client, data);
* this.cool = true;
* }
* }
* );
* Replaces a structure class with an extended one.
* @param {string} structure Name of the structure to replace
* @param {Function} extended Extended structure class/prototype function to replace with
*/
static extend(name, extender) {
if (!structures[name]) throw new RangeError(`"${name}" is not a valid extensible structure.`);
if (typeof extender !== 'function') {
throw new TypeError('The extender must be a function that returns the extended structure class/prototype.');
}
const custom = extender(structures[name]);
if (typeof custom !== 'function') {
throw new TypeError('The extender function must return the extended structure class/prototype.');
}
if (Object.getPrototypeOf(custom) !== structures[name]) {
throw new Error(
"The class/prototype returned from the extender function doesn't extend the existing structure class/prototype."
static set(structure, extended) {
if (!structures[structure]) throw new RangeError(`"${structure}" is not a valid extensible structure.`);
if (typeof extended !== 'function') {
throw new TypeError(
`"extended" argument must be a structure class/prototype function (received ${typeof extended})`
);
}
structures[name] = custom;
return custom;
if (Object.getPrototypeOf(extended) !== structures[structure]) {
throw new Error('The class/prototype function provided doesn\'t extend the existing structure class/prototype.');
}
structures[structure] = extended;
}
}