File tree Expand file tree Collapse file tree 4 files changed +30
-0
lines changed Expand file tree Collapse file tree 4 files changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -47,6 +47,13 @@ export default class Queue<ValueType> implements Iterable<ValueType> {
47
47
*/
48
48
dequeue ( ) : ValueType | undefined ;
49
49
50
+ /**
51
+ Get the next value in the queue without removing it.
52
+
53
+ @returns The value or `undefined` if the queue is empty.
54
+ */
55
+ peek ( ) : ValueType | undefined ;
56
+
50
57
/**
51
58
Clear the queue.
52
59
*/
Original file line number Diff line number Diff line change @@ -46,6 +46,10 @@ export default class Queue {
46
46
return current . value ;
47
47
}
48
48
49
+ peek ( ) {
50
+ return this . #head?. value ;
51
+ }
52
+
49
53
clear ( ) {
50
54
this . #head = undefined ;
51
55
this . #tail = undefined ;
Original file line number Diff line number Diff line change @@ -51,6 +51,12 @@ Remove the next value in the queue.
51
51
52
52
Returns the removed value or ` undefined ` if the queue is empty.
53
53
54
+ #### ` .peek() `
55
+
56
+ Get the next value in the queue without removing it.
57
+
58
+ Returns the value or ` undefined ` if the queue is empty.
59
+
54
60
#### ` .clear() `
55
61
56
62
Clear the queue.
Original file line number Diff line number Diff line change @@ -20,6 +20,19 @@ test('.dequeue()', t => {
20
20
t . is ( queue . dequeue ( ) , undefined ) ;
21
21
} ) ;
22
22
23
+ test ( '.peek()' , t => {
24
+ const queue = new Queue ( ) ;
25
+ t . is ( queue . peek ( ) , undefined ) ;
26
+ queue . enqueue ( '🦄' ) ;
27
+ t . is ( queue . peek ( ) , '🦄' ) ;
28
+ queue . enqueue ( '🌈' ) ;
29
+ t . is ( queue . peek ( ) , '🦄' ) ;
30
+ queue . dequeue ( ) ;
31
+ t . is ( queue . peek ( ) , '🌈' ) ;
32
+ queue . dequeue ( ) ;
33
+ t . is ( queue . peek ( ) , undefined ) ;
34
+ } ) ;
35
+
23
36
test ( '.clear()' , t => {
24
37
const queue = new Queue ( ) ;
25
38
queue . clear ( ) ;
You can’t perform that action at this time.
0 commit comments