Skip to content

Commit 0b65684

Browse files
committed
wip(iterator from test): Added jest test to Iterator.from (snapshots failing)
1 parent 0894b8c commit 0b65684

File tree

4 files changed

+98
-8130
lines changed

4 files changed

+98
-8130
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { describe, expect, it } from "@jest/globals";
2+
3+
import "./index";
4+
5+
describe("Iterator.from", () => {
6+
it("can convert an Array to an Iterator", () => {
7+
const array = [1, 2, 3];
8+
const iterator = Iterator.from(array);
9+
10+
expect([...iterator]).toStrictEqual(array);
11+
});
12+
13+
it("can convert a Set to an Iterator", () => {
14+
const set = new Set([1, 2, 3]);
15+
const iterator = Iterator.from(set);
16+
17+
expect([...iterator]).toStrictEqual([...set]);
18+
});
19+
20+
it("can convert a Map to an Iterator", () => {
21+
const map = new Map([
22+
["a", 1],
23+
["b", 2],
24+
["c", 3],
25+
]);
26+
const iterator = Iterator.from(map);
27+
expect([...iterator]).toStrictEqual([...map.entries()]);
28+
});
29+
30+
it("can convert a Generator Object to an Iterator", () => {
31+
const factory = function* (): Generator<number, void, void> {
32+
yield 3;
33+
yield 1;
34+
yield 4;
35+
yield 1;
36+
yield 5;
37+
};
38+
const iterator = Iterator.from(factory());
39+
40+
expect([...iterator]).toStrictEqual([3, 1, 4, 1, 5]);
41+
});
42+
43+
it("can convert a String to an Iterator", () => {
44+
const string = "🚀💯🔥";
45+
const iterator = Iterator.from(string);
46+
47+
expect([...iterator]).toStrictEqual([...string]);
48+
});
49+
50+
it("can convert an Iterator to an Iterator", () => {
51+
const array = [1, 2, 3];
52+
const iterator = Iterator.from(array.values());
53+
const iterator2 = Iterator.from(iterator);
54+
55+
expect([...iterator2]).toStrictEqual(array);
56+
});
57+
58+
it("can convert an IterableIterator to an Iterator", () => {
59+
const array = [1, 2, 3];
60+
const iterator = Iterator.from(array.values());
61+
const iterator2 = Iterator.from(iterator[Symbol.iterator]());
62+
63+
expect([...iterator2]).toStrictEqual(array);
64+
});
65+
66+
it("can convert an object with a random next() method to an Iterator", () => {
67+
const object = {
68+
values: [1, 2, 3],
69+
index: 0,
70+
next() {
71+
if (this.index < this.values.length) {
72+
return { value: this.values[this.index++], done: false };
73+
}
74+
return { done: true, value: undefined };
75+
},
76+
};
77+
const iterator = Iterator.from(object);
78+
79+
expect([...iterator]).toStrictEqual([1, 2, 3]);
80+
});
81+
82+
it("throws an error if the object is not an IterableIterator", () => {
83+
const object = {};
84+
// @ts-expect-error Invalid argument
85+
expect(() => Iterator.from(object)).toThrow(TypeError);
86+
});
87+
});

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,25 @@ declare global {
1919
).from ??= function <T>(
2020
object: Iterator<T> | Iterable<T> | { next(): IteratorResult<T> },
2121
): IterableIterator<T> {
22-
if (Symbol.iterator in object) {
23-
return object as IterableIterator<T>;
24-
} else if (
25-
"toIterable" in object &&
26-
typeof (object as Iterator<T>).toIterable === "function"
27-
) {
22+
if (typeof (object as Iterable<T>)[Symbol.iterator] === "function") {
23+
return (object as Iterable<T>)[Symbol.iterator]() as IterableIterator<T>;
24+
}
25+
26+
if (typeof (object as Iterator<T>).toIterable === "function") {
2827
return (object as Iterator<T>).toIterable();
29-
} else {
28+
}
29+
30+
if (typeof (object as { next(): IteratorResult<T> }).next === "function") {
3031
return (function* () {
3132
let result: IteratorResult<T>;
3233
do {
33-
result = object.next();
34+
result = (object as { next(): IteratorResult<T> }).next();
3435
if (!result.done) {
3536
yield result.value;
3637
}
3738
} while (!result.done);
3839
})();
3940
}
41+
42+
throw new TypeError("Object is not an Iterator or Iterable");
4043
};

0 commit comments

Comments
 (0)