sortedIndex.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. var createSortedIndex = require('../internal/createSortedIndex');
  2. /**
  3. * Uses a binary search to determine the lowest index at which `value` should
  4. * be inserted into `array` in order to maintain its sort order. If an iteratee
  5. * function is provided it's invoked for `value` and each element of `array`
  6. * to compute their sort ranking. The iteratee is bound to `thisArg` and
  7. * invoked with one argument; (value).
  8. *
  9. * If a property name is provided for `iteratee` the created `_.property`
  10. * style callback returns the property value of the given element.
  11. *
  12. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  13. * style callback returns `true` for elements that have a matching property
  14. * value, else `false`.
  15. *
  16. * If an object is provided for `iteratee` the created `_.matches` style
  17. * callback returns `true` for elements that have the properties of the given
  18. * object, else `false`.
  19. *
  20. * @static
  21. * @memberOf _
  22. * @category Array
  23. * @param {Array} array The sorted array to inspect.
  24. * @param {*} value The value to evaluate.
  25. * @param {Function|Object|string} [iteratee=_.identity] The function invoked
  26. * per iteration.
  27. * @param {*} [thisArg] The `this` binding of `iteratee`.
  28. * @returns {number} Returns the index at which `value` should be inserted
  29. * into `array`.
  30. * @example
  31. *
  32. * _.sortedIndex([30, 50], 40);
  33. * // => 1
  34. *
  35. * _.sortedIndex([4, 4, 5, 5], 5);
  36. * // => 2
  37. *
  38. * var dict = { 'data': { 'thirty': 30, 'forty': 40, 'fifty': 50 } };
  39. *
  40. * // using an iteratee function
  41. * _.sortedIndex(['thirty', 'fifty'], 'forty', function(word) {
  42. * return this.data[word];
  43. * }, dict);
  44. * // => 1
  45. *
  46. * // using the `_.property` callback shorthand
  47. * _.sortedIndex([{ 'x': 30 }, { 'x': 50 }], { 'x': 40 }, 'x');
  48. * // => 1
  49. */
  50. var sortedIndex = createSortedIndex();
  51. module.exports = sortedIndex;