All files isEmpty.ts

100% Statements 22/22
100% Branches 26/26
100% Functions 2/2
100% Lines 14/14

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 34 35            12x 11x 10x 10x 9x               6x 12x   12x   10x   7x   5x   3x 1x 1x      
/**
 * 判断类型
 * @param {any} input
 * @return { string }
 */
function getType(input: any): string {
    if (input === null) return 'null'
    if (input === undefined) return 'undefined'
    const type = Object.prototype.toString.call(input).slice(8, -1).toLowerCase()
    if (type === 'string' && typeof input === 'object') return 'object'
    else return type
}
 
/**
 * 判断传入数据是否为空字符或对象 ( {} [] Map Set), 返回 Boolean
 * @param {any} input
 * @return { boolean }
 */
const isEmpty = function (input: any): boolean {
    const type = getType(input)
    // null or undefined
    if (type === 'null' || type === 'undefined') return true
    // Array or String
    if (type === 'array' || typeof input === 'string') return !input.length
    // Map or Set
    if (type === 'map' || type === 'set') return !input.size
    // Date or Number/Bigint or Boolean 不管具体是什么都应该为true
    if (['date', 'number', 'bigint', 'boolean'].includes(type)) return false
    // Object
    if (type === 'object' && Object.keys(input).length === 0) return true
    console.warn(`isEmpty暂未兼容 '${type}' 类型, 默认返回false`)
    return false
}
export default isEmpty