Skip to content

Commit 8ece5e5

Browse files
committed
Change iter methods to the JS-idiomatic [Symbol.iterator]
1 parent afad8b3 commit 8ece5e5

File tree

10 files changed

+26
-30
lines changed

10 files changed

+26
-30
lines changed

src/Option/None.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ export class None<T = never> extends OptionBase<T> {
5555
return err(error);
5656
}
5757

58-
iter(): IteratorObject<T, unknown, unknown> {
59-
return Iterator.from([]);
60-
}
58+
*[Symbol.iterator](): Iterator<T> {}
6159

6260
and<U>(_optionB: Option<U>): None<never> {
6361
return none();

src/Option/OptionBase.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export abstract class OptionBase<T> {
9797
* Return an iterator over the {@link Some} value.
9898
* @returns A JavaScript iterator over 0 or 1 elements.
9999
*/
100-
abstract iter(): IteratorObject<T, unknown, unknown>;
100+
abstract [Symbol.iterator](): Iterator<T>;
101101

102102
/**
103103
* Return `optionB` if {@link Some}, otherwise `None`.

src/Option/Some.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ export class Some<T> implements OptionBase<T> {
5959
return ok(this.value);
6060
}
6161

62-
iter(): IteratorObject<T, unknown, unknown> {
63-
return Iterator.from([this.value]);
62+
*[Symbol.iterator](): Iterator<T> {
63+
yield this.value;
6464
}
6565

6666
and<U>(optionB: Option<U>): Option<U> {

src/Result/Err.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ export class Err<T = never, E = unknown> extends ResultBase<T, E> {
6060
return err(this.error);
6161
}
6262

63-
iter(): IteratorObject<T, unknown, unknown> {
64-
return Iterator.from([] as T[]);
65-
}
63+
*[Symbol.iterator](): Iterator<T> {}
6664

6765
unwrap(message: string = `Expected ok() but got ${this}`): never {
6866
throw new Error(message);

src/Result/Ok.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ export class Ok<T, E = never> extends ResultBase<T, E> {
6060
return this;
6161
}
6262

63-
iter(): IteratorObject<T, unknown, unknown> {
64-
return Iterator.from([this.value]);
63+
*[Symbol.iterator](): Iterator<T> {
64+
yield this.value;
6565
}
6666

6767
unwrap(_message: string = `Expected ok() but got ${this}`): T {

src/Result/ResultBase.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export abstract class ResultBase<T, E = unknown> {
9292
* Get an iterator over the `Ok` value.
9393
* @returns An iterator yielding the `Ok` value or nothing if `Err`.
9494
*/
95-
abstract iter(): IteratorObject<T, unknown, unknown>;
95+
abstract [Symbol.iterator](): Iterator<T>;
9696

9797
/**
9898
* Unwrap the `Ok` value, or throw an error if `Err`.

tests/option_none_test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ Deno.test("none().okOr(n) returns err(n)", () => {
7373
expect(result.unwrapErr()).toBe("problem");
7474
});
7575

76-
Deno.test("none().iter() returns an empty iterator", () => {
77-
const collected = [...none().iter()];
76+
Deno.test("iterating over none().iter() yields nothing", () => {
77+
const collected = [...none()];
7878
expect(collected.length).toBe(0);
7979
});
8080

tests/option_some_test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ Deno.test("some(n).okOr() returns ok(n)", () => {
7676
expect(result.unwrap()).toBe(42);
7777
});
7878

79-
Deno.test("some(n).iter() returns an iterator over [n]", () => {
80-
const collected = [...some(42).iter()];
79+
Deno.test("iterating over some(n).iter() yields precisely n", () => {
80+
const collected = [...some(42)];
8181
expect(collected.length).toBe(1);
8282
expect(collected[0]).toBe(42);
8383
});

tests/result_err_test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,15 @@ Deno.test("ok.inspectErr() calls its callback", () => {
8383
expect(called).toBe(true);
8484
});
8585

86-
// Deno.test({
87-
// name: "ok(n).iter() gives an iterator containing nothing",
88-
// fn: () => {
89-
// const result = err("problem").iter();
90-
91-
// const collected = [...result];
92-
// expect(collected.length).toBe(0);
93-
// },
94-
// });
86+
Deno.test({
87+
name: "iterating over err(e) yields nothing",
88+
fn: () => {
89+
const result = err("problem");
90+
91+
const collected = [...result];
92+
expect(collected.length).toBe(0);
93+
},
94+
});
9595

9696
Deno.test("err().unwrap() throws", () => {
9797
expect(() => err("problem").unwrap()).toThrow();

tests/result_ok_test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ Deno.test("ok.inspectErr() does not call its callback", () => {
8282
expect(called).toBe(false);
8383
});
8484

85-
// Deno.test("ok(n).iter() gives an iterator containing precisely n", () => {
86-
// const collected = [...ok(42).iter()];
87-
// expect(collected.length).toBe(1);
88-
// expect(collected[0]).toBe(42);
89-
// });
85+
Deno.test("iterating over ok(n) yields precisely n", () => {
86+
const collected = [...ok(42)];
87+
expect(collected.length).toBe(1);
88+
expect(collected[0]).toBe(42);
89+
});
9090

9191
Deno.test("ok(n).unwrap() returns n", () => {
9292
expect(ok(42).unwrap()).toBe(42);

0 commit comments

Comments
 (0)