esnext.iterator.zip-keyed.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var anObject = require('../internals/an-object');
  4. var anObjectOrUndefined = require('../internals/an-object-or-undefined');
  5. var createProperty = require('../internals/create-property');
  6. var call = require('../internals/function-call');
  7. var uncurryThis = require('../internals/function-uncurry-this');
  8. var getBuiltIn = require('../internals/get-built-in');
  9. var propertyIsEnumerableModule = require('../internals/object-property-is-enumerable');
  10. var getIteratorFlattenable = require('../internals/get-iterator-flattenable');
  11. var getModeOption = require('../internals/get-mode-option');
  12. var iteratorCloseAll = require('../internals/iterator-close-all');
  13. var iteratorZip = require('../internals/iterator-zip');
  14. var IS_PURE = require('../internals/is-pure');
  15. var create = getBuiltIn('Object', 'create');
  16. var ownKeys = getBuiltIn('Reflect', 'ownKeys');
  17. var push = uncurryThis([].push);
  18. var THROW = 'throw';
  19. // `Iterator.zipKeyed` method
  20. // https://github.com/tc39/proposal-joint-iteration
  21. $({ target: 'Iterator', stat: true, forced: IS_PURE }, {
  22. zipKeyed: function zipKeyed(iterables /* , options */) {
  23. anObject(iterables);
  24. var options = arguments.length > 1 ? anObjectOrUndefined(arguments[1]) : undefined;
  25. var mode = getModeOption(options);
  26. var paddingOption = mode === 'longest' ? anObjectOrUndefined(options && options.padding) : undefined;
  27. var iters = [];
  28. var padding = [];
  29. var allKeys = ownKeys(iterables);
  30. var keys = [];
  31. var propertyIsEnumerable = propertyIsEnumerableModule.f;
  32. var i, key, value;
  33. for (i = 0; i < allKeys.length; i++) try {
  34. key = allKeys[i];
  35. if (!call(propertyIsEnumerable, iterables, key)) continue;
  36. value = iterables[key];
  37. if (value !== undefined) {
  38. push(keys, key);
  39. push(iters, getIteratorFlattenable(value, false));
  40. }
  41. } catch (error) {
  42. return iteratorCloseAll(iters, THROW, error);
  43. }
  44. var iterCount = iters.length;
  45. if (mode === 'longest') {
  46. if (paddingOption === undefined) {
  47. for (i = 0; i < iterCount; i++) push(padding, undefined);
  48. } else {
  49. for (i = 0; i < keys.length; i++) {
  50. try {
  51. value = paddingOption[keys[i]];
  52. } catch (error) {
  53. return iteratorCloseAll(iters, THROW, error);
  54. }
  55. push(padding, value);
  56. }
  57. }
  58. }
  59. return iteratorZip(iters, mode, padding, function (results) {
  60. var obj = create(null);
  61. for (var j = 0; j < iterCount; j++) {
  62. createProperty(obj, keys[j], results[j]);
  63. }
  64. return obj;
  65. });
  66. }
  67. });