Add createSortedIndex.

This commit is contained in:
jdalton
2015-03-21 18:06:14 -07:00
parent 820ab8d486
commit f26886acb0

View File

@@ -2893,8 +2893,7 @@
* @private
* @param {Array} array The sorted array to inspect.
* @param {*} value The value to evaluate.
* @param {boolean} [retHighest] Specify returning the highest, instead
* of the lowest, index at which a value should be inserted into `array`.
* @param {boolean} [retHighest] Specify returning the highest qualified index.
* @returns {number} Returns the index at which `value` should be inserted
* into `array`.
*/
@@ -2927,8 +2926,7 @@
* @param {Array} array The sorted array to inspect.
* @param {*} value The value to evaluate.
* @param {Function} iteratee The function invoked per iteration.
* @param {boolean} [retHighest] Specify returning the highest, instead
* of the lowest, index at which a value should be inserted into `array`.
* @param {boolean} [retHighest] Specify returning the highest qualified index.
* @returns {number} Returns the index at which `value` should be inserted
* into `array`.
*/
@@ -3663,6 +3661,22 @@
return wrapper;
}
/**
* Creates a `_.sortedIndex` or `_.sortedLastIndex` function.
*
* @private
* @param {boolean} [retHighest] Specify returning the highest qualified index.
* @returns {Function} Returns the new index function.
*/
function createSortedIndex(retHighest) {
return function(array, value, iteratee, thisArg) {
var func = getCallback(iteratee);
return (func === baseCallback && iteratee == null)
? binaryIndex(array, value, retHighest)
: binaryIndexBy(array, value, func(iteratee, thisArg, 1), retHighest);
};
}
/**
* Creates a function that either curries or invokes `func` with optional
* `this` binding and partially applied arguments.
@@ -5377,12 +5391,7 @@
* _.sortedIndex([{ 'x': 30 }, { 'x': 50 }], { 'x': 40 }, 'x');
* // => 1
*/
function sortedIndex(array, value, iteratee, thisArg) {
var func = getCallback(iteratee);
return (func === baseCallback && iteratee == null)
? binaryIndex(array, value)
: binaryIndexBy(array, value, func(iteratee, thisArg, 1));
}
var sortedIndex = createSortedIndex();
/**
* This method is like `_.sortedIndex` except that it returns the highest
@@ -5404,12 +5413,7 @@
* _.sortedLastIndex([4, 4, 5, 5], 5);
* // => 4
*/
function sortedLastIndex(array, value, iteratee, thisArg) {
var func = getCallback(iteratee);
return (func === baseCallback && iteratee == null)
? binaryIndex(array, value, true)
: binaryIndexBy(array, value, func(iteratee, thisArg, 1), true);
}
var sortedLastIndex = createSortedIndex(true);
/**
* Creates a slice of `array` with `n` elements taken from the beginning.