_array-reduce.js 816 B

12345678910111213141516171819202122232425262728
  1. var aFunction = require('./_a-function')
  2. , toObject = require('./_to-object')
  3. , IObject = require('./_iobject')
  4. , toLength = require('./_to-length');
  5. module.exports = function(that, callbackfn, aLen, memo, isRight){
  6. aFunction(callbackfn);
  7. var O = toObject(that)
  8. , self = IObject(O)
  9. , length = toLength(O.length)
  10. , index = isRight ? length - 1 : 0
  11. , 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. };