Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow falsy values and null in Queue and Stack #33

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { Notify } from "./notify.ts";
* assertEquals(await queue.pop(), 3);
* ```
*/
export class Queue<T> {
export class Queue<T extends NonNullable<unknown> | null> {
#notify = new Notify();
#items: T[] = [];

Expand Down Expand Up @@ -52,7 +52,7 @@ export class Queue<T> {
while (true) {
signal?.throwIfAborted();
const value = this.#items.shift();
if (value) {
if (value !== undefined) {
return value;
}
await this.#notify.notified({ signal });
Expand Down
18 changes: 18 additions & 0 deletions queue_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,21 @@ test("Queue 'pop' with signal already aborted", async () => {
"Aborted",
);
});

test("Queue with falsy value is accepted", async () => {
const q = new Queue<number>();
const popper = q.pop();
assertEquals(await promiseState(popper), "pending");
q.push(0);
assertEquals(await promiseState(popper), "fulfilled");
assertEquals(await popper, 0);
});

test("Queue with null is accepted", async () => {
const q = new Queue<null>();
const popper = q.pop();
assertEquals(await promiseState(popper), "pending");
q.push(null);
assertEquals(await promiseState(popper), "fulfilled");
assertEquals(await popper, null);
});
4 changes: 2 additions & 2 deletions stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Notify } from "./notify.ts";
*
* @template T The type of items in the stack.
*/
export class Stack<T> {
export class Stack<T extends NonNullable<unknown> | null> {
#notify = new Notify();
#items: T[] = [];

Expand Down Expand Up @@ -56,7 +56,7 @@ export class Stack<T> {
while (true) {
signal?.throwIfAborted();
const value = this.#items.pop();
if (value) {
if (value !== undefined) {
return value;
}
await this.#notify.notified({ signal });
Expand Down
18 changes: 18 additions & 0 deletions stack_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,21 @@ test("Stack 'pop' with signal already aborted", async () => {
"Aborted",
);
});

test("Stack with falsy value is accepted", async () => {
const q = new Stack<number>();
const popper = q.pop();
assertEquals(await promiseState(popper), "pending");
q.push(0);
assertEquals(await promiseState(popper), "fulfilled");
assertEquals(await popper, 0);
});

test("Stack with null is accepted", async () => {
const q = new Stack<null>();
const popper = q.pop();
assertEquals(await promiseState(popper), "pending");
q.push(null);
assertEquals(await promiseState(popper), "fulfilled");
assertEquals(await popper, null);
});
Loading