Skip to content

@technobuddha > library > JSON > Data Structures

Class: JSONSet<T>

Defined in: json-set.ts:28

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.

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

Type Parameters

Type ParameterDescription
T extends JsonObjectThe type of objects stored in the set. Must extend JsonObject.

Implements

Constructors

Constructor

ts
new JSONSet<T>(values?: 
  | null
| Iterable<T, any, any>): JSONSet<T>;

Defined in: src/json-set.ts:31

Parameters

ParameterType
values?| null | Iterable<T, any, any>

Returns

JSONSet<T>

Properties

PropertyModifierTypeDefault valueDescriptionDefined in
[toStringTag]readonly"JSONSet"'JSONSet'The string tag used by Object.prototype.toString for this class.src/json-set.ts:42

Accessors

size

Get Signature

ts
get size(): number;

Defined in: src/json-set.ts:52

Gets the number of elements in the set.

Returns

number

Implementation of

ts
Set.size

Methods

[iterator]()

ts
iterator: SetIterator<T>;

Defined in: src/json-set.ts:205

Returns an iterator over the values in the set.

Returns

SetIterator<T>

Implementation of

ts
Set.[iterator]

add()

ts
add(value: T): this;

Defined in: src/json-set.ts:59

Adds a serialized value to the set.

Parameters

ParameterType
valueT

Returns

this

Implementation of

ts
Set.add

clear()

ts
clear(): void;

Defined in: src/json-set.ts:67

Removes all elements from the set.

Returns

void

Implementation of

ts
Set.clear

delete()

ts
delete(value: T): boolean;

Defined in: src/json-set.ts:74

Removes the specified value from the set if it exists.

Parameters

ParameterType
valueT

Returns

boolean

Implementation of

ts
Set.delete

difference()

ts
difference<U>(other: ReadonlySetLike<U>): Set<T>;

Defined in: src/json-set.ts:81

Returns a new set containing elements present in this set but not in the other set.

Type Parameters

Type Parameter
U

Parameters

ParameterType
otherReadonlySetLike<U>

Returns

Set<T>

Implementation of

ts
Set.difference

entries()

ts
entries(): SetIterator<[T, T]>;

Defined in: src/json-set.ts:90

Returns an iterator over the set's values as [value, value] pairs.

Returns

SetIterator<[T, T]>

Implementation of

ts
Set.entries

forEach()

ts
forEach(callback: (value: T, key: T, set: Set<T>) => void, thisArg?: unknown): void;

Defined in: src/json-set.ts:99

Executes a provided function once for each value in the set.

Parameters

ParameterType
callback(value: T, key: T, set: Set<T>) => void
thisArg?unknown

Returns

void

Implementation of

ts
Set.forEach

has()

ts
has(value: T): boolean;

Defined in: src/json-set.ts:108

Determines whether the specified value exists in the set.

Parameters

ParameterType
valueT

Returns

boolean

Implementation of

ts
Set.has

intersection()

ts
intersection<U>(other: ReadonlySetLike<U>): Set<T & U>;

Defined in: src/json-set.ts:115

Returns a new set containing only the elements present in both this set and the provided set.

Type Parameters

Type Parameter
U

Parameters

ParameterType
otherReadonlySetLike<U>

Returns

Set<T & U>

Implementation of

ts
Set.intersection

isDisjointFrom()

ts
isDisjointFrom(other: ReadonlySetLike<unknown>): boolean;

Defined in: src/json-set.ts:126

Determines whether this set and the specified set have no elements in common.

Parameters

ParameterType
otherReadonlySetLike<unknown>

Returns

boolean

Implementation of

ts
Set.isDisjointFrom

isSubsetOf()

ts
isSubsetOf(other: ReadonlySetLike<unknown>): boolean;

Defined in: src/json-set.ts:138

Determines whether all elements of this set are contained in another set.

Parameters

ParameterType
otherReadonlySetLike<unknown>

Returns

boolean

Implementation of

ts
Set.isSubsetOf

isSupersetOf()

ts
isSupersetOf(other: ReadonlySetLike<unknown>): boolean;

Defined in: src/json-set.ts:150

Determines whether this set contains all elements of the specified set.

Parameters

ParameterType
otherReadonlySetLike<unknown>

Returns

boolean

Implementation of

ts
Set.isSupersetOf

keys()

ts
keys(): SetIterator<T>;

Defined in: src/json-set.ts:162

Returns an iterator over the keys in the set.

Returns

SetIterator<T>

Implementation of

ts
Set.keys

symmetricDifference()

ts
symmetricDifference<U>(other: ReadonlySetLike<U>): Set<T | U>;

Defined in: src/json-set.ts:169

Returns a new set containing elements that are in either this set or the other set, but not in both.

Type Parameters

Type Parameter
U

Parameters

ParameterType
otherReadonlySetLike<U>

Returns

Set<T | U>

Implementation of

ts
Set.symmetricDifference

union()

ts
union<U>(other: ReadonlySetLike<U>): Set<T | U>;

Defined in: src/json-set.ts:189

Returns a new set containing all unique elements from this set and another set.

Type Parameters

Type Parameter
U

Parameters

ParameterType
otherReadonlySetLike<U>

Returns

Set<T | U>

Implementation of

ts
Set.union

values()

ts
values(): SetIterator<T>;

Defined in: src/json-set.ts:196

Returns an iterator that yields each value in the set after deserialization.

Returns

SetIterator<T>

Implementation of

ts
Set.values