Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Add unshift method to collection #36830

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,18 @@ public function prepend($value, $key = null)
return $this;
}

/**
* Push an item onto the beginning of the collection.
*
* @param mixed $value
* @param mixed $key
* @return $this
*/
public function unshift($value, $key = null)
{
return $this->prepend(...func_get_args());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why func_get_args

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i am trying to make the prepend method to do the work instead of replicating the prepend code into unshift

and because prepend is depending on func_get_args, i had to pass it from unshift

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why can't you write ->prepend($value, $key)

Copy link
Author

@egXcoder egXcoder Mar 31, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i tried this approach before,

public function unshift($value, $key = null)
{
return $this->prepend($value,$key);
}

but calling unshift("x") .. is going to call prepend("x",null)
and this is different from prepend("x") and its going to act differently

but with the func_get_args approach,
calling unshift("x") is going to call prepend("x")
which is forcing unshift and prepend to act the same

}

/**
* Push one or more items onto the end of the collection.
*
Expand Down
21 changes: 21 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3261,6 +3261,27 @@ public function testPrepend()
);
}

public function testUnshift()
{
$c = new Collection(['one', 'two', 'three', 'four']);
$this->assertEquals(
['zero', 'one', 'two', 'three', 'four'],
$c->unshift('zero')->all()
);

$c = new Collection(['one' => 1, 'two' => 2]);
$this->assertEquals(
['zero' => 0, 'one' => 1, 'two' => 2],
$c->unshift(0, 'zero')->all()
);

$c = new Collection(['one' => 1, 'two' => 2]);
$this->assertEquals(
[null => 0, 'one' => 1, 'two' => 2],
$c->unshift(0, null)->all()
);
}

public function testPushWithOneItem()
{
$expected = [
Expand Down