quark-decorators

1.0.5 • Public • Published

quark-decorators

build status stability npm version js-standard-style semantic-release

Simple decorators (ES2016) based on Decorator design pattern.

This package is part of quark framework but it can be used independently.

Installation

NPM

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"]
}

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) // = true

Mixin

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()) // = true

Multiple 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 build

Documentation

To generate the JSDoc :

npm run docs

To generate the documentation and deploy on gh-pages branch :

npm run docs:deploy

Testing

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

License

MIT License © Patrick Heng Fabien Motte

Dependencies (0)

    Dev Dependencies (12)

    Package Sidebar

    Install

    npm i quark-decorators

    Weekly Downloads

    7

    Version

    1.0.5

    License

    MIT

    Last publish

    Collaborators

    • fm_ph