Skip to content

Commit 72cd85c

Browse files
committed
feature: add OrderedListInterface#removeAt
Signed-off-by: Maximilian Bösing <[email protected]>
1 parent 451e911 commit 72cd85c

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

src/OrderedList.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,4 +417,9 @@ public function prepend($value): OrderedListInterface
417417

418418
return $instance;
419419
}
420+
421+
public function removeAt(int $index): OrderedListInterface
422+
{
423+
return $this->filter(static fn ($_, int $i) => $i !== $index);
424+
}
420425
}

src/OrderedListInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,12 @@ public function findFirstMatchingIndex(callable $filter): ?int;
256256
* @return OrderedListInterface<TValue>
257257
*/
258258
public function prepend($value): self;
259+
260+
/**
261+
* Removes an element at the given index.
262+
*
263+
* @param 0|positive-int $index
264+
* @return OrderedListInterface<TValue>
265+
*/
266+
public function removeAt(int $index): self;
259267
}

tests/GenericOrderedListTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,4 +1478,16 @@ public function testReduceWillReturnInitialValueOnEmptyList(): void
14781478
$list = new GenericOrderedList([]);
14791479
self::assertSame(PHP_INT_MAX, $list->reduce(static fn () => 1, PHP_INT_MAX));
14801480
}
1481+
1482+
public function testWillRemoveItemAtSpecificPosition(): void
1483+
{
1484+
$list = new GenericOrderedList([
1485+
1,
1486+
2,
1487+
3,
1488+
]);
1489+
1490+
$list = $list->removeAt(1);
1491+
self::assertSame([1, 3], $list->toNativeArray());
1492+
}
14811493
}

0 commit comments

Comments
 (0)