Mixins with smart merging strategies and errors over silent failure.
Install with one of:
npm install --save smart-mixin
curl 'wzrd.in/standalone/smart-mixin@2' > vendor/smart-mixin.js
Usage:
var mixins = require('smart-mixin');
var mixIntoGameObject = mixins({
render: mixins.ONCE,
onClick: mixins.MANY,
getState: mixins.MANY_MERGED,
getSomething: mixins.MANY_MERGED_LOOSE,
countChickens: mixins.REDUCE_LEFT,
countDucks: mixins.REDUCE_RIGHT,
onKeyPress: function(left, right, key) {
left = left || function(){};
right = right || function(){};
return function(event){
var event = args[0];
if (!event) throw new TypeError(key + ' called without an event object');
var ret = left.apply(this, arguments);
if (event && !event.immediatePropagationIsStopped) {
var ret2 = right.apply(this, arguments);
}
return ret || ret2;
}
}
}, {
unknownFunction: mixins.ONCE,
nonFunctionProperty: "INTERNAL"
});
var mixin = {
getState(foo){
return {bar: foo+1}
}
};
class Duck {
render(){
console.log(this.getState(5));
}
getState(foo){
return {baz: foo - 1}
}
}
mixIntoGameObject(Duck.prototype, mixin);
new Duck().render();
That's it
Nothing too crazy, this was mostly built for use in react-class-mixins, but hopefully
is useful to other people. I'll be adding more test coverage (the mixin.FN apis are fully tested, but not the actual mixin function). Any bug reports will be fixed ASAP.
License
MIT