_set-collection-from.js 802 B

12345678910111213141516171819202122232425262728
  1. 'use strict';
  2. // https://tc39.github.io/proposal-setmap-offrom/
  3. var $export = require('./_export');
  4. var aFunction = require('./_a-function');
  5. var ctx = require('./_ctx');
  6. var forOf = require('./_for-of');
  7. module.exports = function (COLLECTION) {
  8. $export($export.S, COLLECTION, { from: function from(source /* , mapFn, thisArg */) {
  9. var mapFn = arguments[1];
  10. var mapping, A, n, cb;
  11. aFunction(this);
  12. mapping = mapFn !== undefined;
  13. if (mapping) aFunction(mapFn);
  14. if (source == undefined) return new this();
  15. A = [];
  16. if (mapping) {
  17. n = 0;
  18. cb = ctx(mapFn, arguments[2], 2);
  19. forOf(source, false, function (nextItem) {
  20. A.push(cb(nextItem, n++));
  21. });
  22. } else {
  23. forOf(source, false, A.push, A);
  24. }
  25. return new this(A);
  26. } });
  27. };