iterator.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = createIterator;
  6. var _isArrayLike = require('./isArrayLike.js');
  7. var _isArrayLike2 = _interopRequireDefault(_isArrayLike);
  8. var _getIterator = require('./getIterator.js');
  9. var _getIterator2 = _interopRequireDefault(_getIterator);
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. function createArrayIterator(coll) {
  12. var i = -1;
  13. var len = coll.length;
  14. return function next() {
  15. return ++i < len ? { value: coll[i], key: i } : null;
  16. };
  17. }
  18. function createES2015Iterator(iterator) {
  19. var i = -1;
  20. return function next() {
  21. var item = iterator.next();
  22. if (item.done) return null;
  23. i++;
  24. return { value: item.value, key: i };
  25. };
  26. }
  27. function createObjectIterator(obj) {
  28. var okeys = obj ? Object.keys(obj) : [];
  29. var i = -1;
  30. var len = okeys.length;
  31. return function next() {
  32. var key = okeys[++i];
  33. if (key === '__proto__') {
  34. return next();
  35. }
  36. return i < len ? { value: obj[key], key } : null;
  37. };
  38. }
  39. function createIterator(coll) {
  40. if ((0, _isArrayLike2.default)(coll)) {
  41. return createArrayIterator(coll);
  42. }
  43. var iterator = (0, _getIterator2.default)(coll);
  44. return iterator ? createES2015Iterator(iterator) : createObjectIterator(coll);
  45. }
  46. module.exports = exports['default'];