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