wallet-core/node_modules/core-js/modules/es6.array.from.js

38 lines
1.6 KiB
JavaScript
Raw Normal View History

2016-10-10 03:43:44 +02:00
'use strict';
2017-08-14 05:01:11 +02:00
var ctx = require('./_ctx');
var $export = require('./_export');
var toObject = require('./_to-object');
var call = require('./_iter-call');
var isArrayIter = require('./_is-array-iter');
var toLength = require('./_to-length');
var createProperty = require('./_create-property');
var getIterFn = require('./core.get-iterator-method');
2017-05-27 17:36:13 +02:00
2017-08-14 05:01:11 +02:00
$export($export.S + $export.F * !require('./_iter-detect')(function (iter) { Array.from(iter); }), 'Array', {
2016-10-10 03:43:44 +02:00
// 22.1.2.1 Array.from(arrayLike, mapfn = undefined, thisArg = undefined)
2017-08-14 05:01:11 +02:00
from: function from(arrayLike /* , mapfn = undefined, thisArg = undefined */) {
var O = toObject(arrayLike);
var C = typeof this == 'function' ? this : Array;
var aLen = arguments.length;
var mapfn = aLen > 1 ? arguments[1] : undefined;
var mapping = mapfn !== undefined;
var index = 0;
var iterFn = getIterFn(O);
var length, result, step, iterator;
if (mapping) mapfn = ctx(mapfn, aLen > 2 ? arguments[2] : undefined, 2);
2016-10-10 03:43:44 +02:00
// if object isn't iterable or it's array with default iterator - use simple case
2017-08-14 05:01:11 +02:00
if (iterFn != undefined && !(C == Array && isArrayIter(iterFn))) {
for (iterator = iterFn.call(O), result = new C(); !(step = iterator.next()).done; index++) {
2017-05-27 17:36:13 +02:00
createProperty(result, index, mapping ? call(iterator, mapfn, [step.value, index], true) : step.value);
2016-10-10 03:43:44 +02:00
}
} else {
length = toLength(O.length);
2017-08-14 05:01:11 +02:00
for (result = new C(length); length > index; index++) {
2017-05-27 17:36:13 +02:00
createProperty(result, index, mapping ? mapfn(O[index], index) : O[index]);
2016-10-10 03:43:44 +02:00
}
}
result.length = index;
return result;
}
});