Skip to content

Commit eb4b431

Browse files
committed
Always preserve keys and use first item in unique()
1 parent 0b10b88 commit eb4b431

File tree

3 files changed

+28
-21
lines changed

3 files changed

+28
-21
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6947,7 +6947,7 @@ Two elements are considered equal if comparing their string representions return
69476947
(string) $elem1 === (string) $elem2
69486948
```
69496949

6950-
The keys of the elements are only preserved in the new map if the first parameter is NULL.
6950+
The keys of the elements are preserved in the new map.
69516951

69526952
**Examples:**
69536953

@@ -6956,13 +6956,13 @@ Map::from( [0 => 'a', 1 => 'b', 2 => 'b', 3 => 'c'] )->unique();
69566956
// [0 => 'a', 1 => 'b', 3 => 'c']
69576957

69586958
Map::from( [['p' => '1'], ['p' => 1], ['p' => 2]] )->unique( 'p' );
6959-
// [['p' => 1], ['p' => 2]]
6959+
// [0 => ['p' => '1'], 2 => ['p' => 2]]
69606960

69616961
Map::from( [['i' => ['p' => '1']], ['i' => ['p' => 1]]] )->unique( 'i/p' );
6962-
// [['i' => ['p' => 1]]]
6962+
// [0 => ['i' => ['p' => '1']]]
69636963

69646964
Map::from( [['i' => ['p' => '1']], ['i' => ['p' => 1]]] )->unique( fn( $item, $key ) => $item['i']['p'] );
6965-
// [['i' => ['p' => 1]]]
6965+
// [0 => ['i' => ['p' => '1']]]
69666966
```
69676967

69686968
**See also:**

src/Map.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5736,8 +5736,8 @@ public function union( iterable $elements ) : self
57365736
* Results:
57375737
* [0 => 'a', 1 => 'b', 3 => 'c']
57385738
* [['p' => 1], ['p' => 2]]
5739-
* [['i' => ['p' => 1]]]
5740-
* [['i' => ['p' => 1]]]
5739+
* [['i' => ['p' => '1']]]
5740+
* [['i' => ['p' => '1']]]
57415741
*
57425742
* Two elements are considered equal if comparing their string representions returns TRUE:
57435743
* (string) $elem1 === (string) $elem2
@@ -5749,25 +5749,32 @@ public function union( iterable $elements ) : self
57495749
*/
57505750
public function unique( $col = null ) : self
57515751
{
5752-
if( $col instanceof \Closure )
5752+
if( $col === null ) {
5753+
return new static( array_unique( $this->list() ) );
5754+
}
5755+
5756+
if( !( $col instanceof \Closure ) )
57535757
{
5754-
$unique = [];
5758+
$parts = explode( $this->sep, (string) $col );
57555759

5756-
foreach( $this->list() as $key => $item )
5757-
{
5758-
$value = (string) $col( $item, $key );
5759-
$unique[$value] = $item;
5760-
}
5760+
$col = function( $item ) use ( $parts ) {
5761+
return $this->val( $item, $parts );
5762+
};
5763+
}
57615764

5762-
return new static( array_values( $unique ) );
5765+
$list = $unique = [];
57635766

5764-
}
5767+
foreach( $this->list() as $key => $item )
5768+
{
5769+
$value = (string) $col( $item, $key );
57655770

5766-
if( $col !== null ) {
5767-
return $this->col( null, $col )->values();
5771+
if( !isset( $unique[$value] ) ) {
5772+
$unique[$value] = true;
5773+
$list[$key] = $item;
5774+
}
57685775
}
57695776

5770-
return new static( array_unique( $this->list() ) );
5777+
return new static( $list );
57715778
}
57725779

57735780

tests/MapTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3784,7 +3784,7 @@ public function testUniqueKey()
37843784
$r = $m->unique( 'p' );
37853785

37863786
$this->assertInstanceOf( Map::class, $r );
3787-
$this->assertSame( [['p' => 1], ['p' => 2]], $r->toArray() );
3787+
$this->assertSame( [0 => ['p' => '1'], 2 => ['p' => 2]], $r->toArray() );
37883788
}
37893789

37903790

@@ -3794,7 +3794,7 @@ public function testUniquePath()
37943794
$r = $m->unique( 'i/p' );
37953795

37963796
$this->assertInstanceOf( Map::class, $r );
3797-
$this->assertSame( [['i' => ['p' => 1]]], $r->toArray() );
3797+
$this->assertSame( [0 => ['i' => ['p' => '1']]], $r->toArray() );
37983798
}
37993799

38003800

@@ -3804,7 +3804,7 @@ public function testUniqueClosure()
38043804
$r = $m->unique( fn( $item, $key ) => $item['i']['p'] );
38053805

38063806
$this->assertInstanceOf( Map::class, $r );
3807-
$this->assertSame( [['i' => ['p' => 1]]], $r->toArray() );
3807+
$this->assertSame( [0 => ['i' => ['p' => '1']]], $r->toArray() );
38083808
}
38093809

38103810

0 commit comments

Comments
 (0)