-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ramakrishna
committed
Dec 20, 2017
1 parent
9fadf5b
commit 779b611
Showing
2 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
'use strict' | ||
function _callFunc(self, obj, args){ | ||
const fn = typeof obj === 'function' ? obj : obj.function | ||
if(typeof fn !== 'function') { | ||
return | ||
} | ||
let functionArguments = obj.args | ||
const context = obj.ignoreContext ? undefined : self | ||
if(!functionArguments){ | ||
functionArguments = Array.from(args) | ||
} | ||
if(typeof functionArguments === 'object' && functionArguments.constructor === Array) { | ||
fn.apply(context, functionArguments) | ||
} | ||
} | ||
|
||
function overrideFunction(self, functionToReplace, {before, after}) { | ||
const oldFunc = self[functionToReplace] | ||
if(typeof oldFunc !== 'function'){ | ||
throw new Error('Original function not provided') | ||
} | ||
const newFunc = function(...args){ | ||
_callFunc(self, before, args) | ||
oldFunc.apply(self, args) | ||
_callFunc(self, after, args) | ||
} | ||
self[functionToReplace] = newFunc | ||
return newFunc | ||
} | ||
|
||
if(typeof module !== 'undefined' && module.exports){ | ||
module.exports = overrideFunction | ||
} else if (typeof define === 'function' && define.amd) { | ||
define(function () { | ||
return config; | ||
}); | ||
} else { | ||
global = (function () { | ||
return this || (0, eval)('this'); | ||
}()); | ||
global.overrideFunction = overrideFunction | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use strict' | ||
const functionOverride = require('./index') | ||
|
||
const res = { | ||
send: function(...args){ | ||
console.log(`res.send called with arguments -- ${args} -- and context ${JSON.stringify(this)}`) | ||
}, | ||
a: 1 | ||
} | ||
|
||
function before(...args){ | ||
console.log(`before called with arguments -- ${args} -- and context ${JSON.stringify(this)}`) | ||
} | ||
|
||
function after(...args) { | ||
console.log(`after called with arguments -- ${args} -- and context ${JSON.stringify(this)}`) | ||
} | ||
|
||
before = before.bind(this, "before", "fdsfsdfdsfsdfdsfds") | ||
|
||
res.send = functionOverride(res, 'send', {before, after}) | ||
|
||
res.send("dsa", "sfds") |