Skip to content

Commit 9bc712f

Browse files
committed
Streamlined duplicate() implementation with unique()
1 parent 952d1fc commit 9bc712f

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/Map.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,21 +1380,27 @@ public function dump( ?callable $callback = null ) : self
13801380
*/
13811381
public function duplicates( $col = null ) : self
13821382
{
1383-
$items = $list = $this->list();
1383+
$list = $this->list();
1384+
1385+
if( $col === null ) {
1386+
return new static( array_diff_key( $list, array_unique( $list ) ) );
1387+
}
13841388

1385-
if( $col instanceof \Closure )
1389+
if( !( $col instanceof \Closure ) )
13861390
{
1387-
$items = [];
1391+
$parts = explode( $this->sep, (string) $col );
13881392

1389-
foreach( $this->list() as $key => $item )
1390-
{
1391-
$value = (string) $col( $item, $key );
1392-
$items[$key] = $value;
1393-
}
1393+
$col = function( $item ) use ( $parts ) {
1394+
return $this->val( $item, $parts );
1395+
};
13941396
}
1395-
elseif( $col !== null )
1397+
1398+
$items = [];
1399+
1400+
foreach( $list as $key => $item )
13961401
{
1397-
$items = $this->col( $col )->toArray();
1402+
$value = $col( $item, $key );
1403+
$items[$key] = $value;
13981404
}
13991405

14001406
return new static( array_diff_key( $list, array_unique( $items ) ) );

0 commit comments

Comments
 (0)