UNPKG

stepdad

Version:

Stupid simple and lightweight dependency injection

43 lines (33 loc) 1.23 kB
export const Param: any = (desc: string) => { return (target: any, name: "execute", index: number) => { let constructor = target.constructor; let definitions: any[] = (constructor.paramDefinitions = []); let type = Reflect.getMetadata("design:paramtypes", target, name); let name_fn = target[name] && getFunctionParameterName(target[name], index); console.log("desc: ", desc); console.log("type: ", type); //console.log("name: ", getFunctionParameterName(target[name], index)); console.log("definitions: ", definitions); definitions[index] = { type, index, name, name_fn, }; }; }; export function parseFunctionParameterNames( fn: Function ): string[] | undefined { let groups = fn.toString().match(/^[^{=]*\(([\w\d$-,\s]*)\)/); return groups ? groups[1].trim().split(/\s*,\s*/) : undefined; } const getFunctionParameterName = (fn: Function, index: number): string => { let paramNames: string[] | undefined; if ((fn as any).__paramNames) { paramNames = (fn as any).__paramNames; } else { paramNames = (fn as any).__paramNames = parseFunctionParameterNames(fn); } return (paramNames && paramNames[index]) || `param${index}`; };