addon-sdk/source/lib/sdk/util/iteration.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/lib/sdk/util/iteration.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,33 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +'use strict';
     1.8 +
     1.9 +module.metadata = {
    1.10 +  "stability": "experimental"
    1.11 +};
    1.12 +
    1.13 +// This is known as @@iterator in the ES6 spec.  Until it is bound to
    1.14 +// some well-known name, find the @@iterator object by expecting it as
    1.15 +// the first property accessed on a for-of iterable.
    1.16 +const iteratorSymbol = (function() {
    1.17 +  try {
    1.18 +    for (var _ of Proxy.create({get: function(_, name) { throw name; } }))
    1.19 +      break;
    1.20 +  } catch (name) {
    1.21 +    return name;
    1.22 +  }
    1.23 +  throw new TypeError;
    1.24 +})();
    1.25 +
    1.26 +exports.iteratorSymbol = iteratorSymbol;
    1.27 +
    1.28 +// An adaptor that, given an object that is iterable with for-of, is
    1.29 +// suitable for being bound to __iterator__ in order to make the object
    1.30 +// iterable in the same way via for-in.
    1.31 +function forInIterator() {
    1.32 +    for (let item of this)
    1.33 +        yield item;
    1.34 +}
    1.35 +
    1.36 +exports.forInIterator = forInIterator;

mercurial