plant.js 1016 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. var baseLodash = require('./_baseLodash'),
  2. wrapperClone = require('./_wrapperClone');
  3. /**
  4. * Creates a clone of the chain sequence planting `value` as the wrapped value.
  5. *
  6. * @name plant
  7. * @memberOf _
  8. * @since 3.2.0
  9. * @category Seq
  10. * @param {*} value The value to plant.
  11. * @returns {Object} Returns the new `lodash` wrapper instance.
  12. * @example
  13. *
  14. * function square(n) {
  15. * return n * n;
  16. * }
  17. *
  18. * var wrapped = _([1, 2]).map(square);
  19. * var other = wrapped.plant([3, 4]);
  20. *
  21. * other.value();
  22. * // => [9, 16]
  23. *
  24. * wrapped.value();
  25. * // => [1, 4]
  26. */
  27. function wrapperPlant(value) {
  28. var result,
  29. parent = this;
  30. while (parent instanceof baseLodash) {
  31. var clone = wrapperClone(parent);
  32. clone.__index__ = 0;
  33. clone.__values__ = undefined;
  34. if (result) {
  35. previous.__wrapped__ = clone;
  36. } else {
  37. result = clone;
  38. }
  39. var previous = clone;
  40. parent = parent.__wrapped__;
  41. }
  42. previous.__wrapped__ = value;
  43. return result;
  44. }
  45. module.exports = wrapperPlant;