0.1.1 • Published 7 years ago

extra-decorators v0.1.1

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

This is a set of additional decorators I found usefull while developing my projects. More will be added as I discover new ones. Pull requests are welcome.

This is a modular build meaning you can, if you want, only include what you really need. Use require('extra-decorators/mydecorator'). For a full reference, see below.

:warning: Some of these decorators are still experimental and may change in the future!

Reference

:warning: Order matters! Decorators which define the property must go after those that only hook into the property. For example, @alias should go after @onupdate. As a special case, core-decorator's @nonconfigurable should go before the rest.

@autoconstruct(Class)

Marks the property as being able to reconstruct itself using the underlying class and based on what has been given to it.

class Color {

  constructor(name: string) {
    this.name = name
  }

  // ...
}

class Shape {

  @autoconstruct(Color)
  color

  // ...
}

const shape = new Shape()
shape.color = "blue"
shape.color instanceof Color // true
shape.color.name // "blue"

Caveats

TypeScript does not know that the object gets converted to the appropriate type. Therefore, in order to avoid compiler errors, the type of the property has to be set to "any".

@alias(property)

Marks the property as being an alias for another property. The property can be a key referring to another property of the class, or it can be a full path using JavaScipt dot notation, e.g. foo[1].baz.bar.

class Foo {

  baz = {
    bar: 1
  }

  @alias('baz.bar')
  bar: number

}

const foo = new Foo()
foo.bar // gives 1
foo.bar = 3
foo.baz.bar // gives 3

@onupdate(callback)

Plain and simple: runs callback each time the given property gets updated.

this refers to the current instance.

@onaccess(callback)

Runs callback each time the given property gets accessed.

this refers to the current instance.

See Also

License

The MIT License

0.1.1

7 years ago

0.1.0

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago