Pārlūkot izejas kodu

Update errors thrown by Stack class

ApisNecros 1 gadu atpakaļ
vecāks
revīzija
ea935ad4eb
1 mainītis faili ar 10 papildinājumiem un 13 dzēšanām
  1. 10 13
      src/stack.ts

+ 10 - 13
src/stack.ts

@@ -1,4 +1,5 @@
 import { charToDecimal } from "./common.js";
+import { EmptyStackError, WrongStackSizeError } from "./errors.js";
 
 export class Stack {
     /**
@@ -49,7 +50,7 @@ export class Stack {
      */
     public pop(): number {
         const value = this.stack.pop();
-        if(value == undefined){ throw new Error(); }
+        if(value == undefined){ throw new EmptyStackError(); }
 
         return value;
     }
@@ -63,7 +64,7 @@ export class Stack {
     public popMany(count: number): number[] {
         const stackLength = this.stack.length;
         if (count > stackLength) {
-            throw new RangeError(`Unable to remove ${count} elements from a stack with a size of ${stackLength}`);
+            throw new WrongStackSizeError(count, stackLength);
         }
 
         return this.stack.splice(stackLength - count, count).reverse();
@@ -80,7 +81,7 @@ export class Stack {
         if (temp !== undefined) {
             this.stack.push(temp);
         } else {
-            throw new Error()
+            throw new EmptyStackError();
         }
     }
     /**
@@ -89,13 +90,9 @@ export class Stack {
      * Shifts the entire stack rightward by one value
      */
     public shiftRight() {
-        const temp = this.stack.pop();
+        const temp = this.pop();
         
-        if (temp !== undefined) {
-            this.stack.unshift(temp);
-        } else {
-            throw new Error();
-        }
+        this.stack.unshift(temp);
     }
     /**
      * Implement `$`
@@ -103,7 +100,7 @@ export class Stack {
      * Swaps the top two values of the stack
      */
     public swapTwo() {
-        if(this.stack.length < 2) { throw new Error(); }
+        if(this.stack.length < 2) { throw new WrongStackSizeError(2, this.stack.length); }
 
         const popped = this.stack.splice(this.stack.length - 2, 2);
         this.stack.push(...popped.reverse());
@@ -114,7 +111,7 @@ export class Stack {
      * Swaps the top three values of the stack
      */
     public swapThree() {
-        if(this.stack.length < 3) { throw new Error(); }
+        if(this.stack.length < 3) { throw new WrongStackSizeError(3, this.stack.length); }
         // Get the top three values
         const popped = this.stack.splice(this.stack.length - 3, 3);
         // Shift the elements to the right
@@ -128,7 +125,7 @@ export class Stack {
      * Duplicates the element on the top of the stack
      */
     public duplicate() {
-        if (this.stack.length < 1) { throw new Error(); }
+        if (this.stack.length < 1) { throw new EmptyStackError(); }
         this.stack.push(this.stack[this.stack.length-1]!);
     }
     /**
@@ -137,7 +134,7 @@ export class Stack {
      * Removes the element on the top of the stack 
      */
     public remove() {
-        if (this.stack.length < 1) { throw new Error(); }
+        if (this.stack.length < 1) { throw new EmptyStackError(); }
         this.stack.pop();
     }