diff --git a/README.md b/README.md index 54cab4b..ec7154c 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ util function to override any other function ## Quick start Several quick start options are available: -* [Download the latest release](https://github.com/massive-angular/override-fn/archive/v1.0.7.zip) +* [Download the latest release](https://github.com/massive-angular/override-fn/archive/v1.0.8.zip) * Clone the repo: `git clone https://github.com/massive-angular/override-fn.git` * Install with [bower](http://bower.io): `bower install override-fn` * Install with [npm](https://npmjs.com): `npm install override-fn` diff --git a/bower.json b/bower.json index 900fc97..a5365d8 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "override-fn", - "version": "1.0.7", + "version": "1.0.8", "description": "util function to override any other function", "main": "index.js", "authors": [ diff --git a/index.js b/index.js index 7e2e563..3d49150 100644 --- a/index.js +++ b/index.js @@ -1,50 +1,65 @@ function overrideFn(context, fnName, fn) { - var baseFn = context[fnName] || function () {}; + if (typeof fnName === 'string') { + return overrideFnInternal(context, fnName, fn); + } else { + var obj = arguments[1], + keys = Object.keys(obj); - context[fnName] = function overrideFunction() { - var args = arguments, - params = Array.prototype.slice.call(args), - isCalledLikeConstructor = this instanceof overrideFunction; + return keys.reduce(function (result, key) { + result[key] = overrideFnInternal(context, key, obj[key]); - params.unshift(function () { - var _args = arguments.length ? arguments : args, - _params = Array.prototype.slice.call(_args); + return result; + }, {}); + } - if (isCalledLikeConstructor) { - _params.unshift(this); + function overrideFnInternal(context, fnName, fn) { + var baseFn = context[fnName] || function () {}; - return new (Function.prototype.bind.apply(baseFn, _params)); - } + context[fnName] = function overrideFunction() { + var args = arguments, + params = Array.prototype.slice.call(args), + isCalledLikeConstructor = this instanceof overrideFunction; - return baseFn.apply(this, _params); - }.bind(this)); + params.unshift(function () { + var _args = arguments.length ? arguments : args, + _params = Array.prototype.slice.call(_args); - return fn.apply(this, params); - }; + if (isCalledLikeConstructor) { + _params.unshift(this); - try { - Object.defineProperties(context[fnName], { - length: { - get: function () { - return baseFn.length; + return new (Function.prototype.bind.apply(baseFn, _params)); } - }, - name: { - get: function () { - return baseFn.name; + + return baseFn.apply(this, _params); + }.bind(this)); + + return fn.apply(this, params); + }; + + try { + Object.defineProperties(context[fnName], { + length: { + get: function () { + return baseFn.length; + } + }, + name: { + get: function () { + return baseFn.name; + } } - } - }); - } - catch (ex) { - console.warn(ex); - } + }); + } + catch (ex) { + console.warn(ex); + } - context[fnName].toString = function () { - return baseFn.toString(); - }; + context[fnName].toString = function () { + return baseFn.toString(); + }; - return baseFn; + return baseFn; + } } if (typeof module === 'object' && typeof module.exports === 'object') { diff --git a/package.json b/package.json index 92e0fdf..c02a41b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "override-fn", - "version": "1.0.7", + "version": "1.0.8", "description": "util function to override any other function", "main": "index.js", "contributors": [