esnext.set.reduce.js 925 B

12345678910111213141516171819202122232425262728
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var aCallable = require('../internals/a-callable');
  4. var aSet = require('../internals/a-set');
  5. var iterate = require('../internals/set-iterate');
  6. var $TypeError = TypeError;
  7. // `Set.prototype.reduce` method
  8. // https://github.com/tc39/proposal-collection-methods
  9. $({ target: 'Set', proto: true, real: true, forced: true }, {
  10. reduce: function reduce(callbackfn /* , initialValue */) {
  11. var set = aSet(this);
  12. var noInitial = arguments.length < 2;
  13. var accumulator = noInitial ? undefined : arguments[1];
  14. aCallable(callbackfn);
  15. iterate(set, function (value) {
  16. if (noInitial) {
  17. noInitial = false;
  18. accumulator = value;
  19. } else {
  20. accumulator = callbackfn(accumulator, value, value, set);
  21. }
  22. });
  23. if (noInitial) throw new $TypeError('Reduce of empty set with no initial value');
  24. return accumulator;
  25. }
  26. });