GetIteratorFlattenable.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. var AdvanceStringIndex = require('es-abstract/2023/AdvanceStringIndex');
  5. var Call = require('es-abstract/2023/Call');
  6. var GetIteratorDirect = require('./GetIteratorDirect');
  7. var GetMethod = require('es-abstract/2023/GetMethod');
  8. var IsArray = require('es-abstract/2023/IsArray');
  9. var Type = require('es-abstract/2023/Type');
  10. var getIteratorMethod = require('es-abstract/helpers/getIteratorMethod');
  11. module.exports = function GetIteratorFlattenable(obj, stringHandling) {
  12. if (Type(obj) !== 'Object') {
  13. if (stringHandling === 'reject-strings' || typeof obj !== 'string') {
  14. throw new $TypeError('obj must be an Object'); // step 1.a
  15. }
  16. }
  17. var method = void undefined; // step 2
  18. // method = GetMethod(obj, Symbol.iterator); // step 5.a
  19. method = getIteratorMethod(
  20. {
  21. AdvanceStringIndex: AdvanceStringIndex,
  22. GetMethod: GetMethod,
  23. IsArray: IsArray
  24. },
  25. obj
  26. );
  27. var iterator;
  28. if (typeof method === 'undefined') { // step 3
  29. iterator = obj; // step 3.a
  30. } else { // step 4
  31. iterator = Call(method, obj); // step 4.a
  32. }
  33. if (Type(iterator) !== 'Object') {
  34. throw new $TypeError('iterator must be an Object'); // step 5
  35. }
  36. return GetIteratorDirect(iterator); // step 6
  37. };