GetIteratorFlattenable.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. 'use strict';
  2. var $TypeError = require('es-errors/type');
  3. var AdvanceStringIndex = require('es-abstract/2024/AdvanceStringIndex');
  4. var Call = require('es-abstract/2024/Call');
  5. var GetIteratorDirect = require('./GetIteratorDirect');
  6. var GetMethod = require('es-abstract/2024/GetMethod');
  7. var IsArray = require('es-abstract/2024/IsArray');
  8. var Type = require('es-abstract/2024/Type');
  9. var getIteratorMethod = require('es-abstract/helpers/getIteratorMethod');
  10. // https://tc39.es/proposal-iterator-helpers/#sec-getiteratorflattenable
  11. module.exports = function GetIteratorFlattenable(obj, stringHandling) {
  12. if (stringHandling !== 'REJECT-STRINGS' && stringHandling !== 'ITERATE-STRINGS') {
  13. throw new $TypeError('Assertion failed: `stringHandling` must be "REJECT-STRINGS" or "ITERATE-STRINGS"');
  14. }
  15. if (Type(obj) !== 'Object') {
  16. if (stringHandling === 'REJECT-STRINGS' || typeof obj !== 'string') {
  17. throw new $TypeError('obj must be an Object'); // step 1.a
  18. }
  19. }
  20. var method = void undefined; // step 2
  21. // method = GetMethod(obj, Symbol.iterator); // step 5.a
  22. method = getIteratorMethod(
  23. {
  24. AdvanceStringIndex: AdvanceStringIndex,
  25. GetMethod: GetMethod,
  26. IsArray: IsArray
  27. },
  28. obj
  29. );
  30. var iterator;
  31. if (typeof method === 'undefined') { // step 3
  32. iterator = obj; // step 3.a
  33. } else { // step 4
  34. iterator = Call(method, obj); // step 4.a
  35. }
  36. if (Type(iterator) !== 'Object') {
  37. throw new $TypeError('iterator must be an Object'); // step 5
  38. }
  39. return GetIteratorDirect(iterator); // step 6
  40. };