Skip to content

Commit

Permalink
support multiple overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
adascal committed Jun 29, 2016
1 parent 6f310db commit cea4c5a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 38 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
85 changes: 50 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
@@ -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') {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down

0 comments on commit cea4c5a

Please sign in to comment.