From 23af70772f74ee5831a51b7a256e2cc177f3f32a Mon Sep 17 00:00:00 2001 From: buzz-lightsnack-2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Wed, 28 Aug 2024 14:59:57 +0800 Subject: [PATCH] add JavaScript code for Collection (untested) --- src/javascript/collection.js | 63 ++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/javascript/collection.js diff --git a/src/javascript/collection.js b/src/javascript/collection.js new file mode 100644 index 0000000..429b5e9 --- /dev/null +++ b/src/javascript/collection.js @@ -0,0 +1,63 @@ +/* +IPseudoADS Queue +Pseudocode-like collection management for JavaScript + +by buzz-ligthsnack-2007 +*/ + +class Collection { + #contents = []; + #cursor = -1; + + /* + Create a new collection. + */ + constructor() {}; + + /* + Add an item to the end of the collection. + + @param {Object} data the data to be added + */ + addItem(data) { + this.#contents.push(data); + }; + + /* + Reset cursor to be to the start of the collection. + */ + resetNext() { + this.#cursor = -1; + }; + + /* + Test: next item exists. + + @return {bool} existence of the item + */ + hasNext() { + return(this.#contents.length <= (this.#cursor + 1)) + }; + + /* + Get the next item of the queue. + + @return {Object} the next item + */ + getNext() { + this.#cursor++; + return(this.#contents[this.#cursor]); + }; + + /* + Test: collection contains no elements + + @return {bool} test result + */ + isEmpty() { + return(this.#contents.length <= 0); + } +} + + +export {Collection as default};