esnext.set.reduce.js 1.1 KB

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