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 --saveNote : 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-devAnd 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) // = trueClass 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()) // = trueimport { 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 buildTo generate the JSDoc :
npm run docsTo generate the documentation and deploy on gh-pages branch :
npm run docs:deployTo 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 installThen, run the tests :
npm testTo watch (test-driven development) :
npm run test:watchFor coverage :
npm run test:coverageMIT License © Patrick Heng Fabien Motte

