Socket
Book a DemoInstallSign in
Socket

decorator-builder

Package Overview
Dependencies
Maintainers
6
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

decorator-builder

Easily create decorators for multiple and flexible purposes

latest
Source
npmnpm
Version
0.2.4
Version published
Weekly downloads
2
Maintainers
6
Weekly downloads
 
Created
Source

Actions Status Actions Status Actions Status Test Coverage Maintainability Packages npm version

Easily create decorators for multiple and flexible purposes

How to install

npm i decorator-builder

How to use it

Just create your new decorator

const MyDecorator = createClassDecorator();

And then, apply it to your class!

@MyDecorator()
class MyClass {}

The generated decorator is also an Iterable, so, you can get access to every decorated class already loaded

for (const item of MyDecorator) {
  // Target is the Class and args the arguments informed into the decorator
  doABarrelRoll(item.target, item.args);
}

You can also create method decorators:

const MyDecorator = createMethodDecorator();
class MyClass {
  @MyDecorator()
  myMethod() {

  }
}

for (const item of MyDecorator) {
  // Target is the Prototype, name is the method name, descriptor the method descriptor, and args the arguments informed into the decorator
  doABarrelRoll(item.target, item.name, item.descriptor, item.args);
}

Property decorators:

const MyDecorator = createPropertyDecorator();
class MyClass {
  @MyDecorator()
  myProperty: string;
}

for (const item of MyDecorator) {
  // Target is the Prototype, name is the property name, and args the arguments informed into the decorator
  doABarrelRoll(item.target, item.name, item.args);
}

And parameters decorators:

const MyDecorator = createParameterDecorator();
class MyClass {
  myMethod(@MyDecorator() arg: string) {}
}

for (const item of MyDecorator) {
  // Target is the Prototype, name is the method name, index the parameter index, and args the arguments informed into the decorator
  doABarrelRoll(item.target, item.name, item.index, item.args);
}

If you want to create a decorator that receives parameters, you can inform it in it's creation through a function template:

// It works with every type of decorator
const MyDecorator = createMethodDecorator<(value: number, name: string) => void>();

You can also inform a callback to be called at every decorator appliance

const MyDecorator = createMethodDecorator((item) => {
  console.log(item.name);
})

This package also provides a helper function if you want to apply some modification into a class. This is useful to apply in the callback of the decorator:

wrapClass(item.target, (self: SomeClassExtended, a: string, b: number) => {
  self.c = a + b;
})

There is a helper function too so you can wrap methods. This function will directly replace a method on the prototype or the instance, whenever is provided:

// Replaces the method prototype
wrapMethod(SomeClass.prototype, 'method', (previousVersion, paramsOfTheMethod) => previousVersion(paramsOfTheMethod) + 2);
// Replaces just the instance method
wrapMethod(instance, 'method', (previousVersion, paramsOfTheMethod) => previousVersion(paramsOfTheMethod) + 3);

License

Licensed under MIT.

Keywords

decorator

FAQs

Package last updated on 26 Sep 2021

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