Skip to content

Commit

Permalink
feat(LinkedQueue): implement iteration protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
slikts committed Jan 24, 2023
1 parent 3d75244 commit 68799d7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/LinkedQueue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,11 @@ describe('LinkedQueue', () => {
q.forEach(() => void (n += 1));
expect(n).toBe(2);
});

it('iterates', () => {
const q = new LinkedQueue();
q.enqueue(1);
q.enqueue(2);
expect([...q]).toEqual([1, 2]);
});
});
18 changes: 18 additions & 0 deletions src/LinkedQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,22 @@ export default class LinkedQueue<A> {
forEach(f: (value: A) => void): void {
this.#list.forEach(f);
}

[Symbol.iterator]() {
return {
next: () => {
return this.length > 0
? {
value: this.dequeue(),
done: false,
}
: {
done: true,
};
},
[Symbol.iterator]() {
return this;
},
};
}
}

0 comments on commit 68799d7

Please sign in to comment.