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 deduplicate to strings #52350

Merged
merged 2 commits into from
Aug 1, 2024
Merged

Conversation

jasonmccreary
Copy link
Contributor

@jasonmccreary jasonmccreary commented Aug 1, 2024

This adds a convenience method to replace consecutive occurrences of a character with character. Or, more commonly, deduplicate characters within a string.

Str::dedup('random  double  spaces'); // 'random double spaces'
Str::dedup('/some//odd/path//', '/'); // '/some/odd/path/'
Str::dedup('zondaaaa', 'a');          // 'zonda'

Note: While a second parameter might have been added to squish, their primary intention felt different. squish also exists in other languages. So creating a separate, new method seemed more appropriate than expanding the squish behavior within Laravel.

@taylorotwell taylorotwell merged commit 4672516 into laravel:11.x Aug 1, 2024
29 checks passed
@jasonmccreary jasonmccreary deleted the str-dedup branch August 1, 2024 16:08
@jasonmccreary jasonmccreary changed the title Add dedup to strings Add deduplicate to strings Aug 1, 2024
@timopaul
Copy link

@jasonmccreary, I think there is a lot more power in the method if the second parameter could also be an array of characters or a string from which all characters are handled separately. What do you think?

@jasonmccreary
Copy link
Contributor Author

Yeah, that's interesting, kind of like trim. I'd almost opt for an array though to avoid any confusion that you could pass in a string to dedup. Then again, you can now... So that'd be a change of behavior.

I think best to leave it alone for now.

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.

3 participants