Comparing version 1.0.8 to 1.0.9
61
index.ts
class DeboratorOptions { | ||
log?: (text: string) => void; | ||
showReturnValues? = false; | ||
showProperties? = false; | ||
} | ||
@@ -84,20 +85,56 @@ | ||
enum FunctionKind { | ||
Normal, | ||
Getter, | ||
Setter | ||
} | ||
const decorateFunction = (c: any, prop: string) => { | ||
const overrideFunction = (fun: Function, kind: FunctionKind): Function => { | ||
return (...args: any[]) => { | ||
let funName = prop; | ||
switch(kind) { | ||
case FunctionKind.Normal: | ||
default: | ||
break; | ||
case FunctionKind.Getter: | ||
funName = "get " + funName; | ||
break; | ||
case FunctionKind.Setter: | ||
funName = "set " + funName; | ||
break; | ||
} | ||
const callMessage = original.name + "." + funName + dumpArgs(args); | ||
log(callMessage); | ||
const returnValue = fun.apply(c, args); | ||
if (options.showReturnValues) { | ||
log(" " + callMessage + " => " + dumpReturnValue(returnValue)); | ||
} | ||
if (kind !== FunctionKind.Setter) { | ||
return returnValue; | ||
} | ||
} | ||
}; | ||
const originalFun = c[prop]; | ||
if (typeof originalFun !== "function") { | ||
return; | ||
} | ||
c[prop] = (...args: any[]) => { | ||
const callMessage = original.name + "." + prop + dumpArgs(args); | ||
log(callMessage); | ||
let getter = c.__lookupGetter__(prop); | ||
let setter = c.__lookupSetter__(prop); | ||
const returnValue = originalFun.apply(c, args); | ||
if (options.showReturnValues) { | ||
log(" " + callMessage + " => " + dumpReturnValue(returnValue)); | ||
if(options.showProperties) { | ||
if (typeof(getter) === "function") { | ||
c.__defineGetter__(prop, overrideFunction(getter, FunctionKind.Getter)); | ||
} | ||
if (typeof(setter) === "function") { | ||
c.__defineSetter__(prop, overrideFunction(setter, FunctionKind.Setter)); | ||
} | ||
} | ||
return returnValue; | ||
}; | ||
} else { | ||
c[prop] = overrideFunction(originalFun, FunctionKind.Normal); | ||
} | ||
}; | ||
@@ -104,0 +141,0 @@ |
{ | ||
"name": "deborator", | ||
"version": "1.0.8", | ||
"version": "1.0.9", | ||
"description": "a TypeScript decorator, to add console.log() to all methods and properties of a class.", | ||
@@ -5,0 +5,0 @@ "main": "deborator.ts", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9521
153