Skip to content

Commit 7115eae

Browse files
committed
Improved duplicate() and unique() implementation
1 parent 9bc712f commit 7115eae

File tree

1 file changed

+28
-42
lines changed

1 file changed

+28
-42
lines changed

src/Map.php

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,30 +1380,13 @@ public function dump( ?callable $callback = null ) : self
13801380
*/
13811381
public function duplicates( $col = null ) : self
13821382
{
1383-
$list = $this->list();
1383+
$list = $map = $this->list();
13841384

1385-
if( $col === null ) {
1386-
return new static( array_diff_key( $list, array_unique( $list ) ) );
1385+
if( $col !== null ) {
1386+
$map = array_map( $this->mapper( $col ), array_values( $list ), array_keys( $list ) );
13871387
}
13881388

1389-
if( !( $col instanceof \Closure ) )
1390-
{
1391-
$parts = explode( $this->sep, (string) $col );
1392-
1393-
$col = function( $item ) use ( $parts ) {
1394-
return $this->val( $item, $parts );
1395-
};
1396-
}
1397-
1398-
$items = [];
1399-
1400-
foreach( $list as $key => $item )
1401-
{
1402-
$value = $col( $item, $key );
1403-
$items[$key] = $value;
1404-
}
1405-
1406-
return new static( array_diff_key( $list, array_unique( $items ) ) );
1389+
return new static( array_diff_key( $list, array_unique( $map ) ) );
14071390
}
14081391

14091392

@@ -5759,28 +5742,10 @@ public function unique( $col = null ) : self
57595742
return new static( array_unique( $this->list() ) );
57605743
}
57615744

5762-
if( !( $col instanceof \Closure ) )
5763-
{
5764-
$parts = explode( $this->sep, (string) $col );
5765-
5766-
$col = function( $item ) use ( $parts ) {
5767-
return $this->val( $item, $parts );
5768-
};
5769-
}
5770-
5771-
$list = $unique = [];
5772-
5773-
foreach( $this->list() as $key => $item )
5774-
{
5775-
$value = (string) $col( $item, $key );
5776-
5777-
if( !isset( $unique[$value] ) ) {
5778-
$unique[$value] = true;
5779-
$list[$key] = $item;
5780-
}
5781-
}
5745+
$list = $this->list();
5746+
$map = array_map( $this->mapper( $col ), array_values( $list ), array_keys( $list ) );
57825747

5783-
return new static( $list );
5748+
return new static( array_intersect_key( $list, array_unique( $map ) ) );
57845749
}
57855750

57865751

@@ -6170,6 +6135,27 @@ protected function &list() : array
61706135
}
61716136

61726137

6138+
/**
6139+
* Returns a closure that retrieves the value for the passed key
6140+
*
6141+
* @param \Closure|string|null $key Closure or key (e.g. "key1/key2/key3") to retrieve the value for
6142+
* @return \Closure Closure that retrieves the value for the passed key
6143+
*/
6144+
protected function mapper( $key = null, bool $cast = false ) : \Closure
6145+
{
6146+
if( $key instanceof \Closure ) {
6147+
return $key;
6148+
}
6149+
6150+
$parts = $key ? explode( $this->sep, (string) $key ) : [];
6151+
6152+
return function( $item ) use ( $cast, $parts ) {
6153+
$val = $this->val( $item, $parts );
6154+
return $cast ? (string) $val : $val;
6155+
};
6156+
}
6157+
6158+
61736159
/**
61746160
* Returns a configuration value from an array.
61756161
*

0 commit comments

Comments
 (0)