object-iterator.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. 'use strict';
  2. var InternalStateModule = require('../internals/internal-state');
  3. var createIteratorConstructor = require('../internals/iterator-create-constructor');
  4. var createIterResultObject = require('../internals/create-iter-result-object');
  5. var hasOwn = require('../internals/has-own-property');
  6. var objectKeys = require('../internals/object-keys');
  7. var toObject = require('../internals/to-object');
  8. var OBJECT_ITERATOR = 'Object Iterator';
  9. var setInternalState = InternalStateModule.set;
  10. var getInternalState = InternalStateModule.getterFor(OBJECT_ITERATOR);
  11. module.exports = createIteratorConstructor(function ObjectIterator(source, mode) {
  12. var object = toObject(source);
  13. setInternalState(this, {
  14. type: OBJECT_ITERATOR,
  15. mode: mode,
  16. object: object,
  17. keys: objectKeys(object),
  18. index: 0
  19. });
  20. }, 'Object', function next() {
  21. var state = getInternalState(this);
  22. var keys = state.keys;
  23. while (true) {
  24. if (keys === null || state.index >= keys.length) {
  25. state.object = state.keys = null;
  26. return createIterResultObject(undefined, true);
  27. }
  28. var key = keys[state.index++];
  29. var object = state.object;
  30. if (!hasOwn(object, key)) continue;
  31. switch (state.mode) {
  32. case 'keys': return createIterResultObject(key, false);
  33. case 'values': return createIterResultObject(object[key], false);
  34. } /* entries */ return createIterResultObject([key, object[key]], false);
  35. }
  36. });