Skip to content

Instantly share code, notes, and snippets.

@bodia-uz
Created October 22, 2019 09:48
Show Gist options
  • Save bodia-uz/51036e6f6a2128edbb3bc09c1d367a28 to your computer and use it in GitHub Desktop.
Save bodia-uz/51036e6f6a2128edbb3bc09c1d367a28 to your computer and use it in GitHub Desktop.
withLogger
function mPatch(object, fnProperty, { onBefore, onAfter }) {
const originFn = object[fnProperty];
object[fnProperty] = (...args) => {
if (typeof onBefore === 'function') {
onBefore(...args);
}
const returnValue = originFn(...args);
if (typeof onAfter === 'function') {
onAfter(returnValue);
}
return returnValue;
};
return () => {
object[fnProperty] = originFn;
};
}
function withLogger(object, fnProperty) {
return mPatch(object, fnProperty, {
onBefore: args => console.log(`${fnProperty} args:`, args),
onAfter: returnValue => console.log(`${fnProperty} returnValue`, returnValue)
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment