All Projects → CocktailJS → Traits Decorator

CocktailJS / Traits Decorator

Licence: mit
Traits with decorators

Programming Languages

javascript
184084 projects - #8 most used programming language
es7
32 projects

Projects that are alternatives of or similar to Traits Decorator

React Inform
Simple controlled forms with validations in react
Stars: ✭ 81 (-39.1%)
Mutual labels:  decorators
React Render Debugger
🔧 Render debugger for React
Stars: ✭ 103 (-22.56%)
Mutual labels:  decorators
Tsed
📐 Ts.ED is a Node.js and TypeScript framework on top of Express to write your application with TypeScript (or ES6). It provides a lot of decorators and guideline to make your code more readable and less error-prone.
Stars: ✭ 1,941 (+1359.4%)
Mutual labels:  decorators
Lit Element Router
A LitElement Router (1278 bytes gzip)
Stars: ✭ 85 (-36.09%)
Mutual labels:  decorators
Utils Decorators
Decorators for web and node applications
Stars: ✭ 95 (-28.57%)
Mutual labels:  decorators
Angular Cms
An flexiable, extendable, modular, single CMS app based on Angular, Express, MongoDB
Stars: ✭ 109 (-18.05%)
Mutual labels:  decorators
Bottlejs
A powerful dependency injection micro container for JavaScript applications
Stars: ✭ 1,171 (+780.45%)
Mutual labels:  decorators
Typescript Rest Swagger
Swagger tools for typescript-rest
Stars: ✭ 129 (-3.01%)
Mutual labels:  decorators
Css Constructor
💄 CSS constructor for React components
Stars: ✭ 97 (-27.07%)
Mutual labels:  decorators
Python And Oop
Object-Oriented Programming concepts in Python
Stars: ✭ 123 (-7.52%)
Mutual labels:  decorators
Multitasking
Non-blocking Python methods using decorators
Stars: ✭ 87 (-34.59%)
Mutual labels:  decorators
Laravel Likeable
Rate Eloquent models with Likes and Dislikes in Laravel. Development moved to Laravel Love package!
Stars: ✭ 95 (-28.57%)
Mutual labels:  trait
Babel Plugin Mobx Deep Action
Reduces `action` and `runInAction` boilerplates
Stars: ✭ 110 (-17.29%)
Mutual labels:  decorators
Fastify Decorators
Set of Typescript decorators to build Fastify server with controllers, services and hooks
Stars: ✭ 85 (-36.09%)
Mutual labels:  decorators
Ayanami
🍭 A better way to react with state
Stars: ✭ 129 (-3.01%)
Mutual labels:  decorators
Laravel Ownership
Laravel Ownership simplify management of Eloquent model's owner.
Stars: ✭ 71 (-46.62%)
Mutual labels:  trait
Backoff
Python library providing function decorators for configurable backoff and retry
Stars: ✭ 1,670 (+1155.64%)
Mutual labels:  decorators
React Ioc
Hierarchical Dependency Injection with new React 16 Context API
Stars: ✭ 133 (+0%)
Mutual labels:  decorators
Kubevela
The Modern Application Platform.
Stars: ✭ 2,984 (+2143.61%)
Mutual labels:  trait
100 Words Design Patterns Java
GoF Design Patterns, each pattern described with story from real life.
Stars: ✭ 117 (-12.03%)
Mutual labels:  decorators

traits-decorator

npm version Build Status bitHound Score

Experimental library to apply Traits with ES7 decorators.

Install

using npm

npm i -S traits-decorator

using git repository

npm i -S git://github.com/cocktailjs/traits-decorator

API

Decorators

@traits(Trait1, ...TraitN)

Applicable to class definition. It will apply all the given Traits to the class.

@traits(TExample) class MyClass {}

@requires(description1, ...descriptionN)

Applicable to a method defined in a Trait. The decorator does nothing but it serves as a documentation to reflect what method / property the method needs access to.

class TFoo {

    @requires('bar: {String}')
    fooBar() {
        console.log('foo,' + this.bar);
    }
}

Bindings

excludes(Method1, ...MethodN)

Applicable to Trait definition in '@traits'. It will exclude the given method names from the Trait.

@traits(TExample::excludes('foo', 'bar')) 
class MyClass {}

alias(aliases: {})

Applicable to Trait definition in '@traits'. It will alias the method defined in the Trait with the key as the value .

@traits(TExample::alias({baz: 'parentBaz'}))
class MyClass {}

as({alias: {}, excludes: []})

Applicable to Trait definition in '@traits'. It will apply aliases and excluded methods from the Trait

@traits( TExample::as({alias: {baz: 'parentBaz'}, excludes:['foo', 'bar'] }) )
class MyClass {}

Usage

Basically, we have a few Traits (classes) TFirst, TLast and we combine and apply them by using traits decorator:

example.js

'use strict';

import { traits, excludes, alias, requires }  from 'traits-decorator'

class TFirst {

    @requires('collection:[]')
    first() {
        return this.collection[0];
    }

}

class TLast {
    
    @requires('collection:[]')
    last() {
        let collection = this.collection;
        let l = collection.length;
        return collection[l-1];
    }

    justAnother() {}

    foo() {
        console.log('from TLast\'s foo');
    }
}

//composing a Trait with others
@traits( TFirst, TLast::excludes('foo', 'justAnother') )
class TEnum {

    foo() {
        console.log('enum foo')
    }
}

//apply trait TEnum
@traits(TEnum::alias({ foo: 'enumFoo' }) )
class MyClass {

    constructor (collection = []) {
        this.collection = collection
    }
}

let obj = new MyClass([1,2,3])

console.log(obj.first()) // 1

obj.enumFoo() // enum foo

In order to run the example.js we need babel and since we are using some experimental functionality, decorators (@traits) and bindOperator (::) we need to use the --stage 0.

babel-node --stage 0 example.js

Update

@mixins decorator has been removed. If you want to use mixins please use mixins-decorator package.

License MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].