Skip to content

Commit 319cf8b

Browse files
committed
chore(pr): Applying 1st round of PR feedback
1 parent 51bc4bf commit 319cf8b

File tree

4 files changed

+8416
-34
lines changed

4 files changed

+8416
-34
lines changed

workspaces/adventure-pack/goodies/typescript/Iterator.from/index.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ describe("Iterator.from", () => {
4747
expect([...iterator]).toStrictEqual([...string]);
4848
});
4949

50-
it("can convert an Iterator to an Iterator", () => {
50+
it("can convert an Iterator to an IterableIterator without altering the original iterator", () => {
5151
const array = [1, 2, 3];
52-
const iterator = Iterator.from(array.values());
53-
const iterator2 = Iterator.from(iterator);
52+
const iterator = array.values();
53+
const convertedIterator = Iterator.from(iterator);
5454

55-
expect([...iterator2]).toStrictEqual(array);
55+
expect(iterator === convertedIterator).toBe(true);
5656
});
5757

5858
it("can convert an IterableIterator to an Iterator", () => {
@@ -79,7 +79,7 @@ describe("Iterator.from", () => {
7979
expect([...iterator]).toStrictEqual([1, 2, 3]);
8080
});
8181

82-
it("throws an error if the object is not an IterableIterator", () => {
82+
it("throws an error if the object is not an Iterator, Iterable, or an object with a next method", () => {
8383
const object = {};
8484
// @ts-expect-error Invalid argument
8585
expect(() => Iterator.from(object)).toThrow(TypeError);
Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,54 @@
11
import "../Iterator.prototype.toIterable";
22

33
declare global {
4-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
5-
class Iterator<T> {
6-
static from<TElem>(
7-
object:
8-
| Iterator<TElem>
9-
| Iterable<TElem>
10-
| { next(): IteratorResult<TElem> },
11-
): IterableIterator<TElem>;
4+
interface IteratorConstructor {
5+
from<T>(
6+
object: Iterator<T> | Iterable<T> | { next(): IteratorResult<T> },
7+
): IterableIterator<T>;
128
}
9+
10+
const Iterator: IteratorConstructor;
1311
}
1412

1513
(globalThis as Record<string, unknown>).Iterator ??= {};
1614

17-
(
18-
(globalThis as Record<string, unknown>).Iterator as Record<string, unknown>
19-
).from ??= function <T>(
20-
object: Iterator<T> | Iterable<T> | { next(): IteratorResult<T> },
21-
): IterableIterator<T> {
22-
if (typeof (object as Iterable<T>)[Symbol.iterator] === "function") {
23-
return (object as Iterable<T>)[Symbol.iterator]() as IterableIterator<T>;
24-
}
15+
(globalThis as Record<string, unknown>).Iterator = {
16+
from<T>(
17+
object: Iterator<T> | Iterable<T> | { next(): IteratorResult<T> },
18+
): IterableIterator<T> {
19+
if (typeof (object as Iterable<T>)[Symbol.iterator] === "function") {
20+
const iterator = (object as Iterable<T>)[Symbol.iterator]();
21+
if (typeof iterator === "object" && Symbol.iterator in iterator) {
22+
return iterator as IterableIterator<T>;
23+
} else {
24+
return {
25+
...iterator,
26+
[Symbol.iterator]() {
27+
return this;
28+
},
29+
} as IterableIterator<T>;
30+
}
31+
}
2532

26-
if (typeof (object as Iterator<T>).toIterable === "function") {
27-
return (object as Iterator<T>).toIterable();
28-
}
33+
if (typeof (object as Iterator<T>).toIterable === "function") {
34+
return (object as Iterator<T>).toIterable();
35+
}
2936

30-
if (typeof (object as { next(): IteratorResult<T> }).next === "function") {
31-
return (function* () {
32-
let result: IteratorResult<T>;
33-
do {
34-
result = (object as { next(): IteratorResult<T> }).next();
35-
if (!result.done) {
37+
if (typeof (object as { next(): IteratorResult<T> }).next === "function") {
38+
return (function* () {
39+
let result: IteratorResult<T>;
40+
while (true) {
41+
result = (object as { next(): IteratorResult<T> }).next();
42+
if (result.done) {
43+
break;
44+
}
3645
yield result.value;
3746
}
38-
} while (!result.done);
39-
})();
40-
}
47+
})();
48+
}
4149

42-
throw new TypeError("Object is not an Iterator or Iterable");
50+
throw new TypeError(
51+
"Object is not an iterator, iterable, or an object with a next method",
52+
);
53+
},
4354
};

0 commit comments

Comments
 (0)