Skip to content

Commit

Permalink
Merge pull request #9 from samsonasik/add-returnKey-flag
Browse files Browse the repository at this point in the history
Add returnKey flag on Finder to allow return key instead of row
  • Loading branch information
samsonasik authored Jan 12, 2023
2 parents 78165e3 + 8d18e27 commit cd0666c
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 12 deletions.
39 changes: 31 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ $filter = static fn($datum): bool => $datum === 4;

var_dump(\ArrayLookup\AtLeast::once($data, $filter)) // false

// WITH key array included
// WITH key array included, pass $key variable as 2nd arg on filter to be used in filter

$data = [1, 2, 3];
$filter = static fn($datum, $key): bool => $datum === 1 && $key >= 0;
Expand Down Expand Up @@ -77,7 +77,7 @@ $filter = static fn($datum): bool => $datum === 1;

var_dump(\ArrayLookup\AtLeast::twice($data, $filter)) // false

// WITH key array included
// WITH key array included, pass $key variable as 2nd arg on filter to be used in filter

$data = [1, "1", 3];
$filter = static fn($datum, $key): bool => $datum == 1 && $key >= 0;
Expand Down Expand Up @@ -107,7 +107,7 @@ $times = 3;

var_dump(\ArrayLookup\AtLeast::times($data, $filter, $times)) // false

// WITH key array included
// WITH key array included, pass $key variable as 2nd arg on filter to be used in filter

$data = [false, null, 0];
$filter = static fn($datum, $key): bool => ! $datum && $key >= 0;
Expand Down Expand Up @@ -141,7 +141,7 @@ $filter = static fn($datum): bool => $datum == 1;

var_dump(\ArrayLookup\Only::once($data, $filter)) // false

// WITH key array included
// WITH key array included, pass $key variable as 2nd arg on filter to be used in filter

$data = [1, 2, 3];
$filter = static fn($datum, $key): bool => $datum === 1 && $key >= 0;
Expand Down Expand Up @@ -170,7 +170,7 @@ $filter = static fn($datum): bool => (bool) $datum;

var_dump(\ArrayLookup\Only::twice($data, $filter)) // false

// WITH key array included
// WITH key array included, pass $key variable as 2nd arg on filter to be used in filter

$data = [1, "1", 3];
$filter = static fn($datum, $key): bool => $datum == 1 && $key >= 0;
Expand Down Expand Up @@ -201,7 +201,7 @@ $times = 2;

var_dump(\ArrayLookup\Only::times($data, $filter, $times)) // false

// WITH key array included
// WITH key array included, pass $key variable as 2nd arg on filter to be used in filter

$data = [false, null, 1];
$filter = static fn($datum, $key): bool => ! $datum && $key >= 0;
Expand Down Expand Up @@ -233,7 +233,16 @@ var_dump(\ArrayLookup\Finder::first($data, $filter)) // 1
$filter = static fn($datum): bool => $datum == 1000;
var_dump(\ArrayLookup\Finder::first($data, $filter)) // null

// WITH key array included
// RETURN the Array key, pass true to 3rd arg

$filter = static fn($datum): bool => $datum === 1;

var_dump(\ArrayLookup\Finder::first($data, $filter, true)) // 0

$filter = static fn($datum): bool => $datum == 1000;
var_dump(\ArrayLookup\Finder::first($data, $filter, true)) // null

// WITH key array included, pass $key variable as 2nd arg on filter to be used in filter

$filter = static fn($datum, $key): bool => $datum === 1 && $key >= 0;

Expand All @@ -259,7 +268,21 @@ var_dump(\ArrayLookup\Finder::last(
static fn ($datum): bool => $datum < 5
)); // null

// WITH key array included
// RETURN the Array key, pass true to 3rd arg

var_dump(\ArrayLookup\Finder::last(
$data,
static fn ($datum): bool => $datum > 5,
true
)); // 3

var_dump(\ArrayLookup\Finder::last(
$data,
static fn ($datum): bool => $datum < 5,
true
)); // null

// WITH key array included, pass $key variable as 2nd arg on filter to be used in filter

var_dump(\ArrayLookup\Finder::last(
$data,
Expand Down
8 changes: 4 additions & 4 deletions src/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class Finder
* @param array<int|string, mixed>|Traversable<int|string, mixed> $data
* @param callable(mixed $datum, int|string|null $key=): bool $filter
*/
public static function first(iterable $data, callable $filter): mixed
public static function first(iterable $data, callable $filter, bool $returnKey = false): mixed
{
foreach ($data as $key => $datum) {
$isFound = $filter($datum, $key);
Expand All @@ -33,7 +33,7 @@ public static function first(iterable $data, callable $filter): mixed
continue;
}

return $datum;
return $returnKey ? $key : $datum;
}

return null;
Expand All @@ -56,7 +56,7 @@ private static function resolveArrayFromTraversable(Traversable $traversable): a
* @param array<int|string, mixed>|Traversable<int|string, mixed> $data
* @param callable(mixed $datum, int|string|null $key=): bool $filter
*/
public static function last(iterable $data, callable $filter): mixed
public static function last(iterable $data, callable $filter, bool $returnKey = false): mixed
{
// convert to array when data is Traversable instance
if ($data instanceof Traversable) {
Expand Down Expand Up @@ -94,7 +94,7 @@ public static function last(iterable $data, callable $filter): mixed
continue;
}

return $current;
return $returnKey ? $key : $current;
}

return null;
Expand Down
111 changes: 111 additions & 0 deletions tests/FinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,43 @@ public function firstDataProvider(): array
];
}

/**
* @dataProvider firstReturnKeyDataProvider
*/
public function testFirstReturnKey(iterable $data, callable $filter, mixed $expected): void
{
$this->assertSame(
$expected,
Finder::first($data, $filter, true)
);
}

public function firstReturnKeyDataProvider(): array
{
return [
[
[1, 2, 3],
static fn($datum): bool => $datum === 2,
1,
],
[
[1, "1", 3],
static fn($datum): bool => $datum === 1000,
null,
],
[
['abc test', 'def', 'some test'],
static fn(string $datum, int $key): bool => str_contains($datum, 'test') && $key >= 0,
0,
],
[
['abc test', 'def', 'some test'],
static fn(string $datum, int $key): bool => str_contains($datum, 'test') && $key === 1,
null,
],
];
}

/**
* @dataProvider lastDataProvider
*/
Expand Down Expand Up @@ -124,4 +161,78 @@ public function lastDataProvider(): array
],
];
}

/**
* @dataProvider lastReturnKeyDataProvider
*/
public function testLastReturnKey(iterable $data, callable $filter, mixed $expected): void
{
$this->assertSame(
$expected,
Finder::last($data, $filter, true)
);
}

public function lastReturnKeyDataProvider(): array
{
$generator = static function (): Generator {
yield 6;
yield 7;
yield 8;
yield 9;
};

return [
[
[6, 7, 8, 9],
static fn($datum): bool => $datum > 5,
3,
],
[
[6, 7, 8, 9],
static fn($datum): bool => $datum < 5,
null,
],
[
new ArrayIterator([6, 7, 8, 9]),
static fn($datum): bool => $datum > 5,
3,
],
[
new ArrayIterator([6, 7, 8, 9]),
static fn($datum): bool => $datum < 5,
null,
],
[
new ArrayObject([6, 7, 8, 9]),
static fn($datum): bool => $datum > 5,
3,
],
[
new ArrayObject([6, 7, 8, 9]),
static fn($datum): bool => $datum < 5,
null,
],
[
$generator(),
static fn($datum): bool => $datum > 5,
3,
],
[
$generator(),
static fn($datum): bool => $datum < 5,
null,
],
[
['abc test', 'def', 'some test'],
static fn(string $datum, int $key): bool => str_contains($datum, 'test') && $key >= 0,
2,
],
[
['abc test', 'def', 'some test'],
static fn(string $datum, int $key): bool => str_contains($datum, 'test') && $key === 1,
null,
],
];
}
}

0 comments on commit cd0666c

Please sign in to comment.