lodash源码研读之filter,find,findLast,reject,partition
一、源码地址
- GitHub 地址: GitHub - lodash/lodash: A modern JavaScript utility library delivering modularity, performance, & extras.
- 官方文档地址: Lodash 官方文档
二、结构分析
结构框图省略。
三、函数介绍
下面依次介绍各个模块。
1.filter函数
function arrayFilter(array, predicate) {var index = -1,length = array == null ? 0 : array.length,resIndex = 0,result = [];while (++index < length) {var value = array[index];if (predicate(value, index, array)) {result[resIndex++] = value;}}return result;}function baseFilter(collection, predicate) {var result = [];baseEach(collection, function(value, index, collection) {if (predicate(value, index, collection)) {result.push(value);}});return result;}function filter(collection, predicate) {var func = isArray(collection) ? arrayFilter : baseFilter;return func(collection, getIteratee(predicate, 3));}
filter函数用于从集合(如数组或对象)中筛选出满足指定 predicate 函数的元素,并返回一个新的数组。
- arrayFilter:专门用于数组过滤,通过索引遍历数组,效率较高。
- baseFilter:通用过滤函数,适用于各种集合类型,通过 baseEach 遍历集合。
- filter:对外公开的方法,根据集合类型选择合适的过滤函数,并处理 predicate。
2.reject函数
function negate(predicate) {if (typeof predicate != 'function') {throw new TypeError(FUNC_ERROR_TEXT);}return function() {var args = arguments;switch (args.length) {case 0:return !predicate.call(this);case 1:return !predicate.call(this, args[0]);case 2:return !predicate.call(this, args[0], args[1]);case 3:return !predicate.call(this, args[0], args[1], args[2]);}return !predicate.apply(this, args);};}function reject(collection, predicate) {var func = isArray(collection) ? arrayFilter : baseFilter;return func(collection, negate(getIteratee(predicate, 3)));}
reject函数用于筛选数组或集合中不满足指定 predicate 函数的元素。
使用 negate 函数对 predicate 进行取反处理,这样 arrayFilter 或 baseFilter 就会筛选出不满足原 predicate 的元素。
3.find函数
function baseFindIndex(array, predicate, fromIndex, fromRight) {var length = array.length,index = fromIndex + (fromRight ? 1 : -1);while ((fromRight ? index-- : ++index < length)) {if (predicate(array[index], index, array)) {return index;}}return -1;}function findIndex(array, predicate, fromIndex) {var length = array == null ? 0 : array.length;if (!length) {return -1;}var index = fromIndex == null ? 0 : toInteger(fromIndex);if (index < 0) {index = nativeMax(length + index, 0);}return baseFindIndex(array, getIteratee(predicate, 3), index);}var find = createFind(findIndex);
find函数用于在数组中查找满足条件的元素的方法。
- baseFindIndex:基础查找函数,用于在数组中查找满足条件的元素的索引,支持从左到右或从右到左查找。
- findIndex:对外公开的函数,处理数组查找,支持指定起始索引,并调用 baseFindIndex 实现查找。
- find:对外公开的函数,通过 findIndex 查找满足条件的元素,并返回该元素。
4.findLast函数
function baseFindIndex(array, predicate, fromIndex, fromRight) {var length = array.length,index = fromIndex + (fromRight ? 1 : -1);while ((fromRight ? index-- : ++index < length)) {if (predicate(array[index], index, array)) {return index;}}return -1;}function findLastIndex(array, predicate, fromIndex) {var length = array == null ? 0 : array.length;if (!length) {return -1;}var index = length - 1;if (fromIndex !== undefined) {index = toInteger(fromIndex);index = fromIndex < 0 ?nativeMax(length + index, 0) :nativeMin(index, length - 1);}return baseFindIndex(array, getIteratee(predicate, 3), index, true);}var findLast = createFind(findLastIndex);
findLast 函数用于在数组中从右向左查找满足条件的元素的方法。
- baseFindIndex:基础查找函数,用于在数组中查找满足条件的元素的索引,支持从左到右或从右到左查找。
- findLastIndex:对外公开的函数,处理数组从右向左查找,支持指定起始索引,并调用 baseFindIndex 实现查找。
- findLast:对外公开的函数,通过 findLastIndex 查找满足条件的元素,并返回该元素。
5.partition函数
function createAggregator(setter, initializer) {return function(collection, iteratee) {var func = isArray(collection) ? arrayAggregator : baseAggregator,accumulator = initializer ? initializer() : {};return func(collection, setter, getIteratee(iteratee, 2), accumulator);};}var partition = createAggregator(function(result, value, key) {result[key ? 0 : 1].push(value);}, function() {return [[],[]];});
//例子
var users = [{ 'user': 'barney', 'age': 36, 'active': true },{ 'user': 'fred', 'age': 40, 'active': false },{ 'user': 'pebbles', 'age': 28, 'active': true }
];var partitioned = partition(users, function(user) {return user.age > 30;
});console.log(partitioned);
// 输出:
// [
// [{ 'user': 'barney', 'age': 36, 'active': true }, { 'user': 'fred', 'age': 40, 'active': false }],
// [{ 'user': 'pebbles', 'age': 28, 'active': true }]
// ]
partition函数用于将集合分组的函数。它将集合中的元素根据某个条件分为两个数组:一个包含满足条件的元素,另一个包含不满足条件的元素。
四、总结
filter 和 reject 是筛选操作,分别返回满足和不满足条件的元素。
find 和 findLast 是查找操作,分别返回第一个满足条件的元素。
partition 是分组操作,将元素分为满足条件和不满足条件的两组。