Skip to content

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

Merged
taylorotwell merged 6 commits into
laravel:9.xfrom
dwightwatson:squish
Apr 3, 2022
Merged

[9.x] Add String::squish() helper#41791
taylorotwell merged 6 commits into
laravel:9.xfrom
dwightwatson:squish

Conversation

@dwightwatson

Copy link
Copy Markdown
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

dwightwatson commented Apr 2, 2022

Copy link
Copy Markdown
Contributor Author

Looks like that failing build may not be my fault 🤞

@derekmd

derekmd commented Apr 2, 2022

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
Contributor Author

Of course, added to Stringable as well.

@rcerljenko

Copy link
Copy Markdown

@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