Comparing version 0.0.2 to 0.0.3
{ | ||
"name": "mixinable", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# Mixinable | ||
<a href="https://travis-ci.org/dmbch/mixinable"> | ||
<img src="https://travis-ci.org/dmbch/mixinable.svg?branch=master"> | ||
</a> | ||
[![travis](https://img.shields.io/travis/dmbch/mixinable.svg)](https://travis-ci.org/dmbch/mixinable) [![npm](https://img.shields.io/npm/v/mixinable.svg)](https://www.npmjs.com/package/mixinable) | ||
<br/> | ||
@@ -31,18 +30,23 @@ `mixinable` is a small utility library allowing you to use [mixins](https://addyosmani.com/resources/essentialjsdesignpatterns/book/#mixinpatternjavascript) in your code. Apart from enabling you to add and override new methods to your prototypes, it helps you apply different strategies to those additional methods. | ||
T.B.D. - to be documented | ||
#### ```mixin.replace([...implementation])``` | ||
T.B.D. - to be documented | ||
`replace` is the default mixin method application strategy. It mimics the behavior of, for example, Backbone's [extend](http://backbonejs.org/#Model-extend) implementation. `replace` accepts any number of functions, i.e. implementations. | ||
#### ```mixin.parallel([...implementation])``` | ||
T.B.D. - to be documented | ||
`parallel` executes all defined implementations in parallel. This is obviously most useful if there are asynchronous implementations involved - otherwise, it behaves identically to `sequence`. | ||
#### ```mixin.pipe([...implementation])``` | ||
T.B.D. - to be documented | ||
`pipe` passes the each implementation's output to the next, using the first argument as the initial value. All other arguments are being passed to all implementations as-is. | ||
#### ```mixin.sequence([...implementation])``` | ||
`sequence` executes all implementation sequentially, passing all arguments unchanged. Use it if your implementations might rely on others changing the instance they are run on. | ||
### Examples | ||
@@ -49,0 +53,0 @@ |
79
test.js
@@ -48,3 +48,3 @@ 'use strict'; | ||
test('contructor override test', function (t) { | ||
t.plan(3); | ||
t.plan(4); | ||
var expectedOptions = {}; | ||
@@ -55,2 +55,3 @@ var Foo = mixin({ | ||
t.equal(options, expectedOptions, 'options are being passed'); | ||
t.equal(typeof this.constructor, 'function', 'constructor method exists'); | ||
t.ok(this instanceof Foo, 'inheritance is set up correctly'); | ||
@@ -64,3 +65,3 @@ } | ||
test('override test', function (t) { | ||
t.plan(4); | ||
t.plan(6); | ||
var Foo = mixin({ | ||
@@ -77,2 +78,3 @@ foo: mixin.override(function () { | ||
t.pass('explicitly overriden method is being called'); | ||
t.equal(typeof this.foo, 'function', 'foo method exists'); | ||
t.ok(this instanceof createBar, 'inheritance is set up correctly'); | ||
@@ -82,2 +84,3 @@ }, | ||
t.pass('implicitly overriden method is being called'); | ||
t.equal(typeof this.bar, 'function', 'bar method exists'); | ||
t.ok(this instanceof createBar, 'inheritance is set up correctly'); | ||
@@ -94,3 +97,3 @@ } | ||
test('parallel test', function (t) { | ||
t.plan(8); | ||
t.plan(12); | ||
var expectedOptions = {}; | ||
@@ -102,2 +105,3 @@ var createFoo = mixin( | ||
t.equal(options, expectedOptions, '1st is being called with options'); | ||
t.equal(typeof this.foo, 'function', 'foo method exists'); | ||
t.ok(this instanceof createFoo, 'inheritance is set up correctly'); | ||
@@ -111,2 +115,3 @@ } | ||
t.equal(options, expectedOptions, '2nd is being called with options'); | ||
t.equal(typeof this.foo, 'function', 'foo method exists'); | ||
t.ok(this instanceof createFoo, 'inheritance is set up correctly'); | ||
@@ -116,2 +121,3 @@ }, | ||
t.equal(options, expectedOptions, '3rd is being called with options'); | ||
t.equal(typeof this.foo, 'function', 'foo method exists'); | ||
t.ok(this instanceof createFoo, 'inheritance is set up correctly'); | ||
@@ -125,2 +131,3 @@ } | ||
t.equal(options, expectedOptions, '4th is being called with options'); | ||
t.equal(typeof this.foo, 'function', 'foo method exists'); | ||
t.ok(this instanceof createFoo, 'inheritance is set up correctly'); | ||
@@ -133,3 +140,3 @@ } | ||
test('async parallel test', function (t) { | ||
t.plan(11); | ||
t.plan(15); | ||
var counter = 0; | ||
@@ -143,2 +150,3 @@ var expectedOptions = {}; | ||
t.equal(options, expectedOptions, '1st is being called with options'); | ||
t.equal(typeof this.foo, 'function', 'foo method exists'); | ||
t.ok(this instanceof createFoo, 'inheritance is set up correctly'); | ||
@@ -155,2 +163,3 @@ return new Promise(function (resolve) { | ||
t.equal(options, expectedOptions, '2nd is being called with options'); | ||
t.equal(typeof this.foo, 'function', 'foo method exists'); | ||
t.ok(this instanceof createFoo, 'inheritance is set up correctly'); | ||
@@ -169,4 +178,6 @@ return new Promise(function (resolve) { | ||
foo: function (options) { | ||
t.pass('sync/async methods can be mixed'); | ||
t.equal(counter, 0, '3rd is being called without waiting'); | ||
t.equal(options, expectedOptions, '3rd is being called with options'); | ||
t.equal(typeof this.foo, 'function', 'foo method exists'); | ||
t.ok(this instanceof createFoo, 'inheritance is set up correctly'); | ||
@@ -189,9 +200,6 @@ return ++counter; | ||
test('pipe test', function (t) { | ||
t.plan(13); | ||
t.plan(17); | ||
var expectedOptions = {}; | ||
var createFoo = mixin( | ||
{ | ||
increment: function (value) { | ||
return ++value; | ||
}, | ||
foo: mixin.pipe( | ||
@@ -201,4 +209,5 @@ function (value, options) { | ||
t.equal(options, expectedOptions, '1st is being called with options'); | ||
t.equal(typeof this.foo, 'function', 'foo method exists'); | ||
t.ok(this instanceof createFoo, 'inheritance is set up correctly'); | ||
return this.increment(value); | ||
return ++value; | ||
} | ||
@@ -212,4 +221,5 @@ ) | ||
t.equal(options, expectedOptions, '2nd is being called with options'); | ||
t.equal(typeof this.foo, 'function', 'foo method exists'); | ||
t.ok(this instanceof createFoo, 'inheritance is set up correctly'); | ||
return this.increment(value); | ||
return ++value; | ||
}, | ||
@@ -219,4 +229,5 @@ function (value, options) { | ||
t.equal(options, expectedOptions, '3rd is being called with options'); | ||
t.equal(typeof this.foo, 'function', 'foo method exists'); | ||
t.ok(this instanceof createFoo, 'inheritance is set up correctly'); | ||
return this.increment(value); | ||
return ++value; | ||
} | ||
@@ -230,4 +241,5 @@ ] | ||
t.equal(options, expectedOptions, '4th is being called with options'); | ||
t.equal(typeof this.foo, 'function', 'foo method exists'); | ||
t.ok(this instanceof createFoo, 'inheritance is set up correctly'); | ||
return this.increment(value); | ||
return ++value; | ||
} | ||
@@ -241,9 +253,6 @@ }); | ||
test('async pipe test', function (t) { | ||
t.plan(11); | ||
t.plan(15); | ||
var expectedOptions = {}; | ||
var createFoo = mixin( | ||
{ | ||
increment: function (value) { | ||
return ++value; | ||
}, | ||
foo: mixin.pipe( | ||
@@ -253,6 +262,7 @@ function (value, options) { | ||
t.equal(options, expectedOptions, '1st is being called with options'); | ||
t.equal(typeof this.foo, 'function', 'foo method exists'); | ||
t.ok(this instanceof createFoo, 'inheritance is set up correctly'); | ||
return new Promise(function (resolve) { | ||
setTimeout(resolve.bind(null, this.increment(value)), 1); | ||
}.bind(this)); | ||
setTimeout(resolve.bind(null, ++value), 1); | ||
}); | ||
}, | ||
@@ -262,6 +272,7 @@ function (value, options) { | ||
t.equal(options, expectedOptions, '2nd is being called with options'); | ||
t.equal(typeof this.foo, 'function', 'foo method exists'); | ||
t.ok(this instanceof createFoo, 'inheritance is set up correctly'); | ||
return new Promise(function (resolve) { | ||
setTimeout(resolve.bind(null, this.increment(value)), 1); | ||
}.bind(this)); | ||
setTimeout(resolve.bind(null, ++value), 1); | ||
}); | ||
} | ||
@@ -273,6 +284,8 @@ ) | ||
foo: function (value, options) { | ||
t.pass('sync/async methods can be mixed'); | ||
t.equal(value, 3, '3rd is being passed updated value'); | ||
t.equal(options, expectedOptions, '3rd is being called with options'); | ||
t.equal(typeof this.foo, 'function', 'foo method exists'); | ||
t.ok(this instanceof createFoo, 'inheritance is set up correctly'); | ||
return this.increment(value); | ||
return ++value; | ||
} | ||
@@ -293,3 +306,3 @@ }); | ||
test('sequence test', function (t) { | ||
t.plan(12); | ||
t.plan(16); | ||
var counter = 0; | ||
@@ -303,2 +316,3 @@ var expectedOptions = {}; | ||
t.equal(options, expectedOptions, '1st is being called with options'); | ||
t.equal(typeof this.foo, 'function', 'foo method exists'); | ||
t.ok(this instanceof createFoo, 'inheritance is set up correctly'); | ||
@@ -313,2 +327,3 @@ } | ||
t.equal(options, expectedOptions, '2nd is being called with options'); | ||
t.equal(typeof this.foo, 'function', 'foo method exists'); | ||
t.ok(this instanceof createFoo, 'inheritance is set up correctly'); | ||
@@ -319,2 +334,3 @@ }, | ||
t.equal(options, expectedOptions, '3rd is being called with options'); | ||
t.equal(typeof this.foo, 'function', 'foo method exists'); | ||
t.ok(this instanceof createFoo, 'inheritance is set up correctly'); | ||
@@ -329,2 +345,3 @@ } | ||
t.equal(options, expectedOptions, '4th is being called with options'); | ||
t.equal(typeof this.foo, 'function', 'foo method exists'); | ||
t.ok(this instanceof createFoo, 'inheritance is set up correctly'); | ||
@@ -337,3 +354,3 @@ } | ||
test('async sequence test', function (t) { | ||
t.plan(8); | ||
t.plan(15); | ||
var counter = 0; | ||
@@ -347,2 +364,3 @@ var expectedOptions = {}; | ||
t.equal(options, expectedOptions, '1st is being called with options'); | ||
t.equal(typeof this.foo, 'function', 'foo method exists'); | ||
t.ok(this instanceof createFoo, 'inheritance is set up correctly'); | ||
@@ -359,2 +377,3 @@ return new Promise(function (resolve) { | ||
t.equal(options, expectedOptions, '2nd is being called with options'); | ||
t.equal(typeof this.foo, 'function', 'foo method exists'); | ||
t.ok(this instanceof createFoo, 'inheritance is set up correctly'); | ||
@@ -370,7 +389,17 @@ return new Promise(function (resolve) { | ||
} | ||
); | ||
) | ||
.mixin({ | ||
foo: function (options) { | ||
t.pass('sync/async methods can be mixed'); | ||
t.equal(counter, 2, '3rd is being called after waiting for first'); | ||
t.equal(options, expectedOptions, '3rd is being called with options'); | ||
t.equal(typeof this.foo, 'function', 'foo method exists'); | ||
t.ok(this instanceof createFoo, 'inheritance is set up correctly'); | ||
return ++counter; | ||
} | ||
}); | ||
var result = createFoo().foo(expectedOptions); | ||
t.ok(result instanceof Promise, 'sequence\'ed method returns a promise'); | ||
result.then(function () { | ||
t.equal(counter, 2, 'promise resolves after everything else'); | ||
t.equal(counter, 3, 'promise resolves after everything else'); | ||
}) | ||
@@ -377,0 +406,0 @@ .catch(function () { |
66931
511
183