Created
July 27, 2018 18:54
-
-
Save holmberd/1b487a7bf5c47a8a25142c8e0a979315 to your computer and use it in GitHub Desktop.
Simple decorator methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Binds a component to one or more decorator functions. | |
* @param {Object} component | |
* @param {[function]} decorators | |
* @returns {Object} | |
* @throws {Error} | |
*/ | |
function Decorator(component, decorators) { | |
if (!component || !decorators) { | |
throw new Error('Failed to create decorator, arguments invalid.'); | |
} | |
decorators.forEach(function (decorator) { | |
component[decorator.name] = decorator.bind(component); | |
}); | |
return component; | |
} | |
/** | |
* Adds decorator functions to an item. | |
* @private | |
* @returns {ItemDecorator} | |
*/ | |
function ItemDecorator(item) { | |
return Decorator(item, [save, remove, toString]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment