UNPKG

nope-js-node

Version:

NoPE Runtime for Nodejs. For Browser-Support please use nope-browser

218 lines (217 loc) 6.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LimitedList = exports.PriorityList = void 0; const arrayMethods_1 = require("./arrayMethods"); /** * A Priority List. All Items are sorted by a Priority Number. * * @export * @class PriorityList */ class PriorityList { constructor() { this._priority_list = new Array(); this._list = new Array(); this._updated = false; } /** * Function to returns a sorted List containing only the Value * * @returns {Array<T>} Sorted List containing the Values. * @memberof PriorityList */ list() { return (0, arrayMethods_1.extractListElement)(this._priority_list, "data"); } _sort() { // Sort the List based on the element priority this._priority_list.sort((0, arrayMethods_1.dynamicSort)("priority", true)); // Adapt the _list element : this._list = (0, arrayMethods_1.extractListElement)(this._priority_list, "data"); this._updated = true; } /** * Adds Data to the Priority List * @param _priority lower => lower priority * @param _data data which are stored */ push(_priority, _data) { // Add the Element with the given priority to the list this._updated = false; this._priority_list.push({ priority: _priority, data: _data }); } /** * Returns the Element with the lowest priority * * @param {boolean} [remove=true] Flag to remove the item. Defaults to true. Otherwise it remains in the list. * @return {(T | null)} * @memberof PriorityList */ highest(remove = true) { if (!this._updated) { this._sort(); } const _ret = this._priority_list[remove ? "splice" : "slice"](0, 1)[0]; return _ret ? _ret.data : null; } /** * Returns the Element with the highest priority * @param {boolean} [remove=true] Flag to remove the item. Defaults to true. Otherwise it remains in the list. * @return {(T | null)} * @memberof PriorityList */ lowest(remove = true) { if (!this._updated) { this._sort(); } let _ret = undefined; if (remove) { _ret = this._priority_list.pop(); } else { _ret = this._priority_list[this._list.length - 1]; } return _ret ? _ret.data : null; } /** * Returns the Length of the Priority list * * @readonly * @type {number} * @memberof PriorityList */ get length() { return this._priority_list.length; } } exports.PriorityList = PriorityList; /** * Limited List. This list at max contains a specific amount of elements. * After the max number of elements has been added, the first element added * will be removed. */ class LimitedList { constructor(maxLength) { this.maxLength = maxLength; this._pointer = -1; this._list = new Array(); } /** * Adds Data to the Stack. The Pointer is getting adapted. * * @param {T} data * @returns * @memberof LimitedList */ push(data) { // Check if the Maximum length is achieved if (this._list.length >= this.maxLength) { // Remove the First Element this._list = this._list.slice(1, this._pointer + 1); } // Store the Content const ret = this._list.push(data); // Adapt the Pointer this._pointer = this._list.length - 1; return ret; } /** * Contains the Length of the list. * * @readonly * @memberof LimitedList */ get length() { return this._list.length; } /** * Gets the current pointer. * * @readonly * @memberof LimitedList */ get currentPointer() { return this._pointer; } last() { if (this._list.length > 0) { this._pointer = this._list.length - 1; return this._list[this._pointer]; } // No data available. return null; } /** * Returns the Pointer to the first item. * @returns */ first() { this._pointer = this._list.length - 1; if (this._pointer >= 0 && this._pointer < this._list.length) { return this._list[this._pointer]; } // No data available. return null; } /** * Returns the last item. Adapts the pointer and the * current item is the last item. * example: * l = limited.last() * c = limited.current() * * l == c -> True * @returns The last element. */ previous() { // Check if the Pointer is in the defined Range if (this._pointer - 1 >= 0 && this._pointer - 1 < this._list.length) { return this._list[--this._pointer]; } // No data available. return null; } /** * Returns the current item, the pointer is showing at. * @returns */ current() { // Check if the Pointer is in the defined Range if (this._pointer >= 0 && this._pointer < this._list.length) { return this._list[this._pointer]; } /** No data available any more */ return null; } next() { /** Check if the Pointer is in the defined Range */ if (this._pointer + 1 >= 0 && this._pointer + 1 < this._list.length) { return this._list[++this._pointer]; } /** No data available any more */ return null; } /** * Pops the last element. If there is no element undefined is returned. * @returns The last element. */ pop(current = false) { if (current) { const ret = this._list.splice(this._pointer, 1)[0]; return ret; } const ret = this._list.pop(); // Adapt the Pointer this._pointer = this._list.length - 1; return ret; } /** * Helper to iterate over all items. * @param callbackFn * @param thisArg */ forEach(callbackFn, thisArg) { this._list.forEach(callbackFn, thisArg); } } exports.LimitedList = LimitedList;