array.js 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. "use strict";
  2. var setPrototypeOf = require("es5-ext/object/set-prototype-of")
  3. , contains = require("es5-ext/string/#/contains")
  4. , d = require("d")
  5. , Symbol = require("es6-symbol")
  6. , Iterator = require("./");
  7. var defineProperty = Object.defineProperty, ArrayIterator;
  8. ArrayIterator = module.exports = function (arr, kind) {
  9. if (!(this instanceof ArrayIterator)) throw new TypeError("Constructor requires 'new'");
  10. Iterator.call(this, arr);
  11. if (!kind) kind = "value";
  12. else if (contains.call(kind, "key+value")) kind = "key+value";
  13. else if (contains.call(kind, "key")) kind = "key";
  14. else kind = "value";
  15. defineProperty(this, "__kind__", d("", kind));
  16. };
  17. if (setPrototypeOf) setPrototypeOf(ArrayIterator, Iterator);
  18. // Internal %ArrayIteratorPrototype% doesn't expose its constructor
  19. delete ArrayIterator.prototype.constructor;
  20. ArrayIterator.prototype = Object.create(Iterator.prototype, {
  21. _resolve: d(function (i) {
  22. if (this.__kind__ === "value") return this.__list__[i];
  23. if (this.__kind__ === "key+value") return [i, this.__list__[i]];
  24. return i;
  25. })
  26. });
  27. defineProperty(ArrayIterator.prototype, Symbol.toStringTag, d("c", "Array Iterator"));