Socket
Book a DemoInstallSign in
Socket

decorate-with-options

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

decorate-with-options

Create decorator with options and universal signature

1.0.6
latest
Source
npmnpm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

decorator

FAQs

Package last updated on 20 Jun 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.