Skip to content

Commit

Permalink
feat: Added Set::difference() method
Browse files Browse the repository at this point in the history
  • Loading branch information
rudashi committed Sep 28, 2024
1 parent 7c428ff commit 9a7df56
Show file tree
Hide file tree
Showing 2 changed files with 51 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 @@ -112,6 +112,19 @@ public function delete(mixed $value): bool
return false;
}

/**
* Returns a new Set containing elements in given Set.
* @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/difference
*
* @param \Rudashi\JavaScript\Set<TValue> $other
*
* @return \Rudashi\JavaScript\Set<TValue>
*/
public function difference(Set $other): Set
{
return new Set(array_diff($this->items, $other->items));
}

/**
* Returns Set value of each element as SetIterator.
* @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/entries
Expand Down
38 changes: 38 additions & 0 deletions tests/Unit/SetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,44 @@
});
});

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

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

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

expect($b->difference($a))
->toBeInstanceOf(Set::class)
->toMatchArray([]);
});

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

expect($a->difference($b))
->toBeInstanceOf(Set::class)
->toMatchArray([1]);
});

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

expect($b->difference($a))
->toBeInstanceOf(Set::class)
->toMatchArray([5]);
});
});

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

0 comments on commit 9a7df56

Please sign in to comment.