mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-12 11:57:49 +00:00
Add baseWhile and createBaseEach.
This commit is contained in:
120
lodash.src.js
120
lodash.src.js
@@ -2025,21 +2025,7 @@
|
|||||||
* @param {Function} iteratee The function invoked per iteration.
|
* @param {Function} iteratee The function invoked per iteration.
|
||||||
* @returns {Array|Object|string} Returns `collection`.
|
* @returns {Array|Object|string} Returns `collection`.
|
||||||
*/
|
*/
|
||||||
function baseEach(collection, iteratee) {
|
var baseEach = createBaseEach(baseForOwn);
|
||||||
var length = collection ? collection.length : 0;
|
|
||||||
if (!isLength(length)) {
|
|
||||||
return baseForOwn(collection, iteratee);
|
|
||||||
}
|
|
||||||
var index = -1,
|
|
||||||
iterable = toObject(collection);
|
|
||||||
|
|
||||||
while (++index < length) {
|
|
||||||
if (iteratee(iterable[index], index, iterable) === false) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return collection;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The base implementation of `_.forEachRight` without support for callback
|
* The base implementation of `_.forEachRight` without support for callback
|
||||||
@@ -2050,19 +2036,7 @@
|
|||||||
* @param {Function} iteratee The function invoked per iteration.
|
* @param {Function} iteratee The function invoked per iteration.
|
||||||
* @returns {Array|Object|string} Returns `collection`.
|
* @returns {Array|Object|string} Returns `collection`.
|
||||||
*/
|
*/
|
||||||
function baseEachRight(collection, iteratee) {
|
var baseEachRight = createBaseEach(baseForOwnRight, true);
|
||||||
var length = collection ? collection.length : 0;
|
|
||||||
if (!isLength(length)) {
|
|
||||||
return baseForOwnRight(collection, iteratee);
|
|
||||||
}
|
|
||||||
var iterable = toObject(collection);
|
|
||||||
while (length--) {
|
|
||||||
if (iteratee(iterable[length], length, iterable) === false) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return collection;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The base implementation of `_.every` without support for callback
|
* The base implementation of `_.every` without support for callback
|
||||||
@@ -2858,6 +2832,27 @@
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The base implementation of `_.dropRightWhile`, `_.dropWhile`, `_.takeRightWhile`,
|
||||||
|
* and `_.takeWhile` without support for callback shorthands and `this` binding.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
* @param {Array} array The array to query.
|
||||||
|
* @param {Function} predicate The function invoked per iteration.
|
||||||
|
* @param {boolean} [isDrop] Specify dropping elements instead of taking them.
|
||||||
|
* @param {boolean} [fromRight] Specify iterating from right to left.
|
||||||
|
* @returns {Array} Returns the slice of `array`.
|
||||||
|
*/
|
||||||
|
function baseWhile(array, predicate, isDrop, fromRight) {
|
||||||
|
var length = array.length,
|
||||||
|
index = fromRight ? length : -1;
|
||||||
|
|
||||||
|
while ((fromRight ? index-- : ++index < length) && predicate(array[index], index, array)) {}
|
||||||
|
return isDrop
|
||||||
|
? baseSlice(array, (fromRight ? 0 : index), (fromRight ? index + 1 : length))
|
||||||
|
: baseSlice(array, (fromRight ? index + 1 : 0), (fromRight ? length : index));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The base implementation of `wrapperValue` which returns the result of
|
* The base implementation of `wrapperValue` which returns the result of
|
||||||
* performing a sequence of actions on the unwrapped `value`, where each
|
* performing a sequence of actions on the unwrapped `value`, where each
|
||||||
@@ -3095,7 +3090,6 @@
|
|||||||
* **Note:** This function is used to create `_.countBy`, `_.groupBy`, `_.indexBy`,
|
* **Note:** This function is used to create `_.countBy`, `_.groupBy`, `_.indexBy`,
|
||||||
* and `_.partition`.
|
* and `_.partition`.
|
||||||
*
|
*
|
||||||
*
|
|
||||||
* @private
|
* @private
|
||||||
* @param {Function} setter The function to set keys and values of the accumulator object.
|
* @param {Function} setter The function to set keys and values of the accumulator object.
|
||||||
* @param {Function} [initializer] The function to initialize the accumulator object.
|
* @param {Function} [initializer] The function to initialize the accumulator object.
|
||||||
@@ -3168,6 +3162,32 @@
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a `baseEach` or `baseEachRight` function.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
* @param {Function} eachFunc The function to iterate over a collection.
|
||||||
|
* @param {boolean} [fromRight] Specify iterating from right to left.
|
||||||
|
* @returns {Function} Returns the new base function.
|
||||||
|
*/
|
||||||
|
function createBaseEach(eachFunc, fromRight) {
|
||||||
|
return function(collection, iteratee) {
|
||||||
|
var length = collection ? collection.length : 0;
|
||||||
|
if (!isLength(length)) {
|
||||||
|
return eachFunc(collection, iteratee);
|
||||||
|
}
|
||||||
|
var index = fromRight ? length : -1,
|
||||||
|
iterable = toObject(collection);
|
||||||
|
|
||||||
|
while ((fromRight ? index-- : ++index < length)) {
|
||||||
|
if (iteratee(iterable[index], index, iterable) === false) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return collection;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a base function for `_.forIn` or `_.forInRight`.
|
* Creates a base function for `_.forIn` or `_.forInRight`.
|
||||||
*
|
*
|
||||||
@@ -4680,13 +4700,9 @@
|
|||||||
* // => ['barney', 'fred', 'pebbles']
|
* // => ['barney', 'fred', 'pebbles']
|
||||||
*/
|
*/
|
||||||
function dropRightWhile(array, predicate, thisArg) {
|
function dropRightWhile(array, predicate, thisArg) {
|
||||||
var length = array ? array.length : 0;
|
return (array && array.length)
|
||||||
if (!length) {
|
? baseWhile(array, getCallback(predicate, thisArg, 3), true, true)
|
||||||
return [];
|
: [];
|
||||||
}
|
|
||||||
predicate = getCallback(predicate, thisArg, 3);
|
|
||||||
while (length-- && predicate(array[length], length, array)) {}
|
|
||||||
return baseSlice(array, 0, length + 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -4739,14 +4755,9 @@
|
|||||||
* // => ['barney', 'fred', 'pebbles']
|
* // => ['barney', 'fred', 'pebbles']
|
||||||
*/
|
*/
|
||||||
function dropWhile(array, predicate, thisArg) {
|
function dropWhile(array, predicate, thisArg) {
|
||||||
var length = array ? array.length : 0;
|
return (array && array.length)
|
||||||
if (!length) {
|
? baseWhile(array, getCallback(predicate, thisArg, 3), true)
|
||||||
return [];
|
: [];
|
||||||
}
|
|
||||||
var index = -1;
|
|
||||||
predicate = getCallback(predicate, thisArg, 3);
|
|
||||||
while (++index < length && predicate(array[index], index, array)) {}
|
|
||||||
return baseSlice(array, index);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -5536,13 +5547,9 @@
|
|||||||
* // => []
|
* // => []
|
||||||
*/
|
*/
|
||||||
function takeRightWhile(array, predicate, thisArg) {
|
function takeRightWhile(array, predicate, thisArg) {
|
||||||
var length = array ? array.length : 0;
|
return (array && array.length)
|
||||||
if (!length) {
|
? baseWhile(array, getCallback(predicate, thisArg, 3), false, true)
|
||||||
return [];
|
: [];
|
||||||
}
|
|
||||||
predicate = getCallback(predicate, thisArg, 3);
|
|
||||||
while (length-- && predicate(array[length], length, array)) {}
|
|
||||||
return baseSlice(array, length + 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -5595,14 +5602,9 @@
|
|||||||
* // => []
|
* // => []
|
||||||
*/
|
*/
|
||||||
function takeWhile(array, predicate, thisArg) {
|
function takeWhile(array, predicate, thisArg) {
|
||||||
var length = array ? array.length : 0;
|
return (array && array.length)
|
||||||
if (!length) {
|
? baseWhile(array, getCallback(predicate, thisArg, 3))
|
||||||
return [];
|
: [];
|
||||||
}
|
|
||||||
var index = -1;
|
|
||||||
predicate = getCallback(predicate, thisArg, 3);
|
|
||||||
while (++index < length && predicate(array[index], index, array)) {}
|
|
||||||
return baseSlice(array, 0, index);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user