esnext.iterator.from.js 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. // https://github.com/tc39/proposal-iterator-helpers
  2. var $ = require('../internals/export');
  3. var path = require('../internals/path');
  4. var aFunction = require('../internals/a-function');
  5. var anObject = require('../internals/an-object');
  6. var toObject = require('../internals/to-object');
  7. var createIteratorProxy = require('../internals/iterator-create-proxy');
  8. var getIteratorMethod = require('../internals/get-iterator-method');
  9. var Iterator = path.Iterator;
  10. var IteratorProxy = createIteratorProxy(function (arg) {
  11. var result = anObject(this.next.call(this.iterator, arg));
  12. var done = this.done = !!result.done;
  13. if (!done) return result.value;
  14. }, true);
  15. $({ target: 'Iterator', stat: true }, {
  16. from: function from(O) {
  17. var object = toObject(O);
  18. var usingIterator = getIteratorMethod(object);
  19. var iterator;
  20. if (usingIterator != null) {
  21. iterator = aFunction(usingIterator).call(object);
  22. if (iterator instanceof Iterator) return iterator;
  23. } else {
  24. iterator = object;
  25. } return new IteratorProxy({
  26. iterator: iterator
  27. });
  28. }
  29. });