Skip to content

Commit d38f8c0

Browse files
committed
wip(iterator.from): Attempting to fix Interator.from to work
1 parent 3c39749 commit d38f8c0

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import "./index";
44
import "../Iterator.prototype.map";
55
import "../Iterator.prototype.filter";
66

7+
(globalThis as Record<string, unknown>).Iterator &&
8+
delete (
9+
(globalThis as Record<string, unknown>).Iterator as Record<string, unknown>
10+
).from;
11+
712
describe("Iterator.from", () => {
813
it("can convert an Array to an Iterator", () => {
914
const array = [-3, 1, -4, 1, -5];
@@ -69,15 +74,15 @@ describe("Iterator.from", () => {
6974
const object = {
7075
values: [-3, 1, -4, 1, -5, 9],
7176
index: 0,
72-
next() {
77+
next(): IteratorResult<number, void> {
7378
if (this.index < this.values.length) {
7479
return { value: this.values[this.index++], done: false };
7580
}
7681
return { done: true, value: undefined };
7782
},
7883
};
7984
const iterator = Iterator.from(object)
80-
.map((x) => (x !== undefined ? x * 2 : 0))
85+
.map((x) => x * 2)
8186
.filter((x) => x > 0);
8287

8388
expect([...iterator]).toStrictEqual([2, 2, 18]);

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

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,31 @@ declare global {
1313

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

16-
(globalThis as Record<string, unknown>).Iterator = {
17-
from<T>(
16+
((globalThis as Record<string, unknown>).Iterator as { from: unknown }).from ??=
17+
function <T>(
1818
object: Iterator<T> | Iterable<T> | { next(): IteratorResult<T> },
1919
): IterableIterator<T> {
20-
if (typeof (object as Iterable<T>)[Symbol.iterator] === "function") {
21-
const iterable = object as Iterable<T>;
22-
const iterator = iterable[Symbol.iterator]();
23-
return iteratorPrototype.toIterable.call(iterator) as IterableIterator<T>;
20+
const toIterable = iteratorPrototype.toIterable as (
21+
this: Iterator<T>,
22+
) => IterableIterator<T>;
23+
24+
const iteratorFactory = (object as Iterable<T>)[Symbol.iterator];
25+
if (typeof iteratorFactory === "function") {
26+
return toIterable.call(iteratorFactory.call(object));
2427
}
2528

26-
if (Object.prototype.isPrototypeOf.call(iteratorPrototype, object)) {
27-
return iteratorPrototype.toIterable.call(
28-
object as Iterator<T>,
29-
) as IterableIterator<T>;
29+
// eslint-disable-next-line no-prototype-builtins
30+
if (iteratorPrototype.isPrototypeOf(object)) {
31+
return toIterable.call(object as Iterator<T>);
3032
}
3133

32-
if (typeof (object as { next(): IteratorResult<T> }).next === "function") {
33-
return (function* (): IterableIterator<T> {
34-
yield* iteratorPrototype.toIterable.call(
35-
object as Iterator<T>,
36-
) as Iterable<T>;
34+
if (typeof (object as Record<string, unknown>).next === "function") {
35+
return (function* () {
36+
yield* toIterable.call(object as Iterator<T>);
3737
})();
3838
}
3939

4040
throw new TypeError(
4141
"Object is not an iterator, iterable, or an object with a next method",
4242
);
43-
},
44-
};
43+
};

0 commit comments

Comments
 (0)