_lazyValue.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. var baseWrapperValue = require('./_baseWrapperValue'),
  2. getView = require('./_getView'),
  3. isArray = require('./isArray');
  4. /** Used to indicate the type of lazy iteratees. */
  5. var LAZY_FILTER_FLAG = 1,
  6. LAZY_MAP_FLAG = 2;
  7. /* Built-in method references for those with the same name as other `lodash` methods. */
  8. var nativeMin = Math.min;
  9. /**
  10. * Extracts the unwrapped value from its lazy wrapper.
  11. *
  12. * @private
  13. * @name value
  14. * @memberOf LazyWrapper
  15. * @returns {*} Returns the unwrapped value.
  16. */
  17. function lazyValue() {
  18. var array = this.__wrapped__.value(),
  19. dir = this.__dir__,
  20. isArr = isArray(array),
  21. isRight = dir < 0,
  22. arrLength = isArr ? array.length : 0,
  23. view = getView(0, arrLength, this.__views__),
  24. start = view.start,
  25. end = view.end,
  26. length = end - start,
  27. index = isRight ? end : (start - 1),
  28. iteratees = this.__iteratees__,
  29. iterLength = iteratees.length,
  30. resIndex = 0,
  31. takeCount = nativeMin(length, this.__takeCount__);
  32. if (!isArr || (!isRight && arrLength == length && takeCount == length)) {
  33. return baseWrapperValue(array, this.__actions__);
  34. }
  35. var result = [];
  36. outer:
  37. while (length-- && resIndex < takeCount) {
  38. index += dir;
  39. var iterIndex = -1,
  40. value = array[index];
  41. while (++iterIndex < iterLength) {
  42. var data = iteratees[iterIndex],
  43. iteratee = data.iteratee,
  44. type = data.type,
  45. computed = iteratee(value);
  46. if (type == LAZY_MAP_FLAG) {
  47. value = computed;
  48. } else if (!computed) {
  49. if (type == LAZY_FILTER_FLAG) {
  50. continue outer;
  51. } else {
  52. break outer;
  53. }
  54. }
  55. }
  56. result[resIndex++] = value;
  57. }
  58. return result;
  59. }
  60. module.exports = lazyValue;