decorate-with-options

1.0.6 • Public • Published

decorate-with-options

npm License codecov

Installation

npm install decorate-with-options --save

Why?

If your decorator function expects options, you can call it in many different ways(see examples of usage below).

I'm tired of having to remember how exactly I have to call each decorator in my code.

With this module you don't have this problem anymore. Just wrap your decorator once and call it whichever way you want!

Works for decorators and Higher Order Components(HoC).

Usage

1. Define your decorator. It should accept class(or function) as it's first argument and other options(non functions, see here why) as next arguments.

function myDecorator(Component, option1, option2) {
  // ...
}

2. Wrap it in decorate-with-options.

import decorateWithOptions from 'decorate-with-options';
 
export default decorateWithOptions(myDecorator);
 
function myDecorator(Component, option1, option2) {
  // ...
}

3. Use it in your code in any of the following ways.

import myDecorator from './myDecorator';
 
/* As decorator */
 
// Simply apply it
@myDecorator
class Component {}
 
// Call it without options
@myDecorator()
class Component {}
 
// Call it with options
@myDecorator(option1, option2)
class Component {}
 
 
/* As HoC. Works with both classes and functions */
 
// Call it with Component
myDecorator(Component)
 
// Call it with Component and options
myDecorator(Component, option1, option2)
 
// Or the other way around
myDecorator(option1, option2, Component)
 
// Currying is supported
myDecorator(option1, option2)(Component)

Note

How does this module determine that the class or a function is passed in?

With a simple (typeof arg === 'function') check.

So don't pass functions in place of options. Use an object with properties instead. That's the only constraint.

E.g. don't do this:

// Don't do this
const someFunction = () => {};
 
@myDecorator(someFunction);
myDecorator(Component, someFunction);
myDecorator(someFunction, Component);
myDecorator(someFunction)(Component);
 

License

MIT License

Package Sidebar

Install

npm i decorate-with-options

Weekly Downloads

1

Version

1.0.6

License

MIT

Unpacked Size

7.33 kB

Total Files

4

Last publish

Collaborators

  • le0nik