@schukai/monster
Version:
Monster is a simple library for creating fast, robust and lightweight websites.
99 lines (89 loc) • 2.05 kB
JavaScript
/**
* Copyright © schukai GmbH and all contributing authors, {{copyRightYear}}. All rights reserved.
* Node module: @schukai/monster
*
* This source code is licensed under the GNU Affero General Public License version 3 (AGPLv3).
* The full text of the license can be found at: https://www.gnu.org/licenses/agpl-3.0.en.html
*
* For those who do not wish to adhere to the AGPLv3, a commercial license is available.
* Acquiring a commercial license allows you to use this software without complying with the AGPLv3 terms.
* For more information about purchasing a commercial license, please contact schukai GmbH.
*
* SPDX-License-Identifier: AGPL-3.0
*/
import { Base } from "./base.mjs";
import { instanceSymbol } from "../constants.mjs";
export { Stack };
/**
* You can call the method via the monster namespace `new Queue()`.
*
* @license AGPLv3
* @since 1.4.0
* @copyright schukai GmbH
*/
class Stack extends Base {
/**
*
*/
constructor() {
super();
this.data = [];
}
/**
* This method is called by the `instanceof` operator.
* @return {symbol}
* @since 2.1.0
*/
static get [instanceSymbol]() {
return Symbol.for("@schukai/monster/types/stack");
}
/**
* @return {boolean}
*/
isEmpty() {
return this.data.length === 0;
}
/**
* looks at the object at the top of this stack without removing it from the stack.
*
* @return {*}
*/
peek() {
if (this.isEmpty()) {
return undefined;
}
return this.data?.[this.data.length - 1];
}
/**
* pushes an item onto the top of this stack.
*
* @param {*} value
* @return {Queue}
*/
push(value) {
this.data.push(value);
return this;
}
/**
* remove all entries
*
* @return {Queue}
*/
clear() {
this.data = [];
return this;
}
/**
* removes the object at the top of this stack and returns
* that object as the value of this function. is the stack empty
* the return value is undefined.
*
* @return {*}
*/
pop() {
if (this.isEmpty()) {
return undefined;
}
return this.data.pop();
}
}