Skip to content

Commit 986674d

Browse files
committed
Added Arr::unique and Collection::unique
1 parent 383f1cd commit 986674d

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

src/Arr.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,19 @@ public static function filter($array, callable $callback)
109109
return array_filter($array, $callback, ARRAY_FILTER_USE_BOTH);
110110
}
111111

112+
/**
113+
* Get a subset of unique items from $array.
114+
*
115+
* @param ArrayAccess|array $array
116+
* @param int $flag
117+
*
118+
* @return array
119+
*/
120+
public static function unique($array, $flag = SORT_STRING)
121+
{
122+
return array_unique($array, $flag);
123+
}
124+
112125
/**
113126
* @param ArrayAccess|array $array
114127
* @param string|int $key

src/Collection.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,17 @@ public function filter(callable $callback = null)
173173
return new static(array_filter($this->items));
174174
}
175175

176+
/**
177+
* Get a subset of unique items from Collection.
178+
*
179+
* @param int $flag
180+
* @return static
181+
*/
182+
public function unique($flag = SORT_REGULAR)
183+
{
184+
return new static(Arr::unique($this->items, $flag));
185+
}
186+
176187
/**
177188
* Search the collection and return the first corresponding item if successful.
178189
* If $needle is a callable then return the first item where the callable

tests/Unit/ArrTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,17 @@ public function testFilter()
126126
}));
127127
}
128128

129+
public function unique()
130+
{
131+
$completeArray = [3,3, 5,5,5,5, 7,7,7,7,7,7, 9,9,9,9,9,9,9,9];
132+
$filteredArray = [3, 5, 7, 9];
133+
134+
$this->assertEquals($filteredArray, Arr::unique($completeArray));
135+
136+
$completeDots = new Dots($completeArray);
137+
$this->assertEquals($filteredArray, Arr::unique($completeDots));
138+
}
139+
129140
public function testExists()
130141
{
131142
$array = [

tests/Unit/CollectionTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,25 @@ public function testFilter()
164164
$this->assertEquals($partialArray, $filtered->all());
165165
}
166166

167+
public function testUnique()
168+
{
169+
$completeArray = [
170+
'one' => 'value a',
171+
'two' => 'value b',
172+
'three' => 'value a',
173+
'four' => 'value b'
174+
];
175+
$uniqueArray = [
176+
'one' => 'value a',
177+
'two' => 'value b',
178+
];
179+
$collection = new Collection($completeArray);
180+
$filtered = $collection->unique();
181+
182+
$this->assertEquals($completeArray, $collection->all());
183+
$this->assertEquals($uniqueArray, $filtered->all());
184+
}
185+
167186
public function testFind()
168187
{
169188
$items = [

0 commit comments

Comments
 (0)