Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Socket
Sign inDemoInstall

filter.js

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

filter.js

An approach to aspect-oriented programming (AOP) in javascript


Version published
Weekly downloads
12
Maintainers
1
Weekly downloads
 
Created
Source

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');

// Every call to object.method will be wrapped by our filter
// Multiple filters can be applied to a single method
var f = new Filter(object, 'method', function(args, next) {
    // do something with the args
    var result = next(args);
    // do something with the result
    return result;
});

// Temporary disable filter
f.off();

// Activate a disabled filter
f.on();

// Premanently remove filter
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 = {};

//Log queries
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);
});

// Cache SELECT queries
new Filter(connection, 'query', function(args, next) {
	var query = args[0];
	var callback = args[1];

	// Cache only SELECT queries
	if (!query.match(/^SELECT/i)) {
		return next(args);
	}

	//Check if we already have cached result in memory
	if (cache[query]) {
		// skip executing the 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.

Keywords

FAQs

Package last updated on 30 Aug 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc