arrow-orm
Version:
API Builder ORM
30 lines (28 loc) • 794 B
JavaScript
function createWrappedFunction(instance, name) {
if (!instance[name]) {
return;
}
var fn = instance['wrapped' + name];
// only wrap once, if wrapped, return fn
if (fn) {
return instance[name];
}
instance['wrapped' + name] = instance[name];
instance[name] = function () {
// if we don't have a context, use the instance and re-invoke
if (!this) {
return instance[name].apply(instance, arguments);
}
// if we aren't invoking with an instance of our class, re-invoke
if (!(this instanceof Object.getPrototypeOf(instance).constructor)) {
return instance[name].apply(instance, arguments);
}
var args = arguments;
var target = instance['wrapped' + name];
return target.apply(this, args);
};
return instance[name];
}
module.exports = {
createWrappedFunction
};