Lodash源码分析-every,some,size,includes

collection相关的函数, collection指的是一组用于处理集合(如数组或对象)的工具函数。

lodash源码研读之every,some,size,includes

一、源码地址

  •  GitHub 地址: GitHub - lodash/lodash: A modern JavaScript utility library delivering modularity, performance, & extras.
  • 官方文档地址: Lodash 官方文档

二、结构分析

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函数的主要功能是检查集合中的每个元素是否满足指定的条件。

  • isArray(collection): 检查 collection 是否为数组。
  • isIterateeCall(collection, predicate, guard): 判断 collection、predicate 和 guard 是否是迭代调用的参数。
  • getIteratee(predicate, 3): 获取 predicate 的迭代器函数,或者默认的迭代器函数。如果 predicate 为 undefined,则返回默认的迭代器函数。

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 函数的主要功能是获取集合的大小。它通过以下步骤实现:

  • 处理 collection 为 null 或 undefined 的情况,直接返回 0。
  • 处理类数组对象(如数组、字符串),返回其长度或使用 stringSize 获取字符串的长度。
  • 处理 Map 和 Set 类型的集合,直接返回其 size 属性。
  • 处理普通对象,使用 baseKeys 获取对象的键数组,并返回其长度。

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 函数的主要功能是检查集合中是否包含指定的值,并可以从指定的索引位置开始查找。它通过以下步骤实现:

  • 标准化 collection,将其转换为数组或字符串。
  • 处理 fromIndex 参数,确保其为非负整数。
  • 根据 collection 的类型,使用不同的方法检查是否包含 value。
  • 返回布尔值,表示 value 是否存在于 collection 中。

四、总结

_.every 用于检查所有元素是否满足条件,返回 true 或 false。

_.some 用于检查是否有至少一个元素满足条件,返回 true 或 false。

_.size 用于获取集合的大小或字符串的长度,返回一个数值。

_.includes 用于检查集合中是否包含某个值,返回 true 或 false。

你可能感兴趣的:(Loadsh源码分析,javascript,前端)