UNPKG

sussy-util

Version:
173 lines (172 loc) 5.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Error_1 = require("../Error"); const _1 = require("."); class Set { constructor(...items) { this.items = new _1.ImprovedArray(); this.checkFunction = (arrayParam, newItem) => arrayParam === newItem; for (let index = 0; index < items.length; index++) { const item = items.shift(); if (item && !items.includes(item)) { this.items.push(item); } } } /** * If the item is not already in the array, add it * @param {T} item - T - The item to be pushed into the array. */ push(item) { if (!this.items.some((item2) => this.checkFunction(item2, item))) { this.items.push(item); } } /** * finds and removes the item from the Set * @param {T} item - T - The item to delete */ delete(item) { this.items.find((v, i) => { if (v === item) { this.items.remove(i); } }); } /** * @param {number} index - number - The index of the item to get * @returns The item at the index. */ get(index) { if (index < 0 || index >= this.length()) { throw new Error_1.IndexOutOfBoundsError(`Index: ${index} is out of bounds for length ${this.length()}`); } return this.items[index]; } /** * Returns true if the set is empty, false otherwise * @returns The return type is boolean. */ isEmpty() { return this.items.isEmpty(); } /** * The length function returns the length of the items array * @returns The length of the items array. */ length() { return this.items.length; } /** * The clear() function clears the items in the Set */ clear() { this.items.clear(); } /** * The clone function creates a new Set object and passes the items of the current Set object to * the new Set object * @returns A new Set object with the same items as the original Set object. */ clone() { return new Set(...this.items); } /** * The remove function removes an item from the list at the specified index. * @param {number} index - number - The index of the item to remove. */ remove(index) { if (index < 0 || index >= this.length()) { throw new Error_1.IndexOutOfBoundsError(`Index: ${index} is out of bounds for length ${this.length()}`); } this.items.remove(index); } /** * The spread operator (...) is used to convert the Set to an array * @returns An array of the items in the set. */ toArray() { return [...this.items]; } /** * This function changes the check function to the one passed in as a parameter. * @param predicate - (a: T, b: T) => boolean */ changeCheckFunction(predicate) { this.checkFunction = predicate; } toString() { return `Set: ${this.items.toString()}`; } /** * It takes the items array and converts it to a JSON string. * @returns The JSON string representation of the items array. */ toJSONString() { return JSON.stringify(this.items); } /** * Returns true if the Set contains the specified item, false otherwise. * @param item - The item to search for in the Set. * @returns A boolean indicating whether the item exists in the Set. */ has(item) { return this.items.some((item2) => this.checkFunction(item2, item)); } /** * Executes a provided callback function once for each item in the Set. * @param callback - A function to execute for each item in the Set. */ forEach(callback) { this.items.forEach(callback); } /* shorthand for `forEach` */ each(callbackfn) { this.forEach(callbackfn); } ; /** * Returns a new Set that contains the items from both the current Set and another Set. * @param set - The Set to merge with the current Set. * @returns A new Set that contains items from both Sets. */ merge(set) { const mergedSet = new Set(...this.items); set.each((item) => mergedSet.push(item)); return mergedSet; } /** * Returns a new Set that contains items from the current Set that are not present in another Set. * @param set - The Set to subtract from the current Set. * @returns A new Set that contains items from the current Set excluding those present in the other Set. */ subtract(set) { const subtractedSet = new Set(); this.each((item) => { if (!set.has(item)) { subtractedSet.push(item); } }); return subtractedSet; } [Symbol.iterator]() { let index = 0; return { next: () => { if (index < this.items.length) { return { value: this.items[index++], done: false, }; } else { return { value: undefined, done: true, }; } } }; } } exports.default = Set;