Simple decorators (ES2016) based on Decorator design pattern.
This package is part of quark
framework but it can be used independently.
npm install quark-decorators --save
Note : In order to use decorators properly, you need a compiler like Babel 6.
Also, you need to install Babel legacy decorator plugin :
npm install babel-plugin-transform-decorators-legacy --save-dev
And add the following line to your Babel configuration :
{
"plugins": ["transform-decorators-legacy"]
}
Method only
Binds a class method to the current context.
import { bind } from 'quark-decorators'
class Test {
@bind
test () {
return this
}
}
const testInstance = new Test()
const { test } = testInstance
console.log(test() === testInstance) // = true
Class only
Mixes object(s) with a class prototype.
import { mixin } from 'quark-decorators'
const TestMixin = {
test() {
return true
}
}
@mixin(TestMixin)
class Test { }
const testInstance = new Test()
console.log(testInstance.test()) // = true
import { mixin } from 'quark-decorators'
const Test1Mixin = {
test() {
return true
}
}
const Test2Mixin = {
test() {
return false
}
}
@mixin(Test1Mixin, Test2Mixin)
class Test { }
const testInstance = new Test()
console.log(testInstance.test()) // = false (last mixin method value)
import { mixin } from 'quark-decorators'
const TestMixin = {
foo: 'bar',
foo() {
return 'bar'
}
}
@mixin(TestMixin)
class Test { }
const testInstance = new Test()
console.log(testInstance.foo) // = 'bar'
console.log(testInstance.foo()) // = 'bar'
See https://fm-ph.github.io/quark-decorators/
To build the sources with babel
in ./lib
directory :
npm run build
To generate the JSDoc
:
npm run docs
To generate the documentation and deploy on gh-pages
branch :
npm run docs:deploy
To run the tests, first clone the repository and install its dependencies :
git clone https://github.com/fm_ph/quark-decorators.git
cd quark-decorators
npm install
Then, run the tests :
npm test
To watch (test-driven development) :
npm run test:watch
For coverage :
npm run test:coverage
MIT License © Patrick Heng Fabien Motte