4.0.1 • Published 6 years ago

middle.js v4.0.1

Weekly downloads
6
License
MIT
Repository
github
Last release
6 years ago

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

  1. Download built files from github:
https://github.com/luckylooke/middle/tree/master/dist
  1. Clone via git:
git clone https://github.com/luckylooke/middle.git
  1. 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.'; // enahancing message
    next(consignee, message); // passing data to next middleware or at last to ending method
});

Now when you send again the message

mySuperMsgLib.send('Superman', 'I really love you, Lois Lane');

Superman get message 'I really love you, Lois Lanesend 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(){
    // send implementation
});

TODO:

  • Docs for all usecases
4.0.1

6 years ago

4.0.0

6 years ago

3.2.0

7 years ago

3.1.0

7 years ago

3.0.3

7 years ago

3.0.2

7 years ago

3.0.1

7 years ago

2.0.0

7 years ago

3.0.0

7 years ago

1.0.0

8 years ago