All Projects → CocktailJS → cocktail

CocktailJS / cocktail

Licence: MIT license
Traits, Talents & Annotations for NodeJS.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to cocktail

dart sealed
Dart and Flutter sealed class generator and annotations, with match methods and other utilities. There is also super_enum compatible API.
Stars: ✭ 16 (-75.38%)
Mutual labels:  annotations
zf-dependency-injection
Advanced dependency injection for laminas framework
Stars: ✭ 17 (-73.85%)
Mutual labels:  annotations
2vcf
convert 23andme or Ancestry.com raw genotype calls into VCF format, with dbSNP annotations
Stars: ✭ 42 (-35.38%)
Mutual labels:  annotations
aptk
A toolkit project to enable you to build annotation processors more easily
Stars: ✭ 28 (-56.92%)
Mutual labels:  annotations
graphql-metadata
Annotate your graphql schema with lightweight directives
Stars: ✭ 28 (-56.92%)
Mutual labels:  annotations
aspecio
Aspecio, AOP Proxies for OSGi services
Stars: ✭ 14 (-78.46%)
Mutual labels:  annotations
daikon
Common modules shared by Talend applications
Stars: ✭ 14 (-78.46%)
Mutual labels:  properties
soap-typescript
SOAP decorators for creating wsdl's and annotating services to provide metadata for node-soap
Stars: ✭ 20 (-69.23%)
Mutual labels:  annotations
gum
Repository for the Georgetown University Multilayer Corpus (GUM)
Stars: ✭ 71 (+9.23%)
Mutual labels:  annotations
simple-preferences
Android Library to simplify SharedPreferences use with code generation.
Stars: ✭ 48 (-26.15%)
Mutual labels:  annotations
phpunit-injector
Injects services from a PSR-11 dependency injection container to PHPUnit test cases
Stars: ✭ 62 (-4.62%)
Mutual labels:  annotations
kibana-comments-app-plugin
An application plugin to add and visualize comments to your Kibana dashboards
Stars: ✭ 36 (-44.62%)
Mutual labels:  annotations
annotated
Schema generation using annotated entities and mappers
Stars: ✭ 19 (-70.77%)
Mutual labels:  annotations
accelerator-core-js
Accelerator Core provides a simple way to integrate real-time audio/video into your web application using the OpenTok Platform
Stars: ✭ 24 (-63.08%)
Mutual labels:  annotations
api-router
👨 RESTful router for your API in Nette Framework (@nette). Created either directly or via annotation.
Stars: ✭ 18 (-72.31%)
Mutual labels:  annotations
Molecules Dataset Collection
Collection of data sets of molecules for a validation of properties inference
Stars: ✭ 69 (+6.15%)
Mutual labels:  properties
core2
The bare essentials of std::io for use in no_std. Alloc support is optional.
Stars: ✭ 67 (+3.08%)
Mutual labels:  traits
catnip
Static annotations for Kittens for people who don't like to write semiautomatic derivations into companion objects themselves.
Stars: ✭ 38 (-41.54%)
Mutual labels:  annotations
Exact
An open source online platform for collaborative image labeling of almost everything
Stars: ✭ 47 (-27.69%)
Mutual labels:  annotations
geantyref
Advanced generic type reflection library with support for working with AnnotatedTypes (for Java 8+)
Stars: ✭ 73 (+12.31%)
Mutual labels:  annotations

Cocktail JS

Build Status npm version bitHound Score Code Climate

Cocktail is a small but yet powerful library with very simple principles:

  • Reuse code
  • Keep it simple

Reuse code

Cocktail explores three mechanisms to share/reuse/mix code:

  • Extends: OOP inheritance implemented in Javascript.
  • Traits: Traits are composable behavior units that can be added to a Class.
  • Talents: Same idea as Traits but applied to instances of a Class.

Keep it simple

Cocktail has only one public method cocktail.mix() but it relies on annotations to tag some meta-data that describe the mix.

Annotations

Annotations are simple meta-data Cocktail uses to perform some tasks over the given mix. They become part of the process but usually they are not kept in the result of a mix.

	var cocktail = require('cocktail'),
		MyClass  = function(){};

	cocktail.mix(MyClass, {
		'@properties': {
			name: 'default name'
		}
	});

In the example above we created a "Class" named MyClass, and we use the @properties annotation to create the property name and the corresponding setName and getName methods.

As it was mentioned before, annotations are meta-data, which means that they are not part of MyClass or its prototype.

Defining a Class / Module

Using cocktail to define a class is easy and elegant.

var cocktail = require('cocktail');

cocktail.mix({
	'@exports': module,
	'@as': 'class',

	'@properties': {
		name: 'default name'
	},

	constructor: function(name){
		this.setName(name);
	},

	sayHello: function() {
		return 'Hello, my name is ' + this.getName();
	}
});

In this example our class definition uses @exports to tell the mix we want to export the result in the module.exports and @as tells it is a class.

Traits

Traits are Composable Units of Behaviour (You can read more from this paper). Basically, a Trait is a Class, but a special type of Class that has only behaviour (methods) and no state. Traits are an alternative to reuse behaviour in a more predictable manner. They are more robust than Mixins, or Multiple Inheritance since name collisions must be solved by the developer beforehand. If you compose your class with one or more Traits and you have a method defined in more than one place, your program will fail giving no magic rule or any kind of precedence definition.

Enumerable.js

var cocktail = require('cocktail');

cocktail.mix({
	'@exports': module,
	'@as': 'class',

	'@requires': ['getItems'],

	first: function() {
		var items = this.getItems();
		return items[0] || null;
	},

	last: function() {
		var items = this.getItems(),
			l = items.length;
		return items[l-1];
	}

});

The class above is a Trait declaration for an Enumerable functionality. In this case we only defined first and last methods to retrieve the corresponding elements from an array retrieved by getItems methods.

List.js

var cocktail = require('cocktail'),
	Enumerable = require('./Enumerable');

cocktail.mix({
	'@exports': module,
	'@as': 'class',
	'@traits': [Enumerable],

	'@properties': {
		items: undefined
	},

	'@static': {
		/* factory method*/
		create: function(options) {
			var List = this;
			return new List(options);
		}
	},

	constructor: function (options) {
		this.items = options.items || [];
	}
});

The List class uses the Enumerable Trait, the getItems is defined by the @properties annotation.

index.js

var List = require('./List'),
	myArr = ['one', 'two', 'three'],
	myList;

myList = List.create({items: myArr});

console.log(myList.first()); // 'one'
console.log(myList.last());  // 'three'

Talents

Talents are very similar to Traits, in fact a Trait can be applied as a Talent in CocktailJS. The main difference is that a Talent can be applied to an object or module. So we can define a Talent as a Dynamically Composable Unit of Reuse (you can read more from this paper).

Using the Enumerable example, we can use a Trait as a Talent.

index.js

var cocktail = require('cocktail'),
    enumerable = require('./Enumerable'),
	myArr;

myArr = ['one', 'two', 'three'];

cocktail.mix(myArr, {
    '@talents': [enumerable],

	/* glue code for enumerable talent*/
    getItems: function () {
        return this;
    }
});

console.log(myArr.first());  // 'one'
console.log(myArr.last());   // 'three'

We can also create a new Talent to define the getItems method for an Array to retrive the current instance.

ArrayAsItems.js

var cocktail = require('cocktail');

cocktail.mix({
    '@exports': module,
    '@as': 'class',

    getItems: function () {
        return this;
    }
});

And then use it with Enumerable:

var cocktail     = require('cocktail'),
	enumerable   = require('./Enumerable'),
	arrayAsItems = require('./ArrayAsItems');

var myArr = ['one', 'two', 'three'];

cocktail.mix(myArr, { '@talents': [enumerable, arrayAsItems] });

console.log(myArr.first());  // 'one'
console.log(myArr.last());   // 'three'

Getting Started

  • Install the module with: npm install cocktail or add cocktail to your package.json and then npm install
  • Start playing by just adding a var cocktail = require('cocktail') in your file.

Guides

Guides can be found at CocktailJS Guides

Documentation

The latest documentation is published at CocktailJS Documentation

Examples

A Cocktail playground can be found in cocktail recipes repo.

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality.

Running Lint & Tests

Add your unit and/or integration tests and execute

$ npm test

Run unit tests

$npm run unit

Run integration tests

$npm run integration 

Lint your code

$ npm run lint

Before Commiting

Run npm test to check lint and execute tests

$ npm test

Check test code coverage with instanbul

$ npm run coverage

Release History

see CHANGELOG

License

Copyright (c) 2013 - 2016 Maximiliano Fierro
Licensed under the MIT license.

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].