Skip to content

Commit

Permalink
Set content is not completely unique
Browse files Browse the repository at this point in the history
We do not want to have duplicate in our Set, therefore whenever we
create a new instance of a Set we do not include in the collection
existing elements.

Fix #29
  • Loading branch information
widoz authored Feb 29, 2024
1 parent 9ef9398 commit da40b22
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
19 changes: 17 additions & 2 deletions sources/client/src/models/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class Set< T > {
readonly #data: ReadonlyArray< T >;

public constructor( data: ReadonlyArray< T > = [] ) {
this.#data = data;
this.#data = this.ensureUniqueness( data );
}

public add( value: T ): Set< T > {
Expand All @@ -29,7 +29,7 @@ export class Set< T > {
}

public has( value: T ): boolean {
return this.#data.some( ( current ) => this.isEqual( current, value ) );
return this._has( value, this.#data );
}

public map< R = T >( fn: ( value: T ) => R ): Set< R > {
Expand Down Expand Up @@ -99,4 +99,19 @@ export class Set< T > {
private isEqual( a: unknown, b: unknown ): boolean {
return _isEqual( a, b );
}

private _has( value: T, data: ReadonlyArray< T > ): boolean {
return data.some( ( current ) => this.isEqual( current, value ) );
}

private ensureUniqueness( data: ReadonlyArray< T > ): ReadonlyArray< T > {
const accumulator: Array< T > = [];
return data.reduce( ( acc, value ) => {
if ( ! this._has( value, acc ) ) {
acc.push( value );
}

return acc;
}, accumulator );
}
}
9 changes: 8 additions & 1 deletion tests/client/unit/models/set.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe( 'Set', () => {
const set1 = set.add( 1 ).add( 2 ).add( 3 );
const set2 = set.add( 2 ).add( 3 ).add( 4 );
const concat = set1.concat( set2 );
expect( concat.length() ).toBe( 6 );
expect( concat.length() ).toBe( 4 );
expect( concat.has( 1 ) ).toBe( true );
expect( concat.has( 2 ) ).toBe( true );
expect( concat.has( 3 ) ).toBe( true );
Expand Down Expand Up @@ -202,4 +202,11 @@ describe( 'Set', () => {
.add( 6 );
expect( set.equals( set2 ) ).toBe( false );
} );

it( 'Should ensure concatenating two sets keep uniqueness', () => {
const set1 = new Set< any >().add( { a: { a1: 1 } } ).add( { b: 2 } );
const set2 = new Set< any >().add( { a: { a1: 1 } } ).add( { b: 2 } );
const concat = set1.concat( set2 );
expect( concat.length() ).toBe( 2 );
} );
} );
2 changes: 1 addition & 1 deletion tests/client/unit/utils/unique-control-options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe( 'Unique Control Options', () => {
{ label: 'bar', value: 'bar' },
] );

expect( set.length() ).toBe( 4 );
expect( set.length() ).toBe( 2 );
const uniqueSet = uniqueControlOptions( set );
expect( uniqueSet.length() ).toBe( 2 );
expect( uniqueSet.first()?.value ).toBe( 'foo' );
Expand Down

0 comments on commit da40b22

Please sign in to comment.