@decorize/bind

1.0.3 • Public • Published

chat build quality coverage dependencies min+gzip typescript

Decorator to bind the method or all methods of the class to the context used to access it.

Install

npm install @decorize/bind --save

Usage

Decorate the method:

import { bind } from '@decorize/bind';

class Example {
  @bind
  public method(): boolean {
    return this instanceof Example;
  }
}

new Example().method.call(null); // True

Decorate the class:

import { bind } from '@decorize/bind';

@bind
class Example {
  public method(): boolean {
    return this instanceof Example;
  }
}

new Example().method.call(null); // True

Typing

export declare function bind(): ClassDecorator & MethodDecorator;
export declare function bind<T extends Function>(target: T): T;
export declare function bind(target: object, property: PropertyKey, descriptor: PropertyDescriptor): PropertyDescriptor;

Feature

  • Support different naming conventions.
    Available in lowercase @bind or capital letter @Bind.

  • Support different coding conventions.
    Applicable directly to the declaration @bind or as the decorator's factory @bind().

  • Support method and class decoration.
    Particular method or class (methods on the constructor's prototype) can be decorated with automated binding to the context on the fly with subsequent caching.

  • Cache of own and inherited bound methods.
    Bound methods are added to context-dependent cache to avoid unnecessary bindings and increase performance.

  • Polyfill free, TypeScript and ES5 compatibility.
    There is no need for any polyfill and can be fully used with TypeScript (d.ts) and ES5.

  • Intelligent and backward compatible (ES5 vs ES2015+).
    Ensures correct use of the decorator and verifies whether the method can be decorated by checking the attributes of the descriptor. Methods derived from the prototype or by using the super will not be bound.

  • Advanced decoration and synergy with other decorators.
    Logic respects the original method and other decorators, so all the attributes of the descriptor not related to this decorator will be kept or adapted.

  • Extensive source documentation and testing coverage.
    Source code is fully documented and tested for each line.

Future

The package includes an implementation of the decorator using the TypeScript syntax and will be extended in future with the new proposal from TC39.

We are actively supporting the package, so please contact us at GitHub or Gitter if you have any suggestions or questions.

Package Sidebar

Install

npm i @decorize/bind

Weekly Downloads

49

Version

1.0.3

License

MIT

Unpacked Size

21 kB

Total Files

16

Last publish

Collaborators

  • decorize