New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

@typinghare/stack-queue

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@typinghare/stack-queue - npm Package Compare versions

Comparing version
0.0.2
to
0.1.0
+1
-1
package.json
{
"name": "@typinghare/stack-queue",
"version": "0.0.2",
"version": "0.1.0",
"description": "A lightweight library implementing Stack and Queue for TypeScript.",

@@ -5,0 +5,0 @@ "main": "dist/main.js",

+160
-122
import { EmptyQueueException } from './exceptions';
import { SearchPredicate } from './type';
export default class Queue<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: E[] = [];
/**
* @author James Chan
*/
export default class Queue<E = any> implements Iterable<E> {
/**
* The container of elements.
* @private
*/
private _elements: E[] = [];
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(iterable?: Iterable<E>) {
if (iterable) {
for (const element of iterable) {
this.enqueue(element);
}
/**
* To create an empty queue.
*/
public constructor();
/**
* 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(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(iterable?: Iterable<E>) {
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];
/**
* 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
*/
public enqueue(item: E): E {
this.elements.push(item);
return item;
}
/**
* Insert the specified element into this queue.
* @param item the item to insert
*/
public enqueue(item: E): E {
this._elements.push(item);
return item;
}
/**
* (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.
* (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(count: number = 1): E {
let item;
/**
* (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.
* (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(count: number = 1): E {
let item;
for (let i = 0; i < count; ++i) {
item = this.elements.shift();
for (let i = 0; i < count; ++i) {
item = this._elements.shift();
}
if (item === undefined) {
throw new EmptyQueueException();
}
return item;
}
if (item === undefined) {
throw new EmptyQueueException();
/**
* 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
*/
public poll(): E | null {
const item = this._elements.shift();
return item === undefined ? null : item;
}
return 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
*/
public element(): E {
if (this._elements.length === 0) {
throw new EmptyQueueException();
}
/**
* 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
*/
public poll(): E | null {
const item = this.elements.shift();
return item === undefined ? null : item;
}
return this._elements[0];
}
/**
* 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
*/
public element(): E {
if (this.elements.length === 0) {
throw new EmptyQueueException();
/**
* 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
*/
public peek(): E | null {
if (this._elements.length === 0) {
return null;
}
return this._elements[0];
}
return this.elements[0];
}
/**
* Returns the number of elements in this queue.
* @return the number of elements in this queue
*/
public size(): number {
return this._elements.length;
}
/**
* 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
*/
public peek(): E | null {
if (this.elements.length === 0) {
return null;
/**
* Tests if this queue is empty.
* @return true if this queue contains no items, false otherwise
*/
public empty(): boolean {
return this.size() === 0;
}
return this.elements[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
*/
public search(item: E): number {
if (this.empty()) {
return -1;
}
/**
* Returns the number of elements in this queue.
* @return the number of elements in this queue
*/
public size(): number {
return this.elements.length;
}
const index = this._elements.indexOf(item);
return index == -1 ? -1 : index + 1;
}
/**
* Tests if this queue is empty.
* @return true if this queue contains no items, false otherwise
*/
public empty(): boolean {
return this.size() === 0;
}
/**
* 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++;
}
/**
* 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
*/
public search(item: E): number {
if (this.empty()) {
return -1;
return found ? position : -1;
}
const index = this.elements.indexOf(item);
return index == -1 ? -1 : index + 1;
}
/**
* Returns the elements array of this queue.
* @since 0.1.0
*/
public get elements(): E[] {
return this._elements;
}
/**
* 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++;
/**
* A helper function of traversing elements in this queue.
* @param callback
* @since 0.1.0
*/
public each(callback: (value: E, index?: number, array?: E[]) => void) {
this._elements.forEach(callback);
}
return found ? position : -1;
}
/**
* A helper function of traversing elements backward in this queue.
* @param callback
* @since 0.1.0
*/
public inverseEach(callback: (value: E, index?: number, array?: E[]) => void) {
for (let i = this._elements.length - 1; i >= 0; --i) {
callback(this._elements[i], i, this._elements);
}
}
}
+165
-127
import { EmptyStackException } from './exceptions';
import { SearchPredicate } from './type';
/**
* @author James Chan
*/
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: E[] = [];
/**
* The container of elements.
* @private
*/
private _elements: E[] = [];
/**
* To create an empty stack or a stack with given elements in a form of iterable.
*/
public constructor();
public constructor(iterable: Iterable<E>);
public constructor(iterable?: Iterable<E>) {
if (iterable) {
for (const element of iterable) {
this.push(element);
}
/**
* To create an empty stack.
*/
public constructor();
/**
* To create an empty stack or a stack with given elements in a form of iterable.
* @param iterable an iterable object to quickly initialize this stack
*/
public constructor(iterable: Iterable<E>);
/**
* To create an empty stack or a stack with given elements in a form of iterable.
* @param iterable an iterable object to quickly initialize this stack
*/
public constructor(iterable?: Iterable<E>) {
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];
/**
* 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
*/
public push(item: E): E {
this.elements.push(item);
return item;
}
/**
* Pushes an item onto the top of this stack.
* @param item the item to push onto this stack
* @return the item argument
*/
public push(item: E): E {
this._elements.push(item);
return item;
}
/**
* (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.
* (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(count: number = 1): E {
let item;
/**
* (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.
* (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(count: number = 1): E {
let item;
for (let i = 0; i < count; ++i) {
item = this.elements.pop();
for (let i = 0; i < count; ++i) {
item = this._elements.pop();
}
if (item === undefined) {
throw new EmptyStackException();
}
return item;
}
if (item === undefined) {
throw new EmptyStackException();
/**
* 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
*/
public poll(): E | null {
const item = this._elements.pop();
return item === undefined ? null : item;
}
return 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
*/
public element(): E {
if (this._elements.length === 0) {
throw new EmptyStackException();
}
/**
* 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
*/
public poll(): E | null {
const item = this.elements.pop();
return item === undefined ? null : item;
}
return this._elements[this._elements.length - 1];
}
/**
* 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
*/
public element(): E {
if (this.elements.length === 0) {
throw new EmptyStackException();
/**
* 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
*/
public peek(): E | null {
if (this._elements.length === 0) {
return null;
}
return this._elements[this._elements.length - 1];
}
return this.elements[this.elements.length - 1];
}
/**
* Returns the number of elements in this stack.
* @return the number of elements in this stack.
*/
public size(): number {
return this._elements.length;
}
/**
* 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
*/
public peek(): E | null {
if (this.elements.length === 0) {
return null;
/**
* Tests if this stack is empty.
* @return true if this stack contains no items, false otherwise
*/
public empty(): boolean {
return this.size() === 0;
}
return this.elements[this.elements.length - 1];
}
/**
* 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
*/
public search(item: E): number {
if (this._elements.length == 0) {
return -1;
}
/**
* Returns the number of elements in this stack.
* @return the number of elements in this stack.
*/
public size(): number {
return this.elements.length;
}
const index = this._elements.indexOf(item);
return index == -1 ? -1 : this._elements.length - index;
}
/**
* Tests if this stack is empty.
* @return true if this stack contains no items, false otherwise
*/
public empty(): boolean {
return this.size() === 0;
}
/**
* 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++;
}
/**
* 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
*/
public search(item: E): number {
if (this.elements.length == 0) {
return -1;
return found ? position : -1;
}
const index = this.elements.indexOf(item);
return index == -1 ? -1 : this.elements.length - index;
}
/**
* Returns the elements array of this stack.
* @since 0.1.0
*/
public get elements() {
return this._elements;
}
/**
* 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++;
/**
* A helper function of traversing elements in this stack.
* @param callback
* @since 0.1.0
*/
public each(callback: (value: E, index?: number, array?: E[]) => void) {
for (let i = this._elements.length - 1; i >= 0; --i) {
callback(this._elements[i], i, this._elements);
}
}
return found ? position : -1;
}
/**
* A helper function of traversing elements backward in this stack.
* @param callback
* @since 0.1.0
*/
public inverseEach(callback: (value: E, index?: number, array?: E[]) => void) {
this._elements.forEach(callback);
}
}