Skip to content

Commit

Permalink
fix(src) remove use of undefined value function, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lucatume committed Mar 18, 2024
1 parent cfa0ed9 commit 9a4ae05
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/Arrays/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
namespace StellarWP\Arrays;

use ArrayAccess;
use BadMethodCallException;
use Illuminate\Support\Enumerable;
use InvalidArgumentException;
use Throwable;

/**
* Array utilities
Expand Down Expand Up @@ -359,7 +357,7 @@ public static function filter_prefixed( array $array, string $prefix ): array {
public static function first( $array, callable $callback = null, $default = null ) {
if ( is_null( $callback ) ) {
if ( empty( $array ) ) {
return value( $default );
return $default;
}

foreach ( $array as $item ) {
Expand All @@ -373,7 +371,7 @@ public static function first( $array, callable $callback = null, $default = null
}
}

return value( $default );
return $default;
}

/**
Expand Down Expand Up @@ -693,7 +691,7 @@ public static function join( $array, $glue, $finalGlue = '' ) {
*/
public static function last( $array, callable $callback = null, $default = null ) {
if ( is_null( $callback ) ) {
return empty( $array ) ? value( $default ) : end( $array );
return empty( $array ) ? $default : end( $array );
}

return static::first( array_reverse( $array, true ), $callback, $default );
Expand Down
46 changes: 46 additions & 0 deletions tests/wpunit/ArraysTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -851,4 +851,50 @@ public function has_shape_data_provider(): array {
public function test_has_shape( $input, $shape, $strict, $expected ): void {
$this->assertEquals( $expected, Arr::has_shape( $input, $shape, $strict ) );
}

public function test_first(): void {
$this->assertEquals(
'lorem',
Arr::first(['ipsum', 'lorem', 'dolor'], fn($value) => $value === 'lorem', 'default')
);
$this->assertEquals(
'dolor',
Arr::first(['ipsum', 'lorem', 'dolor'], fn($value) => $value === 'dolor', 'default')
);
$this->assertEquals(
'default',
Arr::first(['ipsum', 'lorem', 'dolor'], fn($value) => $value === 'foo', 'default')
);
$this->assertEquals(
'default',
Arr::first(['ipsum', 'lorem', 'dolor'], fn($value) => str_starts_with($value,'p'), 'default')
);
$this->assertEquals(
'lorem',
Arr::first(['ipsum', 'lorem', 'dolor', 'loller'], fn($value) => str_starts_with($value,'l'), 'default')
);
}

public function test_last():void{
$this->assertEquals(
'lorem',
Arr::last(['ipsum', 'lorem', 'dolor'], fn($value) => $value === 'lorem', 'default')
);
$this->assertEquals(
'dolor',
Arr::last(['ipsum', 'dolor', 'lorem'], fn($value) => $value === 'dolor', 'default')
);
$this->assertEquals(
'default',
Arr::last(['ipsum', 'lorem', 'dolor'], fn($value) => $value === 'foo', 'default')
);
$this->assertEquals(
'default',
Arr::last(['ipsum', 'lorem', 'dolor'], fn($value) => str_starts_with($value,'p'), 'default')
);
$this->assertEquals(
'loller',
Arr::last(['ipsum', 'lorem', 'dolor', 'loller'], fn($value) => str_starts_with($value,'l'), 'default')
);
}
}

0 comments on commit 9a4ae05

Please sign in to comment.