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

Add before and after macros #213

Merged
merged 14 commits into from
Oct 15, 2021

Conversation

shugyosha89
Copy link
Contributor

These are some macros to easily insert items into a Collection before or after an existing item.

You can can insert after a given value in the Collection, or after a given key:

collect(['zero', 'two', 'three'])->insertAfter('zero', 'one');
// Collection contains ['zero', 'one', 'two', 'three']

collect(['zero', 'two', 'three'])->insertAfterKey(0, 'one');
// Collection contains ['zero', 'one', 'two', 'three']

Optionally a key for the new item can also be given:

collect(['zero' => 0, 'two' => 2, 'three' => 3]->insertAfter(0, 5, 'five');
// Collection contains ['zero' => 0, 'five' => 5, 'two' => 2, 'three' => 3]

collect(['zero' => 0, 'two' => 2, 'three' => 3]->insertAfterKey('zero', 5, 'five');
// Collection contains ['zero' => 0, 'five' => 5, 'two' => 2, 'three' => 3]

insertBefore and insertBeforeKey work the same way, but they insert before the item:

collect(['zero', 'two', 'three'])->insertBefore('two', 'one');
// Collection contains ['zero', 'one', 'two', 'three']

Please take a look, thanks!

@freekmurze freekmurze merged commit 1af12f5 into spatie:main Oct 15, 2021
@freekmurze
Copy link
Member

Thanks!

@shugyosha89 shugyosha89 deleted the add_before_and_after_macros branch October 16, 2021 10:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants