Skip to content

Commit 82a78e7

Browse files
committed
Fixed potential bugs
1 parent 4ce21f7 commit 82a78e7

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

src/Map.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,11 @@ public function avg( $col = null ) : float
690690
*/
691691
public function before( $value ) : self
692692
{
693-
return new static( array_slice( $this->list(), 0, $this->pos( $value ), true ) );
693+
if( ( $pos = $this->pos( $value ) ) === null ) {
694+
return new static();
695+
}
696+
697+
return new static( array_slice( $this->list(), 0, $pos, true ) );
694698
}
695699

696700

@@ -3302,7 +3306,7 @@ public function map( callable $callback ) : self
33023306
public function max( $col = null )
33033307
{
33043308
$list = $this->list();
3305-
$vals = array_filter( $col ? array_map( $this->mapper( $col ), $list, array_keys( $list ) ) : $list );
3309+
$vals = array_filter( $col ? array_map( $this->mapper( $col ), $list, array_keys( $list ) ) : $list, fn( $v ) => $v !== null );
33063310

33073311
return !empty( $vals ) ? max( $vals ) : null;
33083312
}
@@ -3379,7 +3383,7 @@ public function merge( iterable $elements, bool $recursive = false ) : self
33793383
public function min( $col = null )
33803384
{
33813385
$list = $this->list();
3382-
$vals = array_filter( $col ? array_map( $this->mapper( $col ), $list, array_keys( $list ) ) : $list );
3386+
$vals = array_filter( $col ? array_map( $this->mapper( $col ), $list, array_keys( $list ) ) : $list, fn( $v ) => $v !== null );
33833387

33843388
return !empty( $vals ) ? min( $vals ) : null;
33853389
}
@@ -3810,10 +3814,13 @@ public function pos( $value ) : ?int
38103814
return null;
38113815
}
38123816

3813-
if( ( $key = array_search( $value, $list, true ) ) !== false
3814-
&& ( $pos = array_search( $key, array_keys( $list ), true ) ) !== false
3815-
) {
3816-
return $pos;
3817+
foreach( $list as $key => $item )
3818+
{
3819+
if( $item === $value ) {
3820+
return $pos;
3821+
}
3822+
3823+
++$pos;
38173824
}
38183825

38193826
return null;
@@ -6291,7 +6298,7 @@ public function where( string $key, string $op, $value ) : self
62916298
{
62926299
case '-':
62936300
$list = (array) $value;
6294-
return $val >= current( $list ) && $val <= end( $list );
6301+
return !empty( $list ) && $val >= reset( $list ) && $val <= end( $list );
62956302
case 'in': return in_array( $val, (array) $value );
62966303
case '<': return $val < $value;
62976304
case '>': return $val > $value;

0 commit comments

Comments
 (0)