iteration.js 1010 B

123456789101112131415161718192021222324252627282930313233
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. 'use strict';
  5. module.metadata = {
  6. "stability": "experimental"
  7. };
  8. // This is known as @@iterator in the ES6 spec. Until it is bound to
  9. // some well-known name, find the @@iterator object by expecting it as
  10. // the first property accessed on a for-of iterable.
  11. const iteratorSymbol = (function() {
  12. try {
  13. for (var _ of Proxy.create({get: function(_, name) { throw name; } }))
  14. break;
  15. } catch (name) {
  16. return name;
  17. }
  18. throw new TypeError;
  19. })();
  20. exports.iteratorSymbol = iteratorSymbol;
  21. // An adaptor that, given an object that is iterable with for-of, is
  22. // suitable for being bound to __iterator__ in order to make the object
  23. // iterable in the same way via for-in.
  24. function forInIterator() {
  25. for (let item of this)
  26. yield item;
  27. }
  28. exports.forInIterator = forInIterator;