AddEntriesFromIterable.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. 'use strict';
  2. var inspect = require('object-inspect');
  3. var GetIntrinsic = require('get-intrinsic');
  4. var $TypeError = GetIntrinsic('%TypeError%');
  5. var Call = require('./Call');
  6. var Get = require('./Get');
  7. var GetIterator = require('./GetIterator');
  8. var IsCallable = require('./IsCallable');
  9. var IteratorClose = require('./IteratorClose');
  10. var IteratorStep = require('./IteratorStep');
  11. var IteratorValue = require('./IteratorValue');
  12. var ThrowCompletion = require('./ThrowCompletion');
  13. var Type = require('./Type');
  14. // https://262.ecma-international.org/10.0/#sec-add-entries-from-iterable
  15. module.exports = function AddEntriesFromIterable(target, iterable, adder) {
  16. if (!IsCallable(adder)) {
  17. throw new $TypeError('Assertion failed: `adder` is not callable');
  18. }
  19. if (iterable == null) {
  20. throw new $TypeError('Assertion failed: `iterable` is present, and not nullish');
  21. }
  22. var iteratorRecord = GetIterator(iterable);
  23. while (true) { // eslint-disable-line no-constant-condition
  24. var next = IteratorStep(iteratorRecord);
  25. if (!next) {
  26. return target;
  27. }
  28. var nextItem = IteratorValue(next);
  29. if (Type(nextItem) !== 'Object') {
  30. var error = ThrowCompletion(new $TypeError('iterator next must return an Object, got ' + inspect(nextItem)));
  31. return IteratorClose(iteratorRecord, error);
  32. }
  33. try {
  34. var k = Get(nextItem, '0');
  35. var v = Get(nextItem, '1');
  36. Call(adder, target, [k, v]);
  37. } catch (e) {
  38. return IteratorClose(iteratorRecord, ThrowCompletion(e));
  39. }
  40. }
  41. };