Skip to content

Commit d4eb54f

Browse files
authored
Merge pull request #33 from jsr-core/fix-queue
fix: allow falsy values and null in Queue and Stack
2 parents 7db916b + e2116f7 commit d4eb54f

File tree

5 files changed

+43
-7
lines changed

5 files changed

+43
-7
lines changed

deno.lock

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

queue.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { Notify } from "./notify.ts";
1717
* assertEquals(await queue.pop(), 3);
1818
* ```
1919
*/
20-
export class Queue<T> {
20+
export class Queue<T extends NonNullable<unknown> | null> {
2121
#notify = new Notify();
2222
#items: T[] = [];
2323

@@ -52,7 +52,7 @@ export class Queue<T> {
5252
while (true) {
5353
signal?.throwIfAborted();
5454
const value = this.#items.shift();
55-
if (value) {
55+
if (value !== undefined) {
5656
return value;
5757
}
5858
await this.#notify.notified({ signal });

queue_test.ts

+18
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,21 @@ test("Queue 'pop' with signal already aborted", async () => {
5757
"Aborted",
5858
);
5959
});
60+
61+
test("Queue with falsy value is accepted", async () => {
62+
const q = new Queue<number>();
63+
const popper = q.pop();
64+
assertEquals(await promiseState(popper), "pending");
65+
q.push(0);
66+
assertEquals(await promiseState(popper), "fulfilled");
67+
assertEquals(await popper, 0);
68+
});
69+
70+
test("Queue with null is accepted", async () => {
71+
const q = new Queue<null>();
72+
const popper = q.pop();
73+
assertEquals(await promiseState(popper), "pending");
74+
q.push(null);
75+
assertEquals(await promiseState(popper), "fulfilled");
76+
assertEquals(await popper, null);
77+
});

stack.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { Notify } from "./notify.ts";
1919
*
2020
* @template T The type of items in the stack.
2121
*/
22-
export class Stack<T> {
22+
export class Stack<T extends NonNullable<unknown> | null> {
2323
#notify = new Notify();
2424
#items: T[] = [];
2525

@@ -56,7 +56,7 @@ export class Stack<T> {
5656
while (true) {
5757
signal?.throwIfAborted();
5858
const value = this.#items.pop();
59-
if (value) {
59+
if (value !== undefined) {
6060
return value;
6161
}
6262
await this.#notify.notified({ signal });

stack_test.ts

+18
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,21 @@ test("Stack 'pop' with signal already aborted", async () => {
5757
"Aborted",
5858
);
5959
});
60+
61+
test("Stack with falsy value is accepted", async () => {
62+
const q = new Stack<number>();
63+
const popper = q.pop();
64+
assertEquals(await promiseState(popper), "pending");
65+
q.push(0);
66+
assertEquals(await promiseState(popper), "fulfilled");
67+
assertEquals(await popper, 0);
68+
});
69+
70+
test("Stack with null is accepted", async () => {
71+
const q = new Stack<null>();
72+
const popper = q.pop();
73+
assertEquals(await promiseState(popper), "pending");
74+
q.push(null);
75+
assertEquals(await promiseState(popper), "fulfilled");
76+
assertEquals(await popper, null);
77+
});

0 commit comments

Comments
 (0)