AddEntriesFromIterable.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 Type = require('./Type');
  13. // https://262.ecma-international.org/10.0//#sec-add-entries-from-iterable
  14. module.exports = function AddEntriesFromIterable(target, iterable, adder) {
  15. if (!IsCallable(adder)) {
  16. throw new $TypeError('Assertion failed: `adder` is not callable');
  17. }
  18. if (iterable == null) {
  19. throw new $TypeError('Assertion failed: `iterable` is present, and not nullish');
  20. }
  21. var iteratorRecord = GetIterator(iterable);
  22. while (true) { // eslint-disable-line no-constant-condition
  23. var next = IteratorStep(iteratorRecord);
  24. if (!next) {
  25. return target;
  26. }
  27. var nextItem = IteratorValue(next);
  28. if (Type(nextItem) !== 'Object') {
  29. var error = new $TypeError('iterator next must return an Object, got ' + inspect(nextItem));
  30. return IteratorClose(
  31. iteratorRecord,
  32. function () { throw error; } // eslint-disable-line no-loop-func
  33. );
  34. }
  35. try {
  36. var k = Get(nextItem, '0');
  37. var v = Get(nextItem, '1');
  38. Call(adder, target, [k, v]);
  39. } catch (e) {
  40. return IteratorClose(
  41. iteratorRecord,
  42. function () { throw e; }
  43. );
  44. }
  45. }
  46. };