UNPKG

sussy-util

Version:
49 lines (48 loc) 1.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * Represents a tuple with two elements of types A and B. * @template A - The type of the first element. * @template B - The type of the second element. */ class Tuple { /** * Creates a new Tuple instance with the specified first and second elements. * @param {A} first - The first element of the tuple. * @param {B} second - The second element of the tuple. */ constructor(first, second) { this.first = first; this.second = second; /** * Swaps the first and second elements of the tuple. * @returns {Tuple<B, A>} A new Tuple with the elements swapped. */ this.swap = () => new Tuple(this.second, this.first); /** * Checks if the tuple contains a specific value. * @param {A | B} value - The value to check for in the tuple. * @returns {boolean} - True if the value is found, false otherwise. */ this.contains = (value) => { return this.first === value || this.second === value; }; /** * Returns a string representation of the tuple. * @returns {string} The string representation of the tuple. */ this.toString = () => `(${this.first}, ${this.second})`; /** * Compares this tuple to another tuple of the same type. * @param {unknown} other - The tuple to compare against. * @returns {boolean} True if both tuples have the same elements, false otherwise. */ this.equals = (other) => { if (!other || !(other instanceof Tuple)) return false; return this.first === other.first && this.second === other.second; }; Object.freeze(this); } } exports.default = Tuple;