@technobuddha/library
Version:
A large library of useful functions
101 lines (100 loc) • 3.59 kB
TypeScript
import { type JsonObject } from 'type-fest';
/**
* A Set-like collection for objects that can be serialized to JSON.
*
* `JSONSet` stores objects by serializing them to JSON strings, allowing for deep equality
* comparison of objects rather than reference equality. This is useful for storing and comparing
* objects with the same structure and values, regardless of their references.
* @typeParam T - The type of objects stored in the set. Must extend `JsonObject`.
* @example
* ```typescript
* const set = new JSONSet<{ a: number }>();
* set.add({ a: 1 });
* set.has({ a: 1 }); // true
* set.has({ a: 2 }); // false
* ```
* @remarks
* - All objects are serialized using a `serialize` function and deserialized with a `deserialize` function.
* - The set supports standard set operations such as union, intersection, difference, and symmetricDifference.
* - Iteration yields deserialized objects.
* @see Set
* @group JSON
* @category Data Structures
*/
export declare class JSONSet<T extends JsonObject> implements Set<T> {
protected set: Set<string>;
constructor(values?: Iterable<T> | null);
/**
* The string tag used by Object.prototype.toString for this class.
*/
readonly [Symbol.toStringTag] = "JSONSet";
protected replicate<X = T>(values?: Iterable<X> | null): Set<X>;
/**
* Gets the number of elements in the set.
*/
get size(): number;
/**
* Adds a serialized value to the set.
*/
add(value: T): this;
/**
* Removes all elements from the set.
*/
clear(): void;
/**
* Removes the specified value from the set if it exists.
*/
delete(value: T): boolean;
/**
* Returns a new set containing elements present in this set but not in the other set.
*/
difference<U>(other: ReadonlySetLike<U>): Set<T>;
/**
* Returns an iterator over the set's values as [value, value] pairs.
*/
entries(): SetIterator<[T, T]>;
/**
* Executes a provided function once for each value in the set.
*/
forEach(callback: (value: T, key: T, set: Set<T>) => void, thisArg?: unknown): void;
/**
* Determines whether the specified value exists in the set.
*/
has(value: T): boolean;
/**
* Returns a new set containing only the elements present in both this set and the provided set.
*/
intersection<U>(other: ReadonlySetLike<U>): Set<T & U>;
/**
* Determines whether this set and the specified set have no elements in common.
*/
isDisjointFrom(other: ReadonlySetLike<unknown>): boolean;
/**
* Determines whether all elements of this set are contained in another set.
*/
isSubsetOf(other: ReadonlySetLike<unknown>): boolean;
/**
* Determines whether this set contains all elements of the specified set.
*/
isSupersetOf(other: ReadonlySetLike<unknown>): boolean;
/**
* Returns an iterator over the keys in the set.
*/
keys(): SetIterator<T>;
/**
* Returns a new set containing elements that are in either this set or the other set, but not in both.
*/
symmetricDifference<U>(other: ReadonlySetLike<U>): Set<T | U>;
/**
* Returns a new set containing all unique elements from this set and another set.
*/
union<U>(other: ReadonlySetLike<U>): Set<T | U>;
/**
* Returns an iterator that yields each value in the set after deserialization.
*/
values(): SetIterator<T>;
/**
* Returns an iterator over the values in the set.
*/
[Symbol.iterator](): SetIterator<T>;
}