Skip to content

dwango-js/advice.js

Repository files navigation

advice.js

Build Status

Simple AOP library for both Node.js and browsers.

function base() {}

// compose a new function which calls a function after base returns
var fn = advice.after(base, function() {
  console.log('base was called');
});


var obj = {
  foo: function() {}
};

// redefine a method which calls a function after obj.foo returns
advice.after(obj, 'foo', function() {
  console.log('obj.foo was called');
});

Installation

Node

$ npm install advice.js

Bower

$ bower install advice

Documentation

advice.before(base, fn) : function

Return compose a new function which calls a fn before base returns.

Order: fn -> base

advice.before(obj, method, fn) : void

Redefine the obj.method which will be called a fn before the original obj.method is called.

Order: fn -> obj.method

advice.after(base, fn) : function

Return compose a new function which calls a fn after base returns.

Order: base -> fn

advice.after(obj, method, fn) : void

Redefine the obj.method which will be called a fn before obj.method is called.

Order: obj.method -> fn

advice.around(base, fn) : function

Return compose a new function which around the base by fn.

function base() {}
var results = [];
var fn = advice.around(base, function(base, value) {
  var result = 'around: ' + value;
  // before
  results.push('before: ' + value);
  // base
  base('base was called');
  // after 
  results.push('after: ' + value);
  return result;
});
fn('foo'); // 'around: foo'
results.join(', ');// 'before: foo, base: bar, after: foo'

advice.around(obj, method, fn) : void

Redefine the obj.method which around obj.method by fn.

Tests

npm test

License

MIT

About

Simple AOP module

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •