Giter Club home page Giter Club logo

quark-decorators's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.