quark-decorators
Simple decorators (ES2016) based on Decorator design pattern.
This package is part of quark framework but it can be used independently.
Installation
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"]
}Usage
Bind
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) // = trueMixin
Class only
Mixes object(s) with a class prototype.
Single mixin
import { mixin } from 'quark-decorators'
const TestMixin = {
test() {
return true
}
}
@mixin(TestMixin)
class Test { }
const testInstance = new Test()
console.log(testInstance.test()) // = trueMultiple mixins
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)Mixin with multiple types property
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'API
See https://fm-ph.github.io/quark-decorators/
Build
To build the sources with babel in ./lib directory :
npm run buildDocumentation
To generate the JSDoc :
npm run docsTo generate the documentation and deploy on gh-pages branch :
npm run docs:deployTesting
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 installThen, run the tests :
npm testTo watch (test-driven development) :
npm run test:watchFor coverage :
npm run test:coverageLicense
MIT License © Patrick Heng Fabien Motte
