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-05-27 17:36:13 +02:00
var ctx = require('./_ctx')
, $export = require('./_export')
, toObject = require('./_to-object')
, call = require('./_iter-call')
, isArrayIter = require('./_is-array-iter')
, toLength = require('./_to-length')
, createProperty = require('./_create-property')
, getIterFn = require('./core.get-iterator-method');
$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)
from: function from(arrayLike/*, mapfn = undefined, thisArg = undefined*/){
var O = toObject(arrayLike)
, C = typeof this == 'function' ? this : Array
2017-05-27 17:36:13 +02:00
, aLen = arguments.length
, mapfn = aLen > 1 ? arguments[1] : undefined
2016-10-10 03:43:44 +02:00
, mapping = mapfn !== undefined
, index = 0
, iterFn = getIterFn(O)
, length, result, step, iterator;
2017-05-27 17:36:13 +02:00
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
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);
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;
}
});