Allow multiple methods to be provided to extend and override

This commit is contained in:
David Wheatley 2021-03-14 23:51:25 +00:00
parent 1f1c17628a
commit f3eb142a57

View File

@ -9,16 +9,24 @@
* Care should be taken to extend the correct object in most cases, a class'
* prototype will be the desired target of extension, not the class itself.
*
* @example
* @example <caption>Example usage of extending one method.</caption>
* extend(Discussion.prototype, 'badges', function(badges) {
* // do something with `badges`
* });
*
* @param {Object} object The object that owns the method
* @param {String} method The name of the method to extend
* @example <caption>Example usage of extending multiple methods.</caption>
* extend(IndexPage.prototype, ['oncreate', 'onupdate'], function(vnode) {
* // something that needs to be run on creation and update
* });
*
* @param {object} object The object that owns the method
* @param {string|string[]} methods The name or names of the method(s) to extend
* @param {function} callback A callback which mutates the method's output
*/
export function extend(object, method, callback) {
export function extend(object, methods, callback) {
const allMethods = Array.isArray(methods) ? methods : [methods];
allMethods.forEach((method) => {
const original = object[method];
object[method] = function (...args) {
@ -30,6 +38,7 @@ export function extend(object, method, callback) {
};
Object.assign(object[method], original);
});
}
/**
@ -37,24 +46,32 @@ export function extend(object, method, callback) {
* new function will be run every time the object's method is called.
*
* The replacement function accepts the original method as its first argument,
* which is like a call to 'super'. Any arguments passed to the original method
* which is like a call to `super`. Any arguments passed to the original method
* are also passed to the replacement.
*
* Care should be taken to extend the correct object in most cases, a class'
* prototype will be the desired target of extension, not the class itself.
*
* @example
* @example <caption>Example usage of overriding one method.</caption>
* override(Discussion.prototype, 'badges', function(original) {
* const badges = original();
* // do something with badges
* return badges;
* });
*
* @param {Object} object The object that owns the method
* @param {String} method The name of the method to override
* @example <caption>Example usage of overriding multiple methods.</caption>
* extend(Discussion.prototype, ['oncreate', 'onupdate'], function(original, vnode) {
* // something that needs to be run on creation and update
* });
*
* @param {object} object The object that owns the method
* @param {string|string[]} method The name or names of the method(s) to override
* @param {function} newMethod The method to replace it with
*/
export function override(object, method, newMethod) {
export function override(object, methods, newMethod) {
const allMethods = Array.isArray(methods) ? methods : [methods];
allMethods.forEach((method) => {
const original = object[method];
object[method] = function (...args) {
@ -62,4 +79,5 @@ export function override(object, method, newMethod) {
};
Object.assign(object[method], original);
});
}