decorator-builder
TypeScript icon, indicating that this package has built-in type declarations

0.2.4 • Public • Published

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.

Package Sidebar

Install

npm i decorator-builder

Weekly Downloads

27

Version

0.2.4

License

MIT

Unpacked Size

19.2 kB

Total Files

23

Last publish

Collaborators

  • pedrosodre
  • danielcarvalho
  • gustavobeavis
  • paulododt
  • fgabrielsilva
  • danielgaleni
  • farenheith
  • jeocoutinho