mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 02:47:50 +00:00
lodash: Move sortedIndex to the "Arrays" category. [jddalton]
Former-commit-id: fb9d6afc3c805ac25c8e6e7968fdcb4e8da93d30
This commit is contained in:
89
lodash.js
89
lodash.js
@@ -797,45 +797,6 @@
|
|||||||
'inLoop': everyFactoryOptions.inLoop.replace('!', '')
|
'inLoop': everyFactoryOptions.inLoop.replace('!', '')
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
|
||||||
* Uses a binary search to determine the smallest index at which the `value`
|
|
||||||
* should be inserted into the `collection` in order to maintain the sort order
|
|
||||||
* of the `collection`. If `callback` is passed, it will be executed for each
|
|
||||||
* value in the `collection` to compute their sort ranking. The `callback` is
|
|
||||||
* invoked with 1 argument.
|
|
||||||
*
|
|
||||||
* @static
|
|
||||||
* @memberOf _
|
|
||||||
* @category Collections
|
|
||||||
* @param {Array} array The array to iterate over.
|
|
||||||
* @param {Mixed} value The value to evaluate.
|
|
||||||
* @param {Function} [callback] The function called per iteration.
|
|
||||||
* @returns {Number} Returns the index at which the value should be inserted
|
|
||||||
* into the collection.
|
|
||||||
* @example
|
|
||||||
*
|
|
||||||
* _.sortedIndex([10, 20, 30, 40, 50], 35);
|
|
||||||
* // => 3
|
|
||||||
*/
|
|
||||||
function sortedIndex(array, value, callback) {
|
|
||||||
var mid,
|
|
||||||
low = 0,
|
|
||||||
high = array.length;
|
|
||||||
|
|
||||||
if (callback) {
|
|
||||||
value = callback(value);
|
|
||||||
}
|
|
||||||
while (low < high) {
|
|
||||||
mid = (low + high) >> 1;
|
|
||||||
if ((callback ? callback(array[mid]) : array[mid]) < value) {
|
|
||||||
low = mid + 1;
|
|
||||||
} else {
|
|
||||||
high = mid;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return low;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts the `collection`, into an array. Useful for converting the
|
* Converts the `collection`, into an array. Useful for converting the
|
||||||
* `arguments` object.
|
* `arguments` object.
|
||||||
@@ -1269,6 +1230,45 @@
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uses a binary search to determine the smallest index at which the `value`
|
||||||
|
* should be inserted into the `collection` in order to maintain the sort order
|
||||||
|
* of the `collection`. If `callback` is passed, it will be executed for each
|
||||||
|
* value in the `collection` to compute their sort ranking. The `callback` is
|
||||||
|
* invoked with 1 argument; (value).
|
||||||
|
*
|
||||||
|
* @static
|
||||||
|
* @memberOf _
|
||||||
|
* @category Arrays
|
||||||
|
* @param {Array} array The array to iterate over.
|
||||||
|
* @param {Mixed} value The value to evaluate.
|
||||||
|
* @param {Function} [callback] The function called per iteration.
|
||||||
|
* @returns {Number} Returns the index at which the value should be inserted
|
||||||
|
* into the collection.
|
||||||
|
* @example
|
||||||
|
*
|
||||||
|
* _.sortedIndex([10, 20, 30, 40, 50], 35);
|
||||||
|
* // => 3
|
||||||
|
*/
|
||||||
|
function sortedIndex(array, value, callback) {
|
||||||
|
var mid,
|
||||||
|
low = 0,
|
||||||
|
high = array.length;
|
||||||
|
|
||||||
|
if (callback) {
|
||||||
|
value = callback(value);
|
||||||
|
}
|
||||||
|
while (low < high) {
|
||||||
|
mid = (low + high) >> 1;
|
||||||
|
if ((callback ? callback(array[mid]) : array[mid]) < value) {
|
||||||
|
low = mid + 1;
|
||||||
|
} else {
|
||||||
|
high = mid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return low;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Computes the union of the passed-in arrays.
|
* Computes the union of the passed-in arrays.
|
||||||
*
|
*
|
||||||
@@ -1566,8 +1566,8 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Invokes the `func` function after `wait` milliseconds. Additional arguments
|
* Executes the `func` function after `wait` milliseconds. Additional arguments
|
||||||
* are passed `func` when it is invoked.
|
* are passed to `func` when it is invoked.
|
||||||
*
|
*
|
||||||
* @static
|
* @static
|
||||||
* @memberOf _
|
* @memberOf _
|
||||||
@@ -1588,7 +1588,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defers invoking the `func` function until the current call stack has cleared.
|
* Defers executing the `func` function until the current call stack has cleared.
|
||||||
* Additional arguments are passed to `func` when it is invoked.
|
* Additional arguments are passed to `func` when it is invoked.
|
||||||
*
|
*
|
||||||
* @static
|
* @static
|
||||||
@@ -1666,7 +1666,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new function that, when invoked, will only call the original
|
* Creates a new function that, when executed, will only call the original
|
||||||
* function at most once per every `wait` milliseconds.
|
* function at most once per every `wait` milliseconds.
|
||||||
*
|
*
|
||||||
* @static
|
* @static
|
||||||
@@ -2452,7 +2452,8 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes the `callback` function `n` times.
|
* Executes the `callback` function `n` times. The `callback` is invoked with
|
||||||
|
* 1 argument; (index).
|
||||||
*
|
*
|
||||||
* @static
|
* @static
|
||||||
* @memberOf _
|
* @memberOf _
|
||||||
|
|||||||
Reference in New Issue
Block a user