老是记不住 isEmpty

配图源自 Freepik

平时项目里常用 licia 或 lodash 的 isEmpty() 来判断一个值是否为「空」。

除了对象、数组,也常用来判断 null、undefined 等原始值。

  • 字符串、数组、类数组:若 length 为 0 返回 true。
  • 对象:获取本身可枚举的 key(原型上的不算),若 key length 为 0 返回 true。

对于 Falsy Value,有时得翻文档确认下:

  • null
  • undefined
  • false
  • ''
  • 0
  • 0n
  • NaN

结果:

const {isEmpty} = require('licia')

isEmpty(null) // true
isEmpty(undefined) // true
isEmpty(false) // true
isEmpty('') // true
isEmpty(0) // true
isEmpty(0n) // true
isEmpty(NaN) // true

// 以上值 lodash.isEmpty() 结果相同。

对于 Number、Boolean、BigInt 类型的原始值,isEmpty() 均返回 true。

lodash.isEmpty() 还支持判断 Map/Set 类型,其 size 为 0 则返回 true。而 licia.isEmpty() 不支持的,不管 size 是否为 0 均返回 true。

源码:

// https://github.com/liriliri/licia/blob/master/src/isEmpty.js

_('isArrLike isArr isStr isArgs keys');

exports = function(val) {
    if (val == null) return true;

    if (isArrLike(val) && (isArr(val) || isStr(val) || isArgs(val))) {
        return val.length === 0;
    }

    return keys(val).length === 0;
};
// https://github.com/liriliri/licia/blob/master/src/keys.js

_('has');

if (Object.keys && !LICIA_TEST) {
    exports = Object.keys;
} else {
    exports = function(obj) {
        const ret = [];

        for (const key in obj) {
            if (has(obj, key)) ret.push(key);
        }

        return ret;
    };
}
// https://github.com/liriliri/licia/blob/master/src/has.js

const hasOwnProp = Object.prototype.hasOwnProperty;

exports = function(obj, key) {
    return hasOwnProp.call(obj, key);
};

非常简单:

  1. 判断 null、undefined
  2. 判断数组、类数组
  3. 使用 in 操作符,获取其自身可枚举的属性,再判断 key length

对于原始值,in 操作会隐式转换为包装对象 Object(val),由于它本身没有可枚举属性,所以 Number、Boolean、BigInt 等类型的值 isEmpty() 会返回 true。

lodash 差不多,其支持的类型更丰富一下,比如 ArrayBuffer、TypedArray,但项目一般很少判断这类的,不多说了。https://github.com/lodash/lodash/blob/4.17.15/lodash.js#L11479

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容