Skip to content

Instantly share code, notes, and snippets.

@mohanramphp
Last active December 22, 2018 05:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohanramphp/1596679b304ec92a8ce873bf5b7455ed to your computer and use it in GitHub Desktop.
Save mohanramphp/1596679b304ec92a8ce873bf5b7455ed to your computer and use it in GitHub Desktop.
Log Decorator
import { logClass } from './class-decorator';
import { logMethod } from './method-decorator';
import { logProperty } from './property-decorator';
import { logParameter } from './parameter-decorator';
// decorator factory - which calls the corresponding decorators based on arguments passed
export function log(...args) {
switch (args.length) {
case 3: // can be method or parameter decorator
if (typeof args[2] === "number") { // if 3rd argument is number then its index so its parameter decorator
return logParameter.apply(this, args);
}
return logMethod.apply(this, args);
case 2: // property decorator
return logProperty.apply(this, args);
case 1: // class decorator
return logClass.apply(this, args);
default: // invalid size of arguments
throw new Error('Not a valid decorator');
}
}
@log
class Employee {
@log
private name: string;
constructor(name: string) {
this.name = name;
}
@log
greet(@log message: string): string {
return `${this.name} says: ${message}`;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment