Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

老是记不住 isEmpty #346

Open
toFrankie opened this issue Aug 7, 2024 · 0 comments
Open

老是记不住 isEmpty #346

toFrankie opened this issue Aug 7, 2024 · 0 comments
Labels
2024 2024 年撰写 JS 与 JavaScript、ECMAScript 相关的文章 前端 与 JavaScript、ECMAScript、Web 前端相关的文章

Comments

@toFrankie
Copy link
Owner

toFrankie commented Aug 7, 2024

配图源自 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

@toFrankie toFrankie added 2024 2024 年撰写 JS 与 JavaScript、ECMAScript 相关的文章 前端 与 JavaScript、ECMAScript、Web 前端相关的文章 labels Aug 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2024 2024 年撰写 JS 与 JavaScript、ECMAScript 相关的文章 前端 与 JavaScript、ECMAScript、Web 前端相关的文章
Projects
None yet
Development

No branches or pull requests

1 participant