2
0

array.js 1.0 KB

123456789101112131415161718192021222324252627282930
  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. , Iterator = require('./')
  6. , defineProperty = Object.defineProperty
  7. , ArrayIterator;
  8. ArrayIterator = module.exports = function (arr, kind) {
  9. if (!(this instanceof ArrayIterator)) return new ArrayIterator(arr, kind);
  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. ArrayIterator.prototype = Object.create(Iterator.prototype, {
  19. constructor: d(ArrayIterator),
  20. _resolve: d(function (i) {
  21. if (this.__kind__ === 'value') return this.__list__[i];
  22. if (this.__kind__ === 'key+value') return [i, this.__list__[i]];
  23. return i;
  24. }),
  25. toString: d(function () { return '[object Array Iterator]'; })
  26. });