1 | 'use strict';
|
2 | var bind = require('../internals/function-bind-context');
|
3 | var call = require('../internals/function-call');
|
4 | var aConstructor = require('../internals/a-constructor');
|
5 | var toObject = require('../internals/to-object');
|
6 | var lengthOfArrayLike = require('../internals/length-of-array-like');
|
7 | var getIterator = require('../internals/get-iterator');
|
8 | var getIteratorMethod = require('../internals/get-iterator-method');
|
9 | var isArrayIteratorMethod = require('../internals/is-array-iterator-method');
|
10 | var isBigIntArray = require('../internals/is-big-int-array');
|
11 | var aTypedArrayConstructor = require('../internals/array-buffer-view-core').aTypedArrayConstructor;
|
12 | var toBigInt = require('../internals/to-big-int');
|
13 |
|
14 | module.exports = function from(source /* , mapfn, thisArg */) {
|
15 | var C = aConstructor(this);
|
16 | var O = toObject(source);
|
17 | var argumentsLength = arguments.length;
|
18 | var mapfn = argumentsLength > 1 ? arguments[1] : undefined;
|
19 | var mapping = mapfn !== undefined;
|
20 | var iteratorMethod = getIteratorMethod(O);
|
21 | var i, length, result, thisIsBigIntArray, value, step, iterator, next;
|
22 | if (iteratorMethod && !isArrayIteratorMethod(iteratorMethod)) {
|
23 | iterator = getIterator(O, iteratorMethod);
|
24 | next = iterator.next;
|
25 | O = [];
|
26 | while (!(step = call(next, iterator)).done) {
|
27 | O.push(step.value);
|
28 | }
|
29 | }
|
30 | if (mapping && argumentsLength > 2) {
|
31 | mapfn = bind(mapfn, arguments[2]);
|
32 | }
|
33 | length = lengthOfArrayLike(O);
|
34 | result = new (aTypedArrayConstructor(C))(length);
|
35 | thisIsBigIntArray = isBigIntArray(result);
|
36 | for (i = 0; length > i; i++) {
|
37 | value = mapping ? mapfn(O[i], i) : O[i];
|
38 |
|
39 | result[i] = thisIsBigIntArray ? toBigInt(value) : +value;
|
40 | }
|
41 | return result;
|
42 | };
|