UNPKG

1.79 kBJavaScriptView Raw
1'use strict';
2var bind = require('../internals/function-bind-context');
3var call = require('../internals/function-call');
4var aConstructor = require('../internals/a-constructor');
5var toObject = require('../internals/to-object');
6var lengthOfArrayLike = require('../internals/length-of-array-like');
7var getIterator = require('../internals/get-iterator');
8var getIteratorMethod = require('../internals/get-iterator-method');
9var isArrayIteratorMethod = require('../internals/is-array-iterator-method');
10var isBigIntArray = require('../internals/is-big-int-array');
11var aTypedArrayConstructor = require('../internals/array-buffer-view-core').aTypedArrayConstructor;
12var toBigInt = require('../internals/to-big-int');
13
14module.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 // FF30- typed arrays doesn't properly convert objects to typed array values
39 result[i] = thisIsBigIntArray ? toBigInt(value) : +value;
40 }
41 return result;
42};