_lazyValue.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. var baseWrapperValue = require('./_baseWrapperValue'),
  2. getView = require('./_getView'),
  3. isArray = require('./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. /* Built-in 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 ||
  35. (arrLength == length && takeCount == length)) {
  36. return baseWrapperValue(array, this.__actions__);
  37. }
  38. var result = [];
  39. outer:
  40. while (length-- && resIndex < takeCount) {
  41. index += dir;
  42. var iterIndex = -1,
  43. value = array[index];
  44. while (++iterIndex < iterLength) {
  45. var data = iteratees[iterIndex],
  46. iteratee = data.iteratee,
  47. type = data.type,
  48. computed = iteratee(value);
  49. if (type == LAZY_MAP_FLAG) {
  50. value = computed;
  51. } else if (!computed) {
  52. if (type == LAZY_FILTER_FLAG) {
  53. continue outer;
  54. } else {
  55. break outer;
  56. }
  57. }
  58. }
  59. result[resIndex++] = value;
  60. }
  61. return result;
  62. }
  63. module.exports = lazyValue;