diff --git a/packages/stdlib/src/structs/stack/index.test.ts b/packages/stdlib/src/structs/stack/index.test.ts index e67522a..7d3920a 100644 --- a/packages/stdlib/src/structs/stack/index.test.ts +++ b/packages/stdlib/src/structs/stack/index.test.ts @@ -79,9 +79,11 @@ describe('stack', () => { expect(stack.length).toBe(3); }); - it('throw an error if the stack is empty', () => { + it('return undefined if the stack is empty', () => { const stack = new Stack(); - expect(() => stack.peek()).toThrow(new RangeError('Stack is empty')); + const topElement = stack.peek(); + + expect(topElement).toBeUndefined(); }); }); diff --git a/packages/stdlib/src/structs/stack/index.ts b/packages/stdlib/src/structs/stack/index.ts index db48251..6a6d1af 100644 --- a/packages/stdlib/src/structs/stack/index.ts +++ b/packages/stdlib/src/structs/stack/index.ts @@ -96,7 +96,7 @@ export class Stack implements Iterable, AsyncIterable { */ public peek() { if (this.isEmpty) - throw new RangeError('Stack is empty'); + return undefined; return last(this.stack); }