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

[9.x] Add String::squish() helper #41791

Merged
merged 6 commits into from
Apr 3, 2022
Merged

Conversation

dwightwatson
Copy link
Contributor

This proposes a new string helper method called squish, which is named similarly to the one in Rails. I know there's a lot of fair scrutiny given to adding new helpers but felt it was worth putting it out there in case you saw the usefulness that I do.

My use-cases are both when outputting user input that had additional unnecessary whitespace, and also when creating interpolated strings that have optional values (i.e. "A {$description} listing has been created."). In both of these instances using squish would make the content appear more professional.

If you do like this addition I can follow-up and add it to Stringable as well.

@dwightwatson
Copy link
Contributor Author

dwightwatson commented Apr 2, 2022

Looks like that failing build may not be my fault 🤞

@derekmd
Copy link
Contributor

derekmd commented Apr 2, 2022

You can try force-pushing your branch to re-trigger GitHub Actions and get all-green.

A recent merged PR can cause CacheFileStoreTest@testIncrementDoesNotExtendCacheLife() to intermittently fail if the second clock ticks over mid-case: #40790 (comment)

@taylorotwell
Copy link
Member

Can you also add it to fluent strings?

@taylorotwell taylorotwell marked this pull request as draft April 2, 2022 15:25
@dwightwatson dwightwatson marked this pull request as ready for review April 2, 2022 22:42
@dwightwatson
Copy link
Contributor Author

Of course, added to Stringable as well.

@rcerljenko
Copy link

@dwightwatson could the replacement param be passed as second argument like this:

public static function squish($value, $replacement = ' ')
{
    return preg_replace('/\s+/', $replacement, trim($value));
}

@taylorotwell taylorotwell merged commit 88663c1 into laravel:9.x Apr 3, 2022
@dwightwatson dwightwatson deleted the squish branch April 3, 2022 22:55
chu121su12 pushed a commit to chu121su12/framework that referenced this pull request Apr 5, 2022
* Add squish helper

* Additional test case

* Add squish to Stringable

* Fix code formatting

* Make more consistent

* formatting

Co-authored-by: Taylor Otwell <taylor@laravel.com>
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.

4 participants