underscore-array-methods.js 984 B

1234567891011121314151617181920212223242526272829303132
  1. var underscore = require('./underscore.js');
  2. var each = require('./each.js');
  3. var _setup = require('./_setup.js');
  4. var _chainResult = require('./_chainResult.js');
  5. // Add all mutator `Array` functions to the wrapper.
  6. each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(name) {
  7. var method = _setup.ArrayProto[name];
  8. underscore.prototype[name] = function() {
  9. var obj = this._wrapped;
  10. if (obj != null) {
  11. method.apply(obj, arguments);
  12. if ((name === 'shift' || name === 'splice') && obj.length === 0) {
  13. delete obj[0];
  14. }
  15. }
  16. return _chainResult(this, obj);
  17. };
  18. });
  19. // Add all accessor `Array` functions to the wrapper.
  20. each(['concat', 'join', 'slice'], function(name) {
  21. var method = _setup.ArrayProto[name];
  22. underscore.prototype[name] = function() {
  23. var obj = this._wrapped;
  24. if (obj != null) obj = method.apply(obj, arguments);
  25. return _chainResult(this, obj);
  26. };
  27. });
  28. module.exports = underscore;