underscore-array-methods.js 925 B

12345678910111213141516171819202122232425262728293031
  1. import _ from './underscore.js';
  2. import each from './each.js';
  3. import { ArrayProto } from './_setup.js';
  4. import chainResult from './_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 = ArrayProto[name];
  8. _.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 = ArrayProto[name];
  22. _.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. export default _;