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