_array-reduce.js 821 B

12345678910111213141516171819202122232425262728
  1. var aFunction = require('./_a-function');
  2. var toObject = require('./_to-object');
  3. var IObject = require('./_iobject');
  4. var toLength = require('./_to-length');
  5. module.exports = function (that, callbackfn, aLen, memo, isRight) {
  6. aFunction(callbackfn);
  7. var O = toObject(that);
  8. var self = IObject(O);
  9. var length = toLength(O.length);
  10. var index = isRight ? length - 1 : 0;
  11. var i = isRight ? -1 : 1;
  12. if (aLen < 2) for (;;) {
  13. if (index in self) {
  14. memo = self[index];
  15. index += i;
  16. break;
  17. }
  18. index += i;
  19. if (isRight ? index < 0 : length <= index) {
  20. throw TypeError('Reduce of empty array with no initial value');
  21. }
  22. }
  23. for (;isRight ? index >= 0 : length > index; index += i) if (index in self) {
  24. memo = callbackfn(memo, self[index], index, O);
  25. }
  26. return memo;
  27. };