esnext.iterator.from.js 1.2 KB

123456789101112131415161718192021222324252627282930
  1. // https://github.com/tc39/proposal-iterator-helpers
  2. var $ = require('../internals/export');
  3. var apply = require('../internals/function-apply');
  4. var anObject = require('../internals/an-object');
  5. var toObject = require('../internals/to-object');
  6. var isPrototypeOf = require('../internals/object-is-prototype-of');
  7. var IteratorPrototype = require('../internals/iterators-core').IteratorPrototype;
  8. var createIteratorProxy = require('../internals/iterator-create-proxy');
  9. var getIterator = require('../internals/get-iterator');
  10. var getIteratorMethod = require('../internals/get-iterator-method');
  11. var IteratorProxy = createIteratorProxy(function (args) {
  12. var result = anObject(apply(this.next, this.iterator, args));
  13. var done = this.done = !!result.done;
  14. if (!done) return result.value;
  15. }, true);
  16. $({ target: 'Iterator', stat: true, forced: true }, {
  17. from: function from(O) {
  18. var object = toObject(O);
  19. var usingIterator = getIteratorMethod(object);
  20. var iterator;
  21. if (usingIterator) {
  22. iterator = getIterator(object, usingIterator);
  23. if (isPrototypeOf(IteratorPrototype, iterator)) return iterator;
  24. } else {
  25. iterator = object;
  26. } return new IteratorProxy({ iterator: iterator });
  27. }
  28. });