Skip to content

Commit

Permalink
feat: Added Set::union() method
Browse files Browse the repository at this point in the history
  • Loading branch information
rudashi committed Sep 28, 2024
1 parent 9a7df56 commit e5d4d31
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/JavaScript/Set.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,19 @@ public function toArray(): array
return $this->items;
}

/**
* Merge the Set with the given Set.
* @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/union
*
* @param \Rudashi\JavaScript\Set<TValue> $other
*
* @return \Rudashi\JavaScript\Set<TValue>
*/
public function union(Set $other): Set
{
return new Set([...$this->items, ...$other->items]);
}

/**
* Returns Set values as SetIterator.
* @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/values
Expand Down
29 changes: 29 additions & 0 deletions tests/Unit/SetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,35 @@
});
});

describe('union', function () {
test('merge values', function () {
$odds = new Set([1, 3, 5, 7, 9]);
$squares = new Set([1, 4, 9]);

expect($odds->union($squares))
->toBeInstanceOf(Set::class)
->toMatchArray([1, 3, 5, 7, 9, 4]);
});

test('`a` plus `b`', function () {
$a = new Set([1, 2, 3, 4]);
$b = new Set([5, 4, 3, 2]);

expect($a->union($b))
->toBeInstanceOf(Set::class)
->toMatchArray([1, 2, 3, 4, 5]);
});

test('`b` plus `a`', function () {
$a = new Set([1, 2, 3, 4]);
$b = new Set([5, 4, 3, 2]);

expect($b->union($a))
->toBeInstanceOf(Set::class)
->toMatchArray([5, 4, 3, 2, 1]);
});
});

describe('values', function () {
it('returns new SetIterator', function () {
$set = new Set(['foo', 'bar']);
Expand Down

0 comments on commit e5d4d31

Please sign in to comment.