Skip to content

A tiny javascript library to help decorate functions in a prettier way without the @ syntax.

License

Notifications You must be signed in to change notification settings

bonton-connect/decoration-helper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Decoration Helper

This is a absolutely tiny library to help decorate functions or classes in a prettier way in the ugly messy pre-standardized decorator spec era of Javascript.

Example

Features:

  • Natural composition with decorate(...).with(fn)
  • Decorator style composition with apply(...).to(fn)
  • Functional pipe(...)(fn) and compose(...)(fn) included.

Installation

If you're here, you already know.

yarn add decoration-helper

Usage

To achieve this

@inject('app', 'auth')
@withRouter
@observer
function tomato() {
    console.log("tomato");
}

export default tomato;

you write this:

import { apply } from 'decoration-helper';

function tomato() {
    console.log("tomato");
}

export default apply(
    inject('app', 'auth'),
    withRouter,
    observer
).to(tomato);

or alternatively this, if you prefer:

import { decorate } from 'decoration-helper';

function tomato() {
    console.log("tomato");
}

export default decorate(tomato).with(
    observer,
    withRouter,
    inject('app', 'auth')
);

Note: The order is not reversed when using decorate(obj).with(...decorators)

pipe() and compose()

The proper functional approach would be to use pipe() and compose() as described by Eric Elliot. This post has more details about it.

// Analogous to decorate(fn).with(...);

import { pipe } from 'decoration-helper';

function tomato() {
    console.log("tomato");
}

export default pipe(
    observer,
    withRouter,
    inject('app', 'auth')
)(tomato);



// Analogous to apply(...).to(fn);

import { compose } from 'decoration-helper';

function tomato() {
    console.log("tomato");
}

export default compose(
    inject('app', 'auth'),
    withRouter,
    observer  
)(tomato);

License

MIT

About

A tiny javascript library to help decorate functions in a prettier way without the @ syntax.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published