iterable-to-iterator-fallback.js (1022B)
1 // Copyright (C) 2023 Michael Ficarra. All rights reserved. 2 // This code is governed by the BSD license found in the LICENSE file. 3 /*--- 4 esid: sec-iterator.from 5 description: > 6 Iterator.from falls back to treating its parameter as an iterator if the Symbol.iterator property is null/undefined 7 info: | 8 Iterator.from ( O ) 9 10 includes: [compareArray.js] 11 features: [iterator-helpers] 12 flags: [] 13 ---*/ 14 15 function* g() { 16 yield 0; 17 yield 1; 18 yield 2; 19 } 20 21 let iter = (function () { 22 let n = g(); 23 return { 24 [Symbol.iterator]: 0, 25 next: () => n.next(), 26 }; 27 })(); 28 29 assert.throws(TypeError, function () { 30 Iterator.from(iter); 31 }); 32 33 iter = (function () { 34 let n = g(); 35 return { 36 [Symbol.iterator]: null, 37 next: () => n.next(), 38 }; 39 })(); 40 41 assert.compareArray(Array.from(Iterator.from(iter)), [0, 1, 2]); 42 43 iter = (function () { 44 let n = g(); 45 return { 46 [Symbol.iterator]: undefined, 47 next: () => n.next(), 48 }; 49 })(); 50 51 assert.compareArray(Array.from(Iterator.from(iter)), [0, 1, 2]); 52 53 reportCompare(0, 0);