2016-10-10 03:43:44 +02:00
|
|
|
'use strict';
|
2017-04-20 03:09:25 +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')
|
|
|
|
, 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-04-20 03:09:25 +02:00
|
|
|
, $$ = arguments
|
|
|
|
, $$len = $$.length
|
|
|
|
, mapfn = $$len > 1 ? $$[1] : undefined
|
2016-10-10 03:43:44 +02:00
|
|
|
, mapping = mapfn !== undefined
|
|
|
|
, index = 0
|
|
|
|
, iterFn = getIterFn(O)
|
|
|
|
, length, result, step, iterator;
|
2017-04-20 03:09:25 +02:00
|
|
|
if(mapping)mapfn = ctx(mapfn, $$len > 2 ? $$[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-04-20 03:09:25 +02:00
|
|
|
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-04-20 03:09:25 +02:00
|
|
|
result[index] = mapping ? mapfn(O[index], index) : O[index];
|
2016-10-10 03:43:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
result.length = index;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
});
|