import { ConstructorValues , ForEachCallback } from "@usys/collections-ts/common" ; import Set = require ( "@usys/collections-ts/set" ) ; import stringify from "json-stable-stringify" ; function equals < T > ( a , b ) { return stringify ( a ) === stringify ( b ) ; } function hash ( obj ) { return stringify ( obj ) ; } export class ObjectSet < T extends object > { readonly store_ : Set < T > ; constructor ( values ? , public defaults ? ) { this . store_ = new Set ( undefined , equals , hash ) ; if ( values != null ) { values . forEach ( ( val ) => this . store_ . add ( this . withDefaults ( val ) ) ) ; } } get length ( ) : number { return this . store_ . length ; } add ( value ) { return this . store_ . add ( this . withDefaults ( value ) ) ; } has ( value ) { return this . store_ . has ( this . withDefaults ( value ) ) ; } clear ( ) { this . store_ . clear ( ) ; } delete ( value ) { return this . store_ . delete ( this . withDefaults ( value ) ) ; } forEach < Thisp = undefined > ( callback , thisp ? ) { this . store_ . forEach ( ( value , key ) => { callback . call ( thisp , value , key , this ) ; } ) ; } withDefaults ( value ) { return this . defaults ? { ... this . defaults , ... value } : value ; } [ Symbol . iterator ] ( ) { return this . store_ . iterator ( ) ; } }