"I'm not in this world to live up to your expectations and you're not in this world to live up to mine." —Bruce Lee
import baseForOwn from './baseForOwn.js';
import isArrayLike from '../isArrayLike.js';
/**
* forEach 的基础实现
*
* @private
* @param {Array|Object} collection 需要迭代操作的集合
* @param {Function} iteratee 每次迭代调用的函数
* @returns {Array|Object} 返回新合集 `collection`.
*/
function baseEach(collection, iteratee) {
if (collection == null) {
return collection;
}
// 判断是否为类数组参数,否则返回执行 baseForOwn
if (!isArrayLike(collection)) {
return baseForOwn(collection, iteratee);
}
const length = collection.length;
// 装箱 collection,兼容原始类型情况,例如 string
const iterable = Object(collection);
let index = -1;
while (++index < length) {
if (iteratee(iterable[index], index, iterable) === false) {
break;
}
}
return collection;
}