middle.js
a micro-library for quick implementation of "middleware" support into any project.
By "middleware" I mean something between Express.js middleware pattern and event handling in javascript.
This tool will help you implement open/closed principle from SOLID
By micro-library I mean really micro, it has only 1.6kB minified non-gzipped.
Install
You have several options to install middle.js
- Download built files from github:
https://github.com/luckylooke/middle/tree/master/dist
- Clone via git:
git clone https://github.com/luckylooke/middle.git
- Install via npm:
npm install middle.js
Usage
Let say you want users of your library/system/... to be able to use middleware on your public methods. For example imagine you have super cool message library with method send(consignee, message).
mySuperMsgLib.send('Superman', 'I love you, Lois Lane');
Superman get message 'I love you, Lois Lane'. Lib user enhance this by adding footer like this
mySuperMsgLib.send.use(function addFooter(next, consignee, message){
message += '<br>send by Daily Planet message system.';
next(consignee, message);
});
Now when you send again the message
mySuperMsgLib.send('Superman', 'I really love you, Lois Lane');
Superman get message 'I really love you, Lois Lane
send by Daily Planet message system.'.
To add this functionality to your method for one instace of class, you need to do this:
var mySuperMsgLib = new MySuperMsgLib();
mySuperMsgLib.send = new Middle(MySuperMsgLib.prototype.send, mySuperMsgLib);
For all instances, apply on prototype:
MySuperMsgLib.prototype.send = new Middle(function(){
});
TODO: