GetIteratorFlattenable.js 1.2 KB

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