1 Star 0 Fork 0

UnPourTous/lodash

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
size.js 1.05 KB
一键复制 编辑 原始数据 按行查看 历史
John-David Dalton 提交于 2017-04-17 04:57 +08:00 . Remove baseKeys and baseKeysIn.
import getTag from './.internal/getTag.js'
import isArrayLike from './isArrayLike.js'
import isString from './isString.js'
import stringSize from './.internal/stringSize.js'
/** `Object#toString` result references. */
const mapTag = '[object Map]'
const setTag = '[object Set]'
/**
* Gets the size of `collection` by returning its length for array-like
* values or the number of own enumerable string keyed properties for objects.
*
* @since 0.1.0
* @category Collection
* @param {Array|Object|string} collection The collection to inspect.
* @returns {number} Returns the collection size.
* @example
*
* size([1, 2, 3])
* // => 3
*
* size({ 'a': 1, 'b': 2 })
* // => 2
*
* size('pebbles')
* // => 7
*/
function size(collection) {
if (collection == null) {
return 0
}
if (isArrayLike(collection)) {
return isString(collection) ? stringSize(collection) : collection.length
}
const tag = getTag(collection)
if (tag == mapTag || tag == setTag) {
return collection.size
}
return Object.keys(collection).length
}
export default size
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/UnPourTous/lodash.git
git@gitee.com:UnPourTous/lodash.git
UnPourTous
lodash
lodash
master

搜索帮助