Skip to content

Instantly share code, notes, and snippets.

@holmberd
Created July 27, 2018 18:54
Show Gist options
  • Save holmberd/1b487a7bf5c47a8a25142c8e0a979315 to your computer and use it in GitHub Desktop.
Save holmberd/1b487a7bf5c47a8a25142c8e0a979315 to your computer and use it in GitHub Desktop.
Simple decorator methods
/**
* 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