UNPKG

sussy-util

Version:
153 lines (152 loc) 5.73 kB
import { ImprovedArray, Optional } from '.'; type CheckFunction<T> = (a: T, b: T) => boolean; type CallBack<T> = (a: T, index: number) => void; /** * A custom Set implementation that ensures unique items with flexible equality checks. * @template T - The type of items stored in the Set. */ export default class Set<T> { private items; private checkFunction; /** * Constructs a new Set instance. * @param {T[]} items - The initial items to populate the set. * @param {CheckFunction<T>} [checkFunction] - Optional equality check function. */ constructor(items: T[], checkFunction?: CheckFunction<T>); /** * Adds an item to the set if it does not already exist. * @param {T} item - The item to add. */ push: (item: T) => void; /** * Removes an item from the set. * @param {T} item - The item to remove. * @returns {boolean} - True if the item was removed, false otherwise. */ delete: (item: T) => boolean; /** * Retrieves the item at a given index as an Optional. * @param {number} index - The index of the item. * @returns {Optional<T>} - The item wrapped in an Optional. */ get: (index: number) => Optional<T>; /** * Checks if the set is empty. * @returns {boolean} - True if the set is empty, false otherwise. */ isEmpty: () => boolean; /** * Returns the number of items in the set. * @returns {number} - The number of items in the set. */ length: () => number; /** * Clears all items from the set. */ clear: () => void; /** * Creates a new Set with the same items. * @returns {Set<T>} - A clone of the current set. */ clone: () => Set<T>; /** * Removes an item at a given index. * @param {number} index - The index of the item to remove. * @returns {boolean} - True if the item was removed, false otherwise. */ remove: (index: number) => boolean; /** * Converts the set to an array. * @returns {T[]} - An array representation of the set. */ toArray: () => ImprovedArray<T>; /** * Changes the equality check function. * @param {CheckFunction<T>} predicate - The new equality check function. */ changeCheckFunction: (predicate: CheckFunction<T>) => void; /** * Returns a string representation of the set. * @returns {string} - The string representation of the set. */ toString: () => string; /** * Returns a JSON string representation of the set. * @returns {string} - The JSON string representation of the set. */ toJSONString: () => string; /** * Checks if the set contains an item. * @param {T} item - The item to check for. * @returns {boolean} - True if the item is in the set, false otherwise. */ has: (item: T) => boolean; /** * Executes a callback function for each item in the set. * @param {CallBack<T>} callback - The callback function. */ forEach: (callback: CallBack<T>) => void; /** * Executes a callback function for each item in the set (alias for `forEach`). * @param {CallBack<T>} callback - The callback function. */ each: (callback: CallBack<T>) => void; /** * Merges another set with this set. * @param {Set<T>} set - The set to merge. * @returns {Set<T>} - A new set containing all items from both sets. */ merge: (set: Set<T>) => Set<T>; /** * Returns a new set containing items present in this set but not in the provided set. * @param {Set<T>} set - The set to subtract. * @returns {Set<T>} - A new set with subtracted items. */ subtract: (set: Set<T>) => Set<T>; /** * Filters the set based on a predicate. * @param {(item: T, index: number) => boolean} predicate - The predicate function. * @returns {Set<T>} - A new set with items that satisfy the predicate. */ filter: (predicate: (item: T, index: number) => boolean) => Set<T>; /** * Reduces the set to a single value using a reducer function. * @template U * @param {(accumulator: U, currentItem: T, index: number) => U} reducer - The reducer function. * @param {U} initialValue - The initial value for the accumulator. * @returns {U} - The final accumulated value. */ reduce: <U>(reducer: (accumulator: U, currentItem: T, index: number) => U, initialValue: U) => U; /** * Maps each item in the set to a new value, ensuring uniqueness of mapped items. * @template U * @param {(item: T, index: number) => U} callback - The mapping function. * @returns {Set<U>} - A new set with mapped items. */ map: <U>(callback: (item: T, index: number) => U) => Set<U>; /** * Returns a new set containing items present in both this set and another set. * @param {Set<T>} set - The set to intersect with. * @returns {Set<T>} - A new set with intersected items. */ intersection: (set: Set<T>) => Set<T>; /** * Returns a new set containing all unique items from both sets. * @param {Set<T>} set - The set to unite with. * @returns {Set<T>} - A new set with union items. */ union: (set: Set<T>) => Set<T>; /** * Returns a new set containing items that are in either set, but not both. * @param {Set<T>} set - The set to compare with. * @returns {Set<T>} - A new set with symmetric difference items. */ symmetricDifference: (set: Set<T>) => Set<T>; /** * Enables iteration over the set using `for...of`. * @returns {Iterator<T>} - An iterator for the set. */ [Symbol.iterator](): Iterator<T>; } export {};