wrapperReverse.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. var LazyWrapper = require('../internal/LazyWrapper'),
  2. LodashWrapper = require('../internal/LodashWrapper'),
  3. thru = require('./thru');
  4. /**
  5. * Reverses the wrapped array so the first element becomes the last, the
  6. * second element becomes the second to last, and so on.
  7. *
  8. * **Note:** This method mutates the wrapped array.
  9. *
  10. * @name reverse
  11. * @memberOf _
  12. * @category Chain
  13. * @returns {Object} Returns the new reversed `lodash` wrapper instance.
  14. * @example
  15. *
  16. * var array = [1, 2, 3];
  17. *
  18. * _(array).reverse().value()
  19. * // => [3, 2, 1]
  20. *
  21. * console.log(array);
  22. * // => [3, 2, 1]
  23. */
  24. function wrapperReverse() {
  25. var value = this.__wrapped__;
  26. var interceptor = function(value) {
  27. return value.reverse();
  28. };
  29. if (value instanceof LazyWrapper) {
  30. var wrapped = value;
  31. if (this.__actions__.length) {
  32. wrapped = new LazyWrapper(this);
  33. }
  34. wrapped = wrapped.reverse();
  35. wrapped.__actions__.push({ 'func': thru, 'args': [interceptor], 'thisArg': undefined });
  36. return new LodashWrapper(wrapped, this.__chain__);
  37. }
  38. return this.thru(interceptor);
  39. }
  40. module.exports = wrapperReverse;