@typinghare/stack-queue
Advanced tools
+10
| "use strict"; | ||
| var __importDefault = (this && this.__importDefault) || function (mod) { | ||
| return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
| }; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.Queue = exports.Stack = void 0; | ||
| const stack_1 = __importDefault(require("./stack")); | ||
| exports.Stack = stack_1.default; | ||
| const queue_1 = __importDefault(require("./queue")); | ||
| exports.Queue = queue_1.default; |
| export type SearchPredicate<E> = (item: E) => boolean; |
+5
-3
| { | ||
| "name": "@typinghare/stack-queue", | ||
| "version": "0.0.0", | ||
| "version": "0.0.1", | ||
| "description": "A lightweight library implementing Stack and Queue for TypeScript.", | ||
| "main": "src/main.js", | ||
| "types": "src/main.d.ts", | ||
| "main": "dist/main.js", | ||
| "types": "dist/main.d.ts", | ||
| "author": "James Chan", | ||
| "homepage": "https://github.com/typinghare/stack-queue", | ||
| "email" : "jameschan312.cn@gmail.com", | ||
| "scripts": { | ||
@@ -9,0 +11,0 @@ "test": "jest --verbose --coverage", |
+123
-3
| # stack-queue | ||
| This is a lightweight library implementing Stack and Queue for TypeScript. | ||
| This is a lightweight library implementing Stack and Queue for TypeScript. Install it with the following command. | ||
| ## Install | ||
| ~~~bash | ||
@@ -9,2 +8,123 @@ npm i @typinghare/stack-queue | ||
| ## | ||
| > I know that many JavaScript developers can flexibly use **Array** to implement all functions of stack and queue. Nevertheless, TypeScript developers are not satisfied with Array because readers can hardly identify it as a stack or a queue through the given type definition. As to my own part, I strongly want my code to be neat, readable, and robust. Therefore, I wrote this package, and I hope this can do a favor to some of you guys. | ||
| **This package has been tested by Jest, with 100% coverage**. | ||
| ## Get Started | ||
| This package contains two commonly used classes—**Stack** and **Queue**—with some easy-to-operate methods. A simple example of a stack is as follows. | ||
| ~~~typescript | ||
| import { Stack } from '@typinghare/stack-queue'; | ||
| const smallBag = new Stack<string>(); | ||
| // push items on the top of the stack | ||
| smallBag.push('clothes'); | ||
| smallBag.push('textbooks'); | ||
| smallBag.push('biscuit'); | ||
| // Pop the top item from the stack. This item will be removed. | ||
| console.log(smallBag.pop()); // >> biscuit | ||
| // Peek the top item from the stack. This item will not be removed. | ||
| console.log(smallBag.peek()); // >> textbooks | ||
| // Search an item from the stack. | ||
| console.log(smallBag.search('clothes')); // >> 2 | ||
| ~~~ | ||
| The **Queue** is similar to it. | ||
| ~~~typescript | ||
| import { Queue } from '@typinghare/stack-queue'; | ||
| const line = new Queue<string>(); | ||
| // enqueue items to the queue | ||
| line.enqueue('Reina'); | ||
| line.enqueue('Jack'); | ||
| line.enqueue('Bob'); | ||
| // dequeue an item | ||
| console.log(line.dequeue()); // >> Reina | ||
| // Peek the head of the queue. This item will not be removed. | ||
| console.log(line.peek()); // >> 'Jack' | ||
| // Search an item from the queue. | ||
| console.log(line.search('Jack')); // >> 1 | ||
| ~~~ | ||
| ## Go Further | ||
| ### Basic Methods | ||
| These two classes are basically copied from the Java source code. I notice that `Stack` and `Queue` are written by two different guys, and there are some incoordinations within them, so I adjusted some details and make them perfect. | ||
| | Operation | Throw Exception if Empty | Return null if Empty | | ||
| | --------- | ------------------------ |--------------------| | ||
| | Insert | `push()` \ `enqueue()` | | | ||
| | Remove | `pop()` \ `dequeue()` | `poll()` | | ||
| | Examine | `element()` | `peek()` | | ||
| As shown in the table above, method `poll()` returns and removes the top element of a stack or the head element of a queue, or returns null if it is empty; `element()` retrieves but does not remove, the top element of a stack or the head element of a queue, and throws an exception if it is empty; `peek` does as `element()` but returns null if it is empty. | ||
| Besides, you can use `size()` to count the number of elements in a stack or a queue; use `empty()` to test if a stack of a queue is empty. | ||
| ### Quick Initialization | ||
| When constructing a stack or a queue, you can feed an iterable argument for a quick initialization as follows. The given elements will be added to the class in iterable order. | ||
| ~~~typescript | ||
| const stack = new Stack<number>([1, 2, 3]); | ||
| const queue = new Queue<string>(new Set(['a', 'b', 'c'])); | ||
| ~~~ | ||
| ### Pop or Dequeue Multiple Times at one Invocation | ||
| You can pass a number to `pop()` or `dequeue()` and execute pop or dequeue for specified times at one invocation. The method will return the last item or throw an exception if it is empty when at the last execution. | ||
| ~~~typescript | ||
| const stack = new Stack<number>(); | ||
| stack.push(10); | ||
| stack.push(20); | ||
| stack.push(30); | ||
| console.log(stack.pop(3)); // >> 10 | ||
| console.log(stack.size()); // >> 0 | ||
| ~~~ | ||
| The `dequeue()` is the like. | ||
| ### Search and Find | ||
| You can search for an item from a stack or a queue. Notice that the return value is the 1-based depth of the item, so it counts from `1`, not `0`. The method will return `-1` if the item is not in it. | ||
| ~~~typescript | ||
| const stack = new Stack<number>(); | ||
| stack.push(10); | ||
| stack.push(20); | ||
| stack.push(30); | ||
| console.log(stack.search(10)); // >> 3 | ||
| console.log(stack.search(40)); // >> -1 | ||
| ~~~ | ||
| You can also use `find()` to find an item from a stack or a queue with a predicate. The method will return `-1` if the item satisfied is not in it. | ||
| ~~~typescript | ||
| const queue = new Queue<string>(); | ||
| queue.enqueue('apple'); | ||
| queue.enqueue('banana'); | ||
| queue.enqueue('cat'); | ||
| queue.enqueue('dog'); | ||
| console.log(queue.find(word => word.at(0) === 'c')); // >> 3 | ||
| console.log(queue.find(word => word.at(0) === 'e')); // >> -1 | ||
| ~~~ | ||
| ## Epilogue and Some Grumbles | ||
| I am accustomed to leaving some thoughts at the end of the documentation. NPM is a huge community and owns a large number of excellent packages. But there is not enough specification and developers follow their ideas at their like, which I think is a little bit debatable. When I was writing this package I kept thinking that why do I have to write this basic library by consuming so much time? But I could not find one that is eligible for my requirements. Many of them are old and do not support TypeScript. I really wish that one day in the future, TypeScript can become an independent language and use its own interpreter and VM, and provides more basic classes like Java. To be honest, I don't like Java's syntax, which is really bloated. | ||
| It's the eighth month since I moved to America. I am studying at a community college and FASFA said I am not eligible for financial aid. My family is experiencing a severe deficit problem and I am still unemployed. People say American companies will not accept my out-of-country diploma and thus I cannot find a good job for the time being. I cannot handle the exaggerated inflation and don't see any hope for my desolate future. I wanna suicide, but programming keeps me alive. See how many libraries and projects I can finish before the end of my fragile life. : ) | ||
| export class EmptyStackException extends Error { | ||
| constructor() { | ||
| super('Stack underflow.'); | ||
| } | ||
| } | ||
| export class EmptyQueueException extends Error { | ||
| constructor() { | ||
| super('Queue underflow.'); | ||
| } | ||
| } |
+38
-8
| import { EmptyQueueException } from './exceptions'; | ||
| import { SearchPredicate } from './type'; | ||
@@ -11,7 +12,8 @@ export default class Queue<E = any> { | ||
| public constructor(); | ||
| public constructor(iterable: Iterable<E>); | ||
| /** | ||
| * To create an empty queue or a queue with given elements in a form of iterable. | ||
| * @param iterable an iterable object to quickly initialize this queue | ||
| */ | ||
| public constructor(); | ||
| public constructor(iterable: Iterable<E>); | ||
| public constructor(iterable?: Iterable<E>) { | ||
@@ -44,11 +46,20 @@ if (iterable) { | ||
| /** | ||
| * Retrieves and remove the head of this queue. | ||
| * (when <count> is default) Retrieves and remove the head of this queue. | ||
| * This method differs from poll() only in that it throws an exception if this queue is empty. | ||
| * @return the head of this queue | ||
| * @throws EmptyQueueException if this queue is empty | ||
| * (when <count> is greater than 1) Executes <count> times of dequeue and return the last item. | ||
| * Item traveled will be removed, including the returned one. | ||
| * @param count the number of times executing dequeue, the default value is 1 | ||
| * @return the head of this queue or the last dequeue item | ||
| * @throws EmptyQueueException (when <count> is default) if this queue is empty | ||
| * @throws EmptyQueueException (when <count> is greater than 1) if this queue is empty when the | ||
| * last queue is being executed | ||
| */ | ||
| public dequeue(): E { | ||
| const item = this.elements.shift(); | ||
| public dequeue(count: number = 1): E { | ||
| let item; | ||
| if (item == undefined) { | ||
| for (let i = 0; i < count; ++i) { | ||
| item = this.elements.shift(); | ||
| } | ||
| if (item === undefined) { | ||
| throw new EmptyQueueException(); | ||
@@ -124,2 +135,21 @@ } | ||
| } | ||
| /** | ||
| * Iterates over elements of this queue, returning the first element predicate returns true for. | ||
| * @param predicate the function invoked per iteration. | ||
| * @return the 1-based depth of the item, or -1 if the item satisfied is not in this queue | ||
| */ | ||
| public find(predicate: SearchPredicate<E>): number { | ||
| let position = 1; | ||
| let found = false; | ||
| for (const element of this) { | ||
| if (predicate(element)) { | ||
| found = true; | ||
| break; | ||
| } | ||
| position++; | ||
| } | ||
| return found ? position : -1; | ||
| } | ||
| } |
+37
-7
| import { EmptyStackException } from './exceptions'; | ||
| import { SearchPredicate } from './type'; | ||
@@ -44,11 +45,21 @@ export default class Stack<E = any> implements Iterable<E> { | ||
| /** | ||
| * Pops an item from this stack and returns it. The item popped is removed from this stack. | ||
| * (when <count> is default) Pops an item from this stack and returns it. | ||
| * The item popped is removed from this stack. | ||
| * This method differs from poll() only in that it throws an exception if this stack is empty. | ||
| * @return the item popped from this stack | ||
| * @throws EmptyStackException if this stack is empty | ||
| * (when <count> is greater than 1) Executes <count> times of pop and return the last item. | ||
| * Item traveled will be removed, including the returned one. | ||
| * @param count the number of times executing pop, the default value is 1 | ||
| * @return the item popped from this stack or the last pop item | ||
| * @throws EmptyStackException (when <count> is default) if this stack is empty | ||
| * @throws EmptyStackException (when <count> is greater than 1) if this stack is empty when the | ||
| * last pop is being executed | ||
| */ | ||
| public pop(): E { | ||
| const item = this.elements.pop(); | ||
| public pop(count: number = 1): E { | ||
| let item; | ||
| if (item == undefined) { | ||
| for (let i = 0; i < count; ++i) { | ||
| item = this.elements.pop(); | ||
| } | ||
| if (item === undefined) { | ||
| throw new EmptyStackException(); | ||
@@ -119,3 +130,3 @@ } | ||
| * @param item the item to search for | ||
| * @return the 1 based depth of the item, or -1 if the item is not on this stack | ||
| * @return the 1-based depth of the item, or -1 if the item is not on this stack | ||
| */ | ||
@@ -130,2 +141,21 @@ public search(item: E): number { | ||
| } | ||
| /** | ||
| * Iterates over elements of this stack, returning the first element predicate returns true for. | ||
| * @param predicate the function invoked per iteration. | ||
| * @return the 1-based depth of the item, or -1 if the item satisfied is not in this queue | ||
| */ | ||
| public find(predicate: SearchPredicate<E>): number { | ||
| let position = 1; | ||
| let found = false; | ||
| for (const element of this) { | ||
| if (predicate(element)) { | ||
| found = true; | ||
| break; | ||
| } | ||
| position++; | ||
| } | ||
| return found ? position : -1; | ||
| } | ||
| } |
| export declare class EmptyStackException extends Error { | ||
| constructor(); | ||
| } | ||
| export declare class EmptyQueueException extends Error { | ||
| constructor(); | ||
| } |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.EmptyQueueException = exports.EmptyStackException = void 0; | ||
| class EmptyStackException extends Error { | ||
| constructor() { | ||
| super('Stack underflow.'); | ||
| } | ||
| } | ||
| exports.EmptyStackException = EmptyStackException; | ||
| class EmptyQueueException extends Error { | ||
| constructor() { | ||
| super('Queue underflow.'); | ||
| } | ||
| } | ||
| exports.EmptyQueueException = EmptyQueueException; |
| import Stack from './stack'; | ||
| import Queue from './queue'; | ||
| export { Stack, Queue }; |
-10
| "use strict"; | ||
| var __importDefault = (this && this.__importDefault) || function (mod) { | ||
| return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
| }; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.Queue = exports.Stack = void 0; | ||
| const stack_1 = __importDefault(require("./stack")); | ||
| exports.Stack = stack_1.default; | ||
| const queue_1 = __importDefault(require("./queue")); | ||
| exports.Queue = queue_1.default; |
| export default class Queue<E = any> { | ||
| /** | ||
| * The container of elements. For the sake of safety, this property is private and thus not | ||
| * accessible from outside the class. | ||
| * @private | ||
| */ | ||
| private elements; | ||
| /** | ||
| * To create an empty queue or a queue with given elements in a form of iterable. | ||
| */ | ||
| constructor(); | ||
| constructor(iterable: Iterable<E>); | ||
| /** | ||
| * Iterable implementation. | ||
| */ | ||
| [Symbol.iterator](): Generator<E, void, unknown>; | ||
| /** | ||
| * Insert the specified element into this queue. | ||
| * @param item the item to insert | ||
| */ | ||
| enqueue(item: E): E; | ||
| /** | ||
| * Retrieves and remove the head of this queue. | ||
| * This method differs from poll() only in that it throws an exception if this queue is empty. | ||
| * @return the head of this queue | ||
| * @throws EmptyQueueException if this queue is empty | ||
| */ | ||
| dequeue(): E; | ||
| /** | ||
| * Retrieves and remove the head of this queue, or returns null if this queue is empty. | ||
| * @return the head of this queue, or null if this queue is empty | ||
| */ | ||
| poll(): E | null; | ||
| /** | ||
| * Retrieves, but does not remove, the head of this queue. | ||
| * This method differs from peek() only in that it throws an exception if this queue is empty. | ||
| * @return the head of this queue | ||
| * @throws EmptyQueueException if this queue is empty | ||
| */ | ||
| element(): E; | ||
| /** | ||
| * Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty. | ||
| * @return the head of this queue, or null if this queue is empty | ||
| */ | ||
| peek(): E | null; | ||
| /** | ||
| * Returns the number of elements in this queue. | ||
| * @return the number of elements in this queue | ||
| */ | ||
| size(): number; | ||
| /** | ||
| * Tests if this queue is empty. | ||
| * @return true if this queue contains no items, false otherwise | ||
| */ | ||
| empty(): boolean; | ||
| /** | ||
| * Returns the position of an item in this queue. | ||
| * @param item the item to search for | ||
| * @return the 1-based depth of the item, or -1 if the item is not in this queue | ||
| */ | ||
| search(item: E): number; | ||
| } |
-104
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| const exceptions_1 = require("./exceptions"); | ||
| class Queue { | ||
| constructor(iterable) { | ||
| /** | ||
| * The container of elements. For the sake of safety, this property is private and thus not | ||
| * accessible from outside the class. | ||
| * @private | ||
| */ | ||
| this.elements = []; | ||
| if (iterable) { | ||
| for (const element of iterable) { | ||
| this.enqueue(element); | ||
| } | ||
| } | ||
| } | ||
| /** | ||
| * Iterable implementation. | ||
| */ | ||
| *[Symbol.iterator]() { | ||
| for (let i = 0; i < this.elements.length; ++i) { | ||
| yield this.elements[i]; | ||
| } | ||
| } | ||
| /** | ||
| * Insert the specified element into this queue. | ||
| * @param item the item to insert | ||
| */ | ||
| enqueue(item) { | ||
| this.elements.push(item); | ||
| return item; | ||
| } | ||
| /** | ||
| * Retrieves and remove the head of this queue. | ||
| * This method differs from poll() only in that it throws an exception if this queue is empty. | ||
| * @return the head of this queue | ||
| * @throws EmptyQueueException if this queue is empty | ||
| */ | ||
| dequeue() { | ||
| const item = this.elements.shift(); | ||
| if (item == undefined) { | ||
| throw new exceptions_1.EmptyQueueException(); | ||
| } | ||
| return item; | ||
| } | ||
| /** | ||
| * Retrieves and remove the head of this queue, or returns null if this queue is empty. | ||
| * @return the head of this queue, or null if this queue is empty | ||
| */ | ||
| poll() { | ||
| const item = this.elements.shift(); | ||
| return item === undefined ? null : item; | ||
| } | ||
| /** | ||
| * Retrieves, but does not remove, the head of this queue. | ||
| * This method differs from peek() only in that it throws an exception if this queue is empty. | ||
| * @return the head of this queue | ||
| * @throws EmptyQueueException if this queue is empty | ||
| */ | ||
| element() { | ||
| if (this.elements.length === 0) { | ||
| throw new exceptions_1.EmptyQueueException(); | ||
| } | ||
| return this.elements[0]; | ||
| } | ||
| /** | ||
| * Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty. | ||
| * @return the head of this queue, or null if this queue is empty | ||
| */ | ||
| peek() { | ||
| if (this.elements.length === 0) { | ||
| return null; | ||
| } | ||
| return this.elements[0]; | ||
| } | ||
| /** | ||
| * Returns the number of elements in this queue. | ||
| * @return the number of elements in this queue | ||
| */ | ||
| size() { | ||
| return this.elements.length; | ||
| } | ||
| /** | ||
| * Tests if this queue is empty. | ||
| * @return true if this queue contains no items, false otherwise | ||
| */ | ||
| empty() { | ||
| return this.size() === 0; | ||
| } | ||
| /** | ||
| * Returns the position of an item in this queue. | ||
| * @param item the item to search for | ||
| * @return the 1-based depth of the item, or -1 if the item is not in this queue | ||
| */ | ||
| search(item) { | ||
| if (this.empty()) { | ||
| return -1; | ||
| } | ||
| const index = this.elements.indexOf(item); | ||
| return index == -1 ? -1 : index + 1; | ||
| } | ||
| } | ||
| exports.default = Queue; |
| export default class Stack<E = any> implements Iterable<E> { | ||
| /** | ||
| * The container of elements. For the sake of safety, this property is private and thus not | ||
| * accessible from outside the class. | ||
| * @private | ||
| */ | ||
| private elements; | ||
| /** | ||
| * To create an empty stack or a stack with given elements in a form of iterable. | ||
| */ | ||
| constructor(); | ||
| constructor(iterable: Iterable<E>); | ||
| /** | ||
| * Iterable implementation. | ||
| */ | ||
| [Symbol.iterator](): Generator<E, void, unknown>; | ||
| /** | ||
| * Pushes an item onto the top of this stack. | ||
| * @param item the item to push onto this stack | ||
| * @return the item argument | ||
| */ | ||
| push(item: E): E; | ||
| /** | ||
| * Pops an item from this stack and returns it. The item popped is removed from this stack. | ||
| * This method differs from poll() only in that it throws an exception if this stack is empty. | ||
| * @return the item popped from this stack | ||
| * @throws EmptyStackException if this stack is empty | ||
| */ | ||
| pop(): E; | ||
| /** | ||
| * Pops an item from this stack and returns it, or returns null if this stack is empty. | ||
| * The item popped is removed from this stack. | ||
| * @return the top item on this stack, or null if the stack is empty | ||
| */ | ||
| poll(): E | null; | ||
| /** | ||
| * Returns the top item on this stack without removing it. | ||
| * This method differs from peek() only in that it throws an exception if this stack is empty. | ||
| * @return the top item on this stack | ||
| * @throws EmptyStackException if this stack is empty | ||
| */ | ||
| element(): E; | ||
| /** | ||
| * Returns the top item on this stack without removing it, or returns null if this stack is empty. | ||
| * @return the top item on this stack | ||
| * @throws EmptyStackException if this stack is empty | ||
| */ | ||
| peek(): E | null; | ||
| /** | ||
| * Returns the number of elements in this stack. | ||
| * @return the number of elements in this stack. | ||
| */ | ||
| size(): number; | ||
| /** | ||
| * Tests if this stack is empty. | ||
| * @return true if this stack contains no items, false otherwise | ||
| */ | ||
| empty(): boolean; | ||
| /** | ||
| * Returns the 1-based position where an element is on this stack. | ||
| * If the element occurs as an item in this stack, this method returns the distance from the | ||
| * top of this stack of the occurrence nearest the top of this stack; the topmost item on the | ||
| * stack is considered to be at distance 1. | ||
| * @param item the item to search for | ||
| * @return the 1 based depth of the item, or -1 if the item is not on this stack | ||
| */ | ||
| search(item: E): number; | ||
| } |
-110
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| const exceptions_1 = require("./exceptions"); | ||
| class Stack { | ||
| constructor(iterable) { | ||
| /** | ||
| * The container of elements. For the sake of safety, this property is private and thus not | ||
| * accessible from outside the class. | ||
| * @private | ||
| */ | ||
| this.elements = []; | ||
| if (iterable) { | ||
| for (const element of iterable) { | ||
| this.push(element); | ||
| } | ||
| } | ||
| } | ||
| /** | ||
| * Iterable implementation. | ||
| */ | ||
| *[Symbol.iterator]() { | ||
| for (let i = this.elements.length - 1; i >= 0; --i) { | ||
| yield this.elements[i]; | ||
| } | ||
| } | ||
| /** | ||
| * Pushes an item onto the top of this stack. | ||
| * @param item the item to push onto this stack | ||
| * @return the item argument | ||
| */ | ||
| push(item) { | ||
| this.elements.push(item); | ||
| return item; | ||
| } | ||
| /** | ||
| * Pops an item from this stack and returns it. The item popped is removed from this stack. | ||
| * This method differs from poll() only in that it throws an exception if this stack is empty. | ||
| * @return the item popped from this stack | ||
| * @throws EmptyStackException if this stack is empty | ||
| */ | ||
| pop() { | ||
| const item = this.elements.pop(); | ||
| if (item == undefined) { | ||
| throw new exceptions_1.EmptyStackException(); | ||
| } | ||
| return item; | ||
| } | ||
| /** | ||
| * Pops an item from this stack and returns it, or returns null if this stack is empty. | ||
| * The item popped is removed from this stack. | ||
| * @return the top item on this stack, or null if the stack is empty | ||
| */ | ||
| poll() { | ||
| const item = this.elements.pop(); | ||
| return item === undefined ? null : item; | ||
| } | ||
| /** | ||
| * Returns the top item on this stack without removing it. | ||
| * This method differs from peek() only in that it throws an exception if this stack is empty. | ||
| * @return the top item on this stack | ||
| * @throws EmptyStackException if this stack is empty | ||
| */ | ||
| element() { | ||
| if (this.elements.length === 0) { | ||
| throw new exceptions_1.EmptyStackException(); | ||
| } | ||
| return this.elements[this.elements.length - 1]; | ||
| } | ||
| /** | ||
| * Returns the top item on this stack without removing it, or returns null if this stack is empty. | ||
| * @return the top item on this stack | ||
| * @throws EmptyStackException if this stack is empty | ||
| */ | ||
| peek() { | ||
| if (this.elements.length === 0) { | ||
| return null; | ||
| } | ||
| return this.elements[this.elements.length - 1]; | ||
| } | ||
| /** | ||
| * Returns the number of elements in this stack. | ||
| * @return the number of elements in this stack. | ||
| */ | ||
| size() { | ||
| return this.elements.length; | ||
| } | ||
| /** | ||
| * Tests if this stack is empty. | ||
| * @return true if this stack contains no items, false otherwise | ||
| */ | ||
| empty() { | ||
| return this.size() === 0; | ||
| } | ||
| /** | ||
| * Returns the 1-based position where an element is on this stack. | ||
| * If the element occurs as an item in this stack, this method returns the distance from the | ||
| * top of this stack of the occurrence nearest the top of this stack; the topmost item on the | ||
| * stack is considered to be at distance 1. | ||
| * @param item the item to search for | ||
| * @return the 1 based depth of the item, or -1 if the item is not on this stack | ||
| */ | ||
| search(item) { | ||
| if (this.elements.length == 0) { | ||
| return -1; | ||
| } | ||
| const index = this.elements.indexOf(item); | ||
| return index == -1 ? -1 : this.elements.length - index; | ||
| } | ||
| } | ||
| exports.default = Stack; |
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
1
-50%130
1344.44%16207
-20.43%8
-42.86%285
-52.89%1
Infinity%