Last active
September 5, 2018 09:47
-
-
Save sergebat/2ee8faf2b561a8b31683acac3d094211 to your computer and use it in GitHub Desktop.
Method call log with experimental Typescript decorators
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
const DEBUG = false; | |
const logDebug = function(_target: any, key: string, descriptor: PropertyDescriptor): any { | |
const originalMethod = descriptor.value; | |
descriptor.value = function(...args: any[]) { | |
const functionName = key; | |
console.log(functionName + "(" + args.join(", ") + ")"); | |
const result = originalMethod.apply(this, args); | |
console.log("=> " + result); | |
return result; | |
}; | |
return descriptor; | |
}; | |
const logNoop = function() {}; | |
const log = DEBUG ? logDebug : logNoop; | |
class Test { | |
@log | |
test(a: number, b: number) { | |
} | |
} | |
new Test().test(1, 2); | |
new Test().test(3, 5); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment