Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add array_map_unique() helper #175

Merged
merged 3 commits into from
Jul 4, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions packages/framework/src/helpers.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Hyde\Framework\Hyde;
use Illuminate\Support\Collection;

if (! function_exists('hyde')) {
/**
@@ -26,3 +27,26 @@ function unslash(string $string): string
return trim($string, '/\\');
}
}

if (! function_exists('array_map_unique')) {
/**
* Map a callback over an array and remove duplicates.
*
* Important! The callback and the array parameter positions
* are reversed compared to the PHP function.
*
* Unlike array_unique, keys are reset.
*
* @param array|\Illuminate\Support\Collection $array $array
* @param callable $callback
* @return array
*/
function array_map_unique(array|Collection $array, callable $callback): array
{
if ($array instanceof Collection) {
$array = $array->toArray();
}

return array_values(array_unique(array_map($callback, $array)));
}
}
50 changes: 49 additions & 1 deletion packages/framework/tests/Feature/HelpersTest.php
Original file line number Diff line number Diff line change
@@ -31,7 +31,6 @@ public function test_unslash_function_exists()
$this->assertTrue(function_exists('unslash'));
}

// test unslash function trims trailing slashes
/** @covers ::unslash */
public function test_unslash_function_trims_trailing_slashes()
{
@@ -53,4 +52,53 @@ public function test_unslash_function_trims_trailing_slashes()
$this->assertSame('foo/bar', unslash($test));
}
}

/** @covers ::array_map_unique */
public function test_array_map_unique_function_exists()
{
$this->assertTrue(function_exists('array_map_unique'));
}

/** @covers ::array_map_unique */
public function test_array_map_unique_function_accepts_array_or_collection()
{
$array = [1, 2, 3];
$collection = collect($array);

$this->assertSame($array, array_map_unique($array, function ($item) {
return $item;
}));
$this->assertSame($array, array_map_unique($collection, function ($item) {
return $item;
}));
}

/** @covers ::array_map_unique */
public function test_array_map_unique_function_returns_unique_array()
{
$array = [1, 1, 2];

$this->assertEquals([1, 2], array_map_unique($array, function ($item) {
return $item;
}));
}

/** @covers ::array_map_unique */
public function test_array_map_unique_function_returns_reset_keys()
{
$array = [1, 2, 2, 2, 3];

$this->assertEquals([1, 2, 3], array_map_unique($array, function ($item) {
return $item;
}));
}

public function test_array_map_unique_function_handles_string_arrays()
{
$array = ['foo', 'foo', 'bar'];

$this->assertEquals(['foo', 'bar'], array_map_unique($array, function ($item) {
return $item;
}));
}
}