乐趣区

关于javascript:Util-JS判断任意类型变量是否为空

// 判断任意类型变量是否为空
export function isNull (val) {
    // null or undefined
    if (val === null || val === undefined) return true;

    if (typeof val === 'string' && val.trim() === "") return true;

    if (typeof val === 'boolean') return false;

    if (typeof val === 'number') return !val;

    if (val instanceof Error) return val.message === '';

    switch (Object.prototype.toString.call(val)) {
        // String or Array
        case '[object String]':
        case '[object Array]':
            return !val.length;

        // Map or Set or File
        case '[object File]':
        case '[object Map]':
        case '[object Set]': {return !val.size;}
        // Plain Object
        case '[object Object]': {return !Object.keys(val).length;
        }
    }

    return false;
}
退出移动版