filter.js
An approach to aspect-oriented programming (AOP) in javascript for both node.js and browser.
There are different use cases where AOP comes in handy, cases like logging, caching, testing, debugging, before/after filters and so on. filter.js
provides an elegant approach to AOP in javascript.
Installation
npm install filter.js
How to use
Filter = require('filter.js');
var f = new Filter(object, 'method', function(args, next) {
var result = next(args);
return result;
});
f.off();
f.on();
f.remove();
Here is an example usage of filter.js to log and cache mysql queries:
var Filter = require('filter.js');
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'user',
password : 'secret',
database : 'mydb'
});
var cache = {};
new Filter(connection, 'query', function(args, next) {
var query = args[0];
var callback = args[1];
var start = Date.now();
args[1] = function(err, rows) {
console.log(query);
console.log('Took ' + (Date.now() - start) + ' ms');
callback(err, rows);
};
return next(args);
});
new Filter(connection, 'query', function(args, next) {
var query = args[0];
var callback = args[1];
if (!query.match(/^SELECT/i)) {
return next(args);
}
if (cache[query]) {
callback(null, cache[query]);
return;
}
args[1] = function(err, rows) {
cache[query] = rows;
callback(err, rows);
};
return next(args);
});
connection.query('SELECT * FROM posts', function(err, rows) {
console.log(rows);
});
License
filter.js is released under the MIT license.
The idea is inspired by li3 filter system.