You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.2 KiB

4 years ago
  1. var isSymbol = require('./isSymbol');
  2. /** Used as references for the maximum length and index of an array. */
  3. var MAX_ARRAY_LENGTH = 4294967295,
  4. MAX_ARRAY_INDEX = MAX_ARRAY_LENGTH - 1;
  5. /* Built-in method references for those with the same name as other `lodash` methods. */
  6. var nativeFloor = Math.floor,
  7. nativeMin = Math.min;
  8. /**
  9. * The base implementation of `_.sortedIndexBy` and `_.sortedLastIndexBy`
  10. * which invokes `iteratee` for `value` and each element of `array` to compute
  11. * their sort ranking. The iteratee is invoked with one argument; (value).
  12. *
  13. * @private
  14. * @param {Array} array The sorted array to inspect.
  15. * @param {*} value The value to evaluate.
  16. * @param {Function} iteratee The iteratee invoked per element.
  17. * @param {boolean} [retHighest] Specify returning the highest qualified index.
  18. * @returns {number} Returns the index at which `value` should be inserted
  19. * into `array`.
  20. */
  21. function baseSortedIndexBy(array, value, iteratee, retHighest) {
  22. value = iteratee(value);
  23. var low = 0,
  24. high = array == null ? 0 : array.length,
  25. valIsNaN = value !== value,
  26. valIsNull = value === null,
  27. valIsSymbol = isSymbol(value),
  28. valIsUndefined = value === undefined;
  29. while (low < high) {
  30. var mid = nativeFloor((low + high) / 2),
  31. computed = iteratee(array[mid]),
  32. othIsDefined = computed !== undefined,
  33. othIsNull = computed === null,
  34. othIsReflexive = computed === computed,
  35. othIsSymbol = isSymbol(computed);
  36. if (valIsNaN) {
  37. var setLow = retHighest || othIsReflexive;
  38. } else if (valIsUndefined) {
  39. setLow = othIsReflexive && (retHighest || othIsDefined);
  40. } else if (valIsNull) {
  41. setLow = othIsReflexive && othIsDefined && (retHighest || !othIsNull);
  42. } else if (valIsSymbol) {
  43. setLow = othIsReflexive && othIsDefined && !othIsNull && (retHighest || !othIsSymbol);
  44. } else if (othIsNull || othIsSymbol) {
  45. setLow = false;
  46. } else {
  47. setLow = retHighest ? (computed <= value) : (computed < value);
  48. }
  49. if (setLow) {
  50. low = mid + 1;
  51. } else {
  52. high = mid;
  53. }
  54. }
  55. return nativeMin(high, MAX_ARRAY_INDEX);
  56. }
  57. module.exports = baseSortedIndexBy;