Skip to content

Commit

Permalink
Merge pull request #33 from jsr-core/fix-queue
Browse files Browse the repository at this point in the history
fix: allow falsy values and null in Queue and Stack
  • Loading branch information
lambdalisue authored Aug 16, 2024
2 parents 7db916b + e2116f7 commit d4eb54f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 7 deletions.
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);
});

0 comments on commit d4eb54f

Please sign in to comment.