All files / lib Utils.ts

93.33% Statements 14/15
72.72% Branches 16/22
100% Functions 6/6
92.3% Lines 12/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33    12x   428x     37x       220x 126x 126x       161x 161x 88x     88x     161x       15x      
import { Type } from "./interfaces/IType";
 
export class Utils {
 
    static isClass = (fn: any) => /^\s*class/.test(fn?.toString());
 
    static getRequiredParamLength(constructor: any) {
        return constructor.length || 0;
    }
 
    static getMeta<R>(key: string | symbol, ctr: Type, def: R): R {
        if (!Utils.isClass(ctr)) return def;
        const meta = Reflect.getMetadata(key, ctr.prototype) || {};
        return meta[key] || def;
    }
 
    static logClass(ctr: any, missParamIndex?: number) {
        const constructorArgs = Reflect.getMetadata("design:paramtypes", ctr) || [];
        const classStr = `${ctr.name}(${constructorArgs.map((arg: any, i: number) => {
           Iif (i === missParamIndex) {
               return "?";
           }
           return  arg.name;
           // @ts-ignore
        }).join(", ")}) `;
        return classStr + (missParamIndex !== undefined ? `Please check the ${missParamIndex + 1}. param: ${constructorArgs[missParamIndex]?.name}` : "");
    }
 
    static isPrimitiveCtr(ctr: any) {
        return ctr.name === "String" || ctr.name === "Number";
    }
}