1.0.10 • Published 9 years ago

override-fn v1.0.10

Weekly downloads
37
License
MIT
Repository
github
Last release
9 years ago

override-fn

util function to override any other function

Quick start

Several quick start options are available:

  • Download the latest release
  • Clone the repo: git clone https://github.com/massive-angular/override-fn.git
  • Install with bower: bower install override-fn
  • Install with npm: npm install override-fn

Examples

Use original parameters

var overrideFn = require('override-fn');

var context = {
    fn: function (a, b, c) {
        console.log('hello from fn: ', a, b, c);
    }
};

var originalFn = overrideFn(context, 'fn', function (baseFn) {
    console.log('hello from override-fn');
    baseFn();
});

context.fn(1, 2, 3);
// output:
// hello from override-fn
// hello from fn: 1, 2, 3

originalFn(1, 2, 3);
// output:
// hello from fn: 1, 2, 3

Use custom parameters

var overrideFn = require('override-fn');

var context = {
    fn: function (a, b, c) {
        console.log('hello from fn: ', a, b, c);
    }
};

var originalFn = overrideFn(context, 'fn', function (baseFn, a, b, c) {
    console.log('hello from override-fn');
    baseFn(a + 1, b + 1, c + 1);
});

context.fn(1, 2, 3);
// output:
// hello from override-fn
// hello from fn: 2, 3, 4

originalFn(1, 2, 3);
// output:
// hello from fn: 1, 2, 3

Override class

var overrideFn = require('override-fn');

var originalDate = overrideFn(global, 'Date', function (baseFn) {
    console.log('hello from override-fn');
    return baseFn(); // important to return created instance
});

var date = new Date();
console.log(date);
console.log(date instanceof Date);
console.log(date instanceof originalDate);
// output:
// hello from override-fn
// Tue May 24 2016 16:00:54 GMT+0300 (EEST)
// false
// true

Creators

Slava Matvienco

Alexandr Dascal

License

Code released under the MIT license.

1.0.10

9 years ago

1.0.9

9 years ago

1.0.8

9 years ago

1.0.7

10 years ago

1.0.6

10 years ago

1.0.5

10 years ago

1.0.4

10 years ago

1.0.3

10 years ago

1.0.2

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago