Skip to content

Commit

Permalink
Add uniqWith
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbiller committed Mar 16, 2017
1 parent a7275c1 commit 9755c5e
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ You can also check code coverage by running ```composer run test:coverage```.
- first
- last
- uniq
- uniqWith
- Function composition
- flow
- compose
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"src/reverse.php",
"src/some.php",
"src/uniq.php",
"src/uniqWith.php",
"src/zip.php"
]
},
Expand Down
12 changes: 4 additions & 8 deletions src/uniq.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@
*/
function uniq(...$args) {
$uniq = function (array $collection) {
$uniqArray = [];
$isEqual = function ($a, $b) {
return $a == $b;
};

foreach($collection as $item) {
if (!includes($item, $uniqArray)) {
$uniqArray[] = $item;
}
}

return $uniqArray;
return uniqWith($isEqual, $collection);
};

return curryN($uniq, 1)(...$args);
Expand Down
37 changes: 37 additions & 0 deletions src/uniqWith.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace fphp;

function uniqWith(...$args) {

/**
* Return an array without duplicates using a comparator function
*
* @param Callable $comparator comparator function
* @param array $collection collection
* @return array
* @author Rémy Peru <peru.remy@gmail.com>
*/
$uniqWith = function (Callable $comparator, array $collection) {
$uniqArray = [];

foreach($collection as $item) {
$isUnique = true;

for ($i = 0; $i < count($uniqArray); $i++) {
if ($comparator($item, $uniqArray[$i])) {
$isUnique = false;
break;
}
}

if ($isUnique) {
$uniqArray[] = $item;
}
}

return $uniqArray;
};

return curryN($uniqWith, 2)(...$args);
}
54 changes: 54 additions & 0 deletions tests/uniqWith.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

use function fphp\uniqWith;

describe('uniqWith', function () {

beforeEach(function () {
$this->comparator = function($a, $b) {
return $a === $b;
};
});

it('should return input array without duplicates', function () {
$res = uniqWith($this->comparator, [1, 2, 3, 3 ,4 ,5 , 'orange', 'green', 'orange']);

expect($res)->toBe([1, 2, 3, 4 ,5 , 'orange', 'green']);
});

it('should be curried', function () {
$res = uniqWith($this->comparator);
$res = $res([1, 2, 3, 3 ,4 ,5 , 'orange', 'green', 'orange']);

expect($res)->toBe([1, 2, 3, 4 ,5 , 'orange', 'green']);
});

it('should work with non contiguous arrays', function () {
$res = uniqWith($this->comparator, [0 => 1, 4 => 2, 8 => 2]);

expect($res)->toBe([1, 2]);
});

it('should work for my use case', function () {
$order = [
'items' => [
['user' => '1444', 'ticket' => '2ae6eeb0-0a75-11e7-93ae-92361f002671'],
['user' => '1222', 'ticket' => '2ae6eeb0-0a75-11e7-93ae-92361f002671'],
['user' => '1444', 'ticket' => '2ae6eeb0-0a75-11e7-93ae-92361f002671']
]
];

$comparator = function ($a, $b) {
return $a['user'] == $b['user'] && $a['ticket'] == $b['ticket'];
};

$expected = [
['user' => '1444', 'ticket' => '2ae6eeb0-0a75-11e7-93ae-92361f002671'],
['user' => '1222', 'ticket' => '2ae6eeb0-0a75-11e7-93ae-92361f002671']
];

$res = uniqWith($comparator, $order['items']);

expect($res)->toBe($expected);
});
});

0 comments on commit 9755c5e

Please sign in to comment.