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
0
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.1.1
to
1.0.0
+121
dist/collection.js
/**
* @since 1.0.0
*/
export class Collection {
/**
* Collection elements.
* @protected
*/
internalElements = [];
/**
* Creates a collection.
* @param iterable An iterable object to initialize this collection
* (optional).
* @param reversed Whether to reverse the order of elements (default: true).
*/
constructor(iterable, reversed = false) {
this.addAll(iterable, reversed);
}
/**
* Retrieves an element.
* @param index The index of the element to retrieve
*/
get(index) {
return this.internalElements[index];
}
/**
* Returns an iterator for the collection.
*/
*[Symbol.iterator]() {
for (let i = 0; i < this.internalElements.length; ++i) {
yield this.internalElements[i];
}
}
/**
* Gets the elements array of this collection.
* @since 0.1.0
*/
get elements() {
return this.internalElements;
}
/**
* Returns the number of elements in this collection.
* @return The number of elements.
*/
size() {
return this.internalElements.length;
}
/**
* Tests if the collection is empty.
* @return True if the collection contains no items, false otherwise.
*/
empty() {
return this.size() === 0;
}
/**
* Iterates over elements in this collection, applying the callback
* function.
* @param callback The function to apply to each element.
* @since 0.1.0
*/
each(callback) {
this.internalElements.forEach(callback);
}
/**
* Iterates over elements in reverse order, applying the callback function.
* @param callback The function to apply to each element.
* @since 0.1.0
*/
inverseEach(callback) {
for (let i = this.internalElements.length - 1; i >= 0; --i) {
callback(this.internalElements[i], i, this.internalElements);
}
}
/**
* Finds the first element that matches the predicate.
* @param predicate The function to test each element.
* @return The 1-based position of the item, or -1 if not found.
*/
find(predicate) {
let position = 1;
let found = false;
for (const element of this) {
if (predicate(element)) {
found = true;
break;
}
position++;
}
return found ? position : -1;
}
/**
* Searches for the position of an item in the collection.
* @param item The item to search for.
* @return The 0-based position from the beginning of the collection, or -1
* if not found.
*/
search(item) {
if (this.internalElements.length == 0) {
return -1;
}
return this.internalElements.indexOf(item);
}
/**
* Add elements to the collection.
* @param iterable An iterable object to initialize this collection
* (optional).
* @param reversed Whether to reverse the order of elements (default: true).
*/
addAll(iterable, reversed = false) {
if (iterable) {
for (const element of iterable) {
if (reversed) {
this.internalElements.unshift(element);
}
else {
this.internalElements.push(element);
}
}
}
}
}
export * from './exceptions.js';
export * from './collection.js';
export * from './queue.js';
export * from './stack.js';
export * from './type.js';
// Generated by dts-bundle-generator v9.5.1
export declare class EmptyStackException extends Error {
}
export declare class EmptyQueueException extends Error {
}
export type SearchPredicate<E> = (item: E) => boolean;
/**
* @since 1.0.0
*/
export declare class Collection<E = unknown> implements Iterable<E> {
/**
* Collection elements.
* @protected
*/
protected internalElements: E[];
/**
* Creates a collection.
* @param iterable An iterable object to initialize this collection
* (optional).
* @param reversed Whether to reverse the order of elements (default: true).
*/
constructor(iterable?: Iterable<E>, reversed?: boolean);
/**
* Retrieves an element.
* @param index The index of the element to retrieve
*/
get(index: number): E;
/**
* Returns an iterator for the collection.
*/
[Symbol.iterator](): IterableIterator<E>;
/**
* Gets the elements array of this collection.
* @since 0.1.0
*/
get elements(): E[];
/**
* Returns the number of elements in this collection.
* @return The number of elements.
*/
size(): number;
/**
* Tests if the collection is empty.
* @return True if the collection contains no items, false otherwise.
*/
empty(): boolean;
/**
* Iterates over elements in this collection, applying the callback
* function.
* @param callback The function to apply to each element.
* @since 0.1.0
*/
each(callback: (value: E, index?: number, array?: E[]) => void): void;
/**
* Iterates over elements in reverse order, applying the callback function.
* @param callback The function to apply to each element.
* @since 0.1.0
*/
inverseEach(callback: (value: E, index?: number, array?: E[]) => void): void;
/**
* Finds the first element that matches the predicate.
* @param predicate The function to test each element.
* @return The 1-based position of the item, or -1 if not found.
*/
find(predicate: SearchPredicate<E>): number;
/**
* Searches for the position of an item in the collection.
* @param item The item to search for.
* @return The 0-based position from the beginning of the collection, or -1
* if not found.
*/
search(item: E): number;
/**
* Add elements to the collection.
* @param iterable An iterable object to initialize this collection
* (optional).
* @param reversed Whether to reverse the order of elements (default: true).
*/
addAll(iterable?: Iterable<E>, reversed?: boolean): void;
}
/**
* @author James Chen
*/
export declare class Queue<E = unknown> extends Collection<E> {
/**
* Insert the specified element into this queue.
* @param item the item to insert
*/
enqueue(item: E): E;
/**
* (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, default by 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
*/
dequeue(count?: number): 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;
enqueueAll(iterator: Iterable<E>): void;
}
/**
* @author James Chen
*/
export declare class Stack<E = unknown> extends Collection<E> {
[Symbol.iterator](): IterableIterator<E>;
/**
* 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;
/**
* (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 the
* length of this stack is less than <count>. In this case, this
* function pops all elements and throw the exception.
* last pop is being executed
*/
pop(count?: number): 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;
/**
* Iterates over elements in this collection, applying the callback
* function.
* @param callback The function to apply to each element.
* @since 0.1.0
*/
each(callback: (value: E, index?: number, array?: E[]) => void): void;
/**
* Iterates over elements in reverse order, applying the callback function.
* @param callback The function to apply to each element.
* @since 0.1.0
*/
inverseEach(callback: (value: E, index?: number, array?: E[]) => void): void;
/**
* Pushes a collection of elements onto the stack.
* @param iterator The collection of elements to push onto the stack
*/
pushAll(iterator: Iterable<E>): void;
/**
* Searches for the position of an item in the stack.
* @param item The item to search for.
* @return The 0-based position from the top of the stack, or -1 if
* not found.
*/
search(item: E): number;
}
export {};
+2
-7

@@ -1,9 +0,4 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EmptyQueueException = exports.EmptyStackException = void 0;
class EmptyStackException extends Error {
export class EmptyStackException extends Error {
}
exports.EmptyStackException = EmptyStackException;
class EmptyQueueException extends Error {
export class EmptyQueueException extends Error {
}
exports.EmptyQueueException = EmptyQueueException;

@@ -1,33 +0,8 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const exceptions_1 = require("./exceptions");
import { EmptyQueueException } from './exceptions.js';
import { Collection } from './collection.js';
/**
* @author James Chan
* @author James Chen
*/
class Queue {
export class Queue extends Collection {
/**
* 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
*/
constructor(iterable) {
/**
* The container of elements.
* @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.

@@ -37,3 +12,3 @@ * @param item the item to insert

enqueue(item) {
this._elements.push(item);
this.internalElements.push(item);
return item;

@@ -43,10 +18,12 @@ }

* (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
* 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, default by 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
* @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
*/

@@ -56,6 +33,6 @@ dequeue(count = 1) {

for (let i = 0; i < count; ++i) {
item = this._elements.shift();
item = this.internalElements.shift();
}
if (item === undefined) {
throw new exceptions_1.EmptyQueueException();
throw new EmptyQueueException();
}

@@ -65,7 +42,8 @@ return item;

/**
* Retrieves and remove the head of this queue, or returns null if this queue is empty.
* 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();
const item = this.internalElements.shift();
return item === undefined ? null : item;

@@ -75,3 +53,4 @@ }

* 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.
* This method differs from peek() only in that it throws an exception if
* this queue is empty.
* @return the head of this queue

@@ -81,86 +60,21 @@ * @throws EmptyQueueException if this queue is empty

element() {
if (this._elements.length === 0) {
throw new exceptions_1.EmptyQueueException();
if (this.internalElements.length === 0) {
throw new EmptyQueueException();
}
return this._elements[0];
return this.internalElements[0];
}
/**
* Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
* 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) {
if (this.internalElements.length === 0) {
return null;
}
return this._elements[0];
return this.internalElements[0];
}
/**
* Returns the number of elements in this queue.
* @return the number of elements in this queue
*/
size() {
return this._elements.length;
enqueueAll(iterator) {
this.addAll(iterator);
}
/**
* 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;
}
/**
* 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
*/
find(predicate) {
let position = 1;
let found = false;
for (const element of this) {
if (predicate(element)) {
found = true;
break;
}
position++;
}
return found ? position : -1;
}
/**
* Returns the elements array of this queue.
* @since 0.1.0
*/
get elements() {
return this._elements;
}
/**
* A helper function of traversing elements in this queue.
* @param callback
* @since 0.1.0
*/
each(callback) {
this._elements.forEach(callback);
}
/**
* A helper function of traversing elements backward in this queue.
* @param callback
* @since 0.1.0
*/
inverseEach(callback) {
for (let i = this._elements.length - 1; i >= 0; --i) {
callback(this._elements[i], i, this._elements);
}
}
}
exports.default = Queue;

@@ -1,30 +0,10 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const exceptions_1 = require("./exceptions");
import { EmptyStackException } from './exceptions.js';
import { Collection } from './collection.js';
/**
* @author James Chan
* @author James Chen
*/
class Stack {
/**
* 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
*/
constructor(iterable) {
/**
* The container of elements.
* @private
*/
this._elements = [];
if (iterable) {
for (const element of iterable) {
this.push(element);
}
}
}
/**
* Iterable implementation.
*/
export class Stack extends Collection {
*[Symbol.iterator]() {
for (let i = this._elements.length - 1; i >= 0; --i) {
yield this._elements[i];
for (let i = this.internalElements.length - 1; i >= 0; --i) {
yield this.internalElements[i];
}

@@ -38,3 +18,3 @@ }

push(item) {
this._elements.push(item);
this.internalElements.push(item);
return item;

@@ -45,9 +25,13 @@ }

* 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.
* 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
* @throws EmptyStackException (when <count> is default) if this stack is
* empty
* @throws EmptyStackException (when <count> is greater than 1) if the
* length of this stack is less than <count>. In this case, this
* function pops all elements and throw the exception.
* last pop is being executed

@@ -57,17 +41,18 @@ */

let item;
for (let i = 0; i < count; ++i) {
item = this._elements.pop();
while (count > 0) {
if (this.internalElements.length === 0) {
throw new EmptyStackException();
}
item = this.internalElements.pop();
count--;
}
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.
* 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();
const item = this.internalElements.pop();
return item === undefined ? null : item;

@@ -77,3 +62,4 @@ }

* 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.
* This method differs from peek() only in that it throws an exception if
* this stack is empty.
* @return the top item on this stack

@@ -83,9 +69,10 @@ * @throws EmptyStackException if this stack is empty

element() {
if (this._elements.length === 0) {
throw new exceptions_1.EmptyStackException();
if (this.internalElements.length === 0) {
throw new EmptyStackException();
}
return this._elements[this._elements.length - 1];
return this.internalElements[this.internalElements.length - 1];
}
/**
* Returns the top item on this stack without removing it, or returns null if this stack is empty.
* 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

@@ -95,79 +82,41 @@ * @throws EmptyStackException if this stack is empty

peek() {
if (this._elements.length === 0) {
if (this.internalElements.length === 0) {
return null;
}
return this._elements[this._elements.length - 1];
return this.internalElements[this.internalElements.length - 1];
}
/**
* Returns the number of elements in this stack.
* @return the number of elements in this stack.
* Iterates over elements in this collection, applying the callback
* function.
* @param callback The function to apply to each element.
* @since 0.1.0
*/
size() {
return this._elements.length;
each(callback) {
super.inverseEach(callback);
}
/**
* 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;
}
/**
* 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
*/
find(predicate) {
let position = 1;
let found = false;
for (const element of this) {
if (predicate(element)) {
found = true;
break;
}
position++;
}
return found ? position : -1;
}
/**
* Returns the elements array of this stack.
* Iterates over elements in reverse order, applying the callback function.
* @param callback The function to apply to each element.
* @since 0.1.0
*/
get elements() {
return this._elements;
inverseEach(callback) {
super.each(callback);
}
/**
* A helper function of traversing elements in this stack.
* @param callback
* @since 0.1.0
* Pushes a collection of elements onto the stack.
* @param iterator The collection of elements to push onto the stack
*/
each(callback) {
for (let i = this._elements.length - 1; i >= 0; --i) {
callback(this._elements[i], i, this._elements);
}
pushAll(iterator) {
this.addAll(iterator);
}
/**
* A helper function of traversing elements backward in this stack.
* @param callback
* @since 0.1.0
* Searches for the position of an item in the stack.
* @param item The item to search for.
* @return The 0-based position from the top of the stack, or -1 if
* not found.
*/
inverseEach(callback) {
this._elements.forEach(callback);
search(item) {
const index = super.search(item);
return index >= 0 ? this.internalElements.length - index - 1 : -1;
}
}
exports.default = Stack;

@@ -1,2 +0,1 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
export {};
{
"name": "@typinghare/stack-queue",
"version": "0.1.1",
"description": "A lightweight library implementing Stack and Queue for TypeScript.",
"main": "dist/main.js",
"types": "dist/main.d.ts",
"author": "James Chan",
"version": "1.0.0",
"main": "dist/index.js",
"types": "types/index.d.ts",
"type": "module",
"author": {
"name": "James Chen",
"email": "jameschan312.cn@gmail.com"
},
"homepage": "https://github.com/typinghare/stack-queue",
"email" : "jameschan312.cn@gmail.com",
"scripts": {
"test": "jest --verbose --coverage",
"publish": "npm publish --access public"
},
"keywords": [

@@ -22,11 +21,17 @@ "stack",

"devDependencies": {
"@types/jest": "^28.1.6",
"jest": "^28.1.3",
"ts-jest": "^28.0.7",
"typescript": "^4.7.4"
"@types/jest": "^29.5.12",
"dts-bundle-generator": "^9.5.1",
"jest": "^29.7.0",
"prettier": "^3.3.3",
"ts-jest": "^29.2.4",
"typescript": "^5.5.4"
},
"files": [
"src/**/*",
"types",
"dist/**/*"
]
}
],
"scripts": {
"test": "jest --verbose --coverage",
"build": "tsc && dts-bundle-generator -o types/index.d.ts src/index.ts"
}
}
export declare class EmptyStackException extends Error {
}
export declare class EmptyQueueException extends Error {
}
import Stack from './stack';
import Queue from './queue';
export { Stack, Queue };
"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;
import { SearchPredicate } from './type';
/**
* @author James Chan
*/
export default class Queue<E = any> implements Iterable<E> {
/**
* The container of elements.
* @private
*/
private _elements;
/**
* To create an empty queue.
*/
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
*/
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;
/**
* (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
*/
dequeue(count?: number): 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;
/**
* 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
*/
find(predicate: SearchPredicate<E>): number;
/**
* Returns the elements array of this queue.
* @since 0.1.0
*/
get elements(): E[];
/**
* A helper function of traversing elements in this queue.
* @param callback
* @since 0.1.0
*/
each(callback: (value: E, index?: number, array?: E[]) => void): void;
/**
* A helper function of traversing elements backward in this queue.
* @param callback
* @since 0.1.0
*/
inverseEach(callback: (value: E, index?: number, array?: E[]) => void): void;
}
import { SearchPredicate } from './type';
/**
* @author James Chan
*/
export default class Stack<E = any> implements Iterable<E> {
/**
* The container of elements.
* @private
*/
private _elements;
/**
* To create an empty stack.
*/
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
*/
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;
/**
* (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
*/
pop(count?: number): 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;
/**
* 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
*/
find(predicate: SearchPredicate<E>): number;
/**
* Returns the elements array of this stack.
* @since 0.1.0
*/
get elements(): E[];
/**
* A helper function of traversing elements in this stack.
* @param callback
* @since 0.1.0
*/
each(callback: (value: E, index?: number, array?: E[]) => void): void;
/**
* A helper function of traversing elements backward in this stack.
* @param callback
* @since 0.1.0
*/
inverseEach(callback: (value: E, index?: number, array?: E[]) => void): void;
}
export type SearchPredicate<E> = (item: E) => boolean;
export class EmptyStackException extends Error {
}
export class EmptyQueueException extends Error {
}
import Stack from './stack';
import Queue from './queue';
export { Stack, Queue };
import { EmptyQueueException } from './exceptions';
import { SearchPredicate } from './type';
/**
* @author James Chan
*/
export default class Queue<E = any> implements Iterable<E> {
/**
* The container of elements.
* @private
*/
private _elements: E[] = [];
/**
* 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];
}
}
/**
* 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;
for (let i = 0; i < count; ++i) {
item = this._elements.shift();
}
if (item === undefined) {
throw new 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
*/
public poll(): E | null {
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
*/
public element(): E {
if (this._elements.length === 0) {
throw new 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
*/
public peek(): E | null {
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
*/
public size(): number {
return this._elements.length;
}
/**
* Tests if this queue is empty.
* @return true if this queue contains no items, false otherwise
*/
public empty(): boolean {
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
*/
public search(item: E): number {
if (this.empty()) {
return -1;
}
const index = this._elements.indexOf(item);
return index == -1 ? -1 : index + 1;
}
/**
* 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;
}
/**
* Returns the elements array of this queue.
* @since 0.1.0
*/
public get elements(): E[] {
return this._elements;
}
/**
* 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);
}
/**
* 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);
}
}
}
import { EmptyStackException } from './exceptions';
import { SearchPredicate } from './type';
/**
* @author James Chan
*/
export default class Stack<E = any> implements Iterable<E> {
/**
* The container of elements.
* @private
*/
private _elements: E[] = [];
/**
* 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];
}
}
/**
* 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;
for (let i = 0; i < count; ++i) {
item = this._elements.pop();
}
if (item === undefined) {
throw new 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
*/
public poll(): E | null {
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
*/
public element(): E {
if (this._elements.length === 0) {
throw new 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
*/
public peek(): E | null {
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.
*/
public size(): number {
return this._elements.length;
}
/**
* Tests if this stack is empty.
* @return true if this stack contains no items, false otherwise
*/
public empty(): boolean {
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
*/
public search(item: E): number {
if (this._elements.length == 0) {
return -1;
}
const index = this._elements.indexOf(item);
return index == -1 ? -1 : this._elements.length - index;
}
/**
* 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;
}
/**
* Returns the elements array of this stack.
* @since 0.1.0
*/
public get elements() {
return this._elements;
}
/**
* 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);
}
}
/**
* 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);
}
}
export type SearchPredicate<E> = (item: E) => boolean;