es.iterator.from.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var call = require('../internals/function-call');
  4. var toObject = require('../internals/to-object');
  5. var isPrototypeOf = require('../internals/object-is-prototype-of');
  6. var IteratorPrototype = require('../internals/iterators-core').IteratorPrototype;
  7. var createIteratorProxy = require('../internals/iterator-create-proxy');
  8. var getIteratorFlattenable = require('../internals/get-iterator-flattenable');
  9. var IS_PURE = require('../internals/is-pure');
  10. var FORCED = IS_PURE || function () {
  11. // Should not throw when an underlying iterator's `return` method is null
  12. // https://bugs.webkit.org/show_bug.cgi?id=288714
  13. try {
  14. // eslint-disable-next-line es/no-iterator -- required for testing
  15. Iterator.from({ 'return': null })['return']();
  16. } catch (error) {
  17. return true;
  18. }
  19. }();
  20. var IteratorProxy = createIteratorProxy(function () {
  21. return call(this.next, this.iterator);
  22. }, true);
  23. // `Iterator.from` method
  24. // https://tc39.es/ecma262/#sec-iterator.from
  25. $({ target: 'Iterator', stat: true, forced: FORCED }, {
  26. from: function from(O) {
  27. var iteratorRecord = getIteratorFlattenable(typeof O == 'string' ? toObject(O) : O, true);
  28. return isPrototypeOf(IteratorPrototype, iteratorRecord.iterator)
  29. ? iteratorRecord.iterator
  30. : new IteratorProxy(iteratorRecord);
  31. }
  32. });