1
|
'use strict';
|
2
|
|
3
|
var inspect = require('object-inspect');
|
4
|
|
5
|
var GetIntrinsic = require('../GetIntrinsic');
|
6
|
|
7
|
var $TypeError = GetIntrinsic('%TypeError%');
|
8
|
|
9
|
var Call = require('./Call');
|
10
|
var Get = require('./Get');
|
11
|
var GetIterator = require('./GetIterator');
|
12
|
var IsCallable = require('./IsCallable');
|
13
|
var IteratorClose = require('./IteratorClose');
|
14
|
var IteratorStep = require('./IteratorStep');
|
15
|
var IteratorValue = require('./IteratorValue');
|
16
|
var Type = require('./Type');
|
17
|
|
18
|
// https://tc39.es/ecma262/#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 (Type(nextItem) !== 'Object') {
|
35
|
var error = new $TypeError('iterator next must return an Object, got ' + inspect(nextItem));
|
36
|
return IteratorClose(
|
37
|
iteratorRecord,
|
38
|
function () { throw error; } // eslint-disable-line no-loop-func
|
39
|
);
|
40
|
}
|
41
|
try {
|
42
|
var k = Get(nextItem, '0');
|
43
|
var v = Get(nextItem, '1');
|
44
|
Call(adder, target, [k, v]);
|
45
|
} catch (e) {
|
46
|
return IteratorClose(
|
47
|
iteratorRecord,
|
48
|
function () { throw e; }
|
49
|
);
|
50
|
}
|
51
|
}
|
52
|
};
|