var-util.ts 1.6 KB
Newer Older
码梦天涯's avatar
码梦天涯 已提交
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
export namespace VarUtil {
    export function is(variable: any, type: string): boolean {
        return typeOf(variable) === type;
    }

    export function typeOf(variable: any): string {
        const tag: string = Object.prototype.toString.call(variable);

        const match: string = tag.match(/(?<=\s)[\W\w]*(?=])/)[0];

        return match.toLowerCase();
    }

    export function isNumber(variable: any): boolean {
        return is(variable, "number");
    }

    export function isString(variable: any): boolean {
        return is(variable, "string");
    }

    export function isBoolean(variable: any): boolean {
        return is(variable, "boolean");
    }

    export function isObject(variable: any): boolean {
        return is(variable, "object");
    }

    export function isArray(variable: any): boolean {
        if (Array.isArray) {
            return Array.isArray(variable);
        }
        return is(variable, "array");
    }

    export function isFunction(variable: any): boolean {
        return is(variable, "function");
    }

    export function isSymbol(variable: any): boolean {
        return is(variable, "symbol");
    }

    export function isRegExp(variable: any): boolean {
        return is(variable, "regexp");
    }

    export function isDate(variable: any): boolean {
        return is(variable, "date");
    }

    export function isError(variable: any): boolean {
        return is(variable, "error");
    }

    export function isUndefined(variable: any): boolean {
        return is(variable, "undefined");
    }

    export function isNull(variable: any): boolean {
        return is(variable, "null");
    }
}