collection相关的函数, collection指的是一组用于处理集合(如数组或对象)的工具函数。
lodash源码研读之every,some,size,includes
every函数基于baseEvery和arrayEvery,some函数基于baseSome和arraySome。结构框图省略。
下面依次介绍各个模块。
1.baseEvery和arrayEvery模块
function baseEvery(collection, predicate) {
var result = true;
baseEach(collection, function(value, index, collection) {
result = !!predicate(value, index, collection);
return result;
});
return result;
}
function arrayEvery(array, predicate) {
var index = -1,
length = array == null ? 0 : array.length;
while (++index < length) {
if (!predicate(array[index], index, array)) {
return false;
}
}
return true;
}
baseEvery 函数通过 baseEach 遍历集合中的每个元素,并对每个元素执行 predicate 函数,检查是否所有元素都满足条件。
arrayEvery 函数专门为数组设计,通过 while 循环遍历数组中的每个元素,并对每个元素执行 predicate 函数,检查是否所有元素都满足条件。
所有条件都满足返回true,有一个不满足则返回false。
2.every函数
function every(collection, predicate, guard) {
var func = isArray(collection) ? arrayEvery : baseEvery;
if (guard && isIterateeCall(collection, predicate, guard)) {
predicate = undefined;
}
return func(collection, getIteratee(predicate, 3));
}
//例子
const items = [1, 2, 3, 4];
const allEven = _.every(items, (item) => item % 2 === 0);
console.log(allEven); // 输出: false
every函数的主要功能是检查集合中的每个元素是否满足指定的条件。
3.baseSome和arraySome模块
function baseSome(collection, predicate) {
var result;
baseEach(collection, function(value, index, collection) {
result = predicate(value, index, collection);
return !result;
});
return !!result;
}
function arraySome(array, predicate) {
var index = -1,
length = array == null ? 0 : array.length;
while (++index < length) {
if (predicate(array[index], index, array)) {
return true;
}
}
return false;
}
baseSome 函数通过 baseEach 遍历集合中的每个元素,并对每个元素执行 predicate 函数,检查是否有任何元素满足条件。
arraySome 函数专门为数组设计,通过 while 循环遍历数组中的每个元素,并对每个元素执行 predicate 函数,检查是否有任何元素满足条件。
有元素满足条件即返回true,若都不满足返回false。
4.some函数
function some(collection, predicate, guard) {
var func = isArray(collection) ? arraySome : baseSome;
if (guard && isIterateeCall(collection, predicate, guard)) {
predicate = undefined;
}
return func(collection, getIteratee(predicate, 3));
}
//例子
const items = [1, 2, 3, 4];
const anyEven = _.some(items, (item) => item % 2 === 0);
console.log(anyEven); // 输出: true
some函数的主要功能是检查集合中是否至少有一个元素满足指定的条件。
5.size函数
function size(collection) {
if (collection == null) {
return 0;
}
if (isArrayLike(collection)) {
return isString(collection) ? stringSize(collection) : collection.length;
}
var tag = getTag(collection);
if (tag == mapTag || tag == setTag) {
return collection.size;
}
return baseKeys(collection).length;
}
size 函数的主要功能是获取集合的大小。它通过以下步骤实现:
6.includes函数
function includes(collection, value, fromIndex, guard) {
collection = isArrayLike(collection) ? collection : values(collection);
fromIndex = (fromIndex && !guard) ? toInteger(fromIndex) : 0;
var length = collection.length;
if (fromIndex < 0) {
fromIndex = nativeMax(length + fromIndex, 0);
}
return isString(collection) ?
(fromIndex <= length && collection.indexOf(value, fromIndex) > -1) :
(!!length && baseIndexOf(collection, value, fromIndex) > -1);
}
//例子
_.includes([1, 2, 3], 1, 2);//从索引2开始查找数组是否包含1
// => false
includes 函数的主要功能是检查集合中是否包含指定的值,并可以从指定的索引位置开始查找。它通过以下步骤实现:
_.every 用于检查所有元素是否满足条件,返回 true 或 false。
_.some 用于检查是否有至少一个元素满足条件,返回 true 或 false。
_.size 用于获取集合的大小或字符串的长度,返回一个数值。
_.includes 用于检查集合中是否包含某个值,返回 true 或 false。