We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
平时项目里常用 licia 或 lodash 的 isEmpty() 来判断一个值是否为「空」。
isEmpty()
除了对象、数组,也常用来判断 null、undefined 等原始值。
对于 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); };
非常简单:
对于原始值,in 操作会隐式转换为包装对象 Object(val),由于它本身没有可枚举属性,所以 Number、Boolean、BigInt 等类型的值 isEmpty() 会返回 true。
Object(val)
lodash 差不多,其支持的类型更丰富一下,比如 ArrayBuffer、TypedArray,但项目一般很少判断这类的,不多说了。https://github.com/lodash/lodash/blob/4.17.15/lodash.js#L11479
The text was updated successfully, but these errors were encountered:
No branches or pull requests
平时项目里常用 licia 或 lodash 的
isEmpty()
来判断一个值是否为「空」。除了对象、数组,也常用来判断 null、undefined 等原始值。
对于 Falsy Value,有时得翻文档确认下:
null
undefined
false
''
0
0n
NaN
结果:
源码:
非常简单:
The text was updated successfully, but these errors were encountered: