Cleanup _.rearg docs and simplify reorder.

This commit is contained in:
John-David Dalton
2014-11-06 20:41:50 -08:00
parent 8089242b2f
commit 062ec036f8

View File

@@ -2692,7 +2692,7 @@
args[index] = arguments[index]; args[index] = arguments[index];
} }
if (argPos) { if (argPos) {
args = reorder(args, argPos); args = arrayReduceRight(argPos, reorder, args);
} }
if (partials) { if (partials) {
args = composeArgs(args, partials, holders); args = composeArgs(args, partials, holders);
@@ -3171,28 +3171,27 @@
} }
/** /**
* A specialized version of `_.pick` that picks `object` properties `predicate` * Reorder `array` according to the specified indexes where the element at
* returns truthy for. * the first index is assigned as the first element, the element at
* the second index is assigned as the second element, and so on.
* *
* @private * @private
* @param {Object} object The source object. * @param {Array} array The array to reorder.
* @param {Function} predicate The function invoked per iteration. * @param {Array} indexes The arranged array indexes.
* @returns {Object} Returns the new object. * @returns {Array} Returns `array`.
*/ */
function reorder(args, positions) { function reorder(array, indexes) {
var length = positions.length; var arrLength = array.length,
length = nativeMin(indexes.length, arrLength),
oldArray = baseSlice(array);
while (length--) { while (length--) {
var pos = positions[length], var index = indexes[length];
posIndex = -1, array[length] = (index > -1 && index < arrLength && index % 1 == 0)
posLength = pos.length, ? oldArray[index]
argsClone = baseSlice(args); : undefined;
while (++posIndex < posLength) {
args[pos[posIndex]] = argsClone[posIndex];
}
} }
return args; return array;
} }
/** /**
@@ -6818,19 +6817,30 @@
} }
/** /**
* Creates a function that invokes `func` with `partial` arguments prepended * Creates a function that invokes `func` with arguments arranged according
* to those provided to the new function. This method is similar to `_.bind` * to the specified indexes where the argument value at the first index is
* except it does **not** alter the `this` binding. * provided as the first argument, the argument value at the second index is
* provided as the second argument, and so on.
* *
* @static * @static
* @memberOf _ * @memberOf _
* @category Function * @category Function
* @param {Function} func The function to partially apply arguments to. * @param {Function} func The function to rearrange arguments for.
* @param {...(number|number[])} [indexes] The indexes of elements to remove, * @param {...(number|number[])} [indexes] The arranged argument indexes,
* specified as individual indexes or arrays of indexes. * specified as individual indexes or arrays of indexes.
* @returns {Function} Returns the new function. * @returns {Function} Returns the new function.
* @example * @example
* *
* var rearged = _.rearg(function(a, b, c) {
* return [a, b, c];
* }, 2, 0, 1);
*
* rearged('b', 'c', 'a')
* // => ['a', 'b', 'c']
*
* var map = _.rearg(_.map, [1, 0]);
* map(function(n) { return n * 3; }, [1, 2, 3]);
* // => [3, 6, 9]
*/ */
function rearg(func) { function rearg(func) {
var indexes = baseFlatten(arguments, false, false, 1); var indexes = baseFlatten(arguments, false, false, 1);