lazyValue.js 1.9 KB

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