Skip to content

Commit

Permalink
[9.x] Add String::squish() helper (#41791)
Browse files Browse the repository at this point in the history
* Add squish helper

* Additional test case

* Add squish to Stringable

* Fix code formatting

* Make more consistent

* formatting

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
dwightwatson and taylorotwell authored Apr 3, 2022
1 parent 7c9430a commit 88663c1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,17 @@ public static function snake($value, $delimiter = '_')
return static::$snakeCache[$key][$delimiter] = $value;
}

/**
* Remove all "extra" blank space from the given string.
*
* @param string $value
* @return string
*/
public static function squish($value)
{
return preg_replace('/\s+/', ' ', trim($value));
}

/**
* Determine if a given string starts with a given substring.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,16 @@ public function scan($format)
return collect(sscanf($this->value, $format));
}

/**
* Remove all "extra" blank space from the given string.
*
* @return static
*/
public function squish()
{
return new static(Str::squish($this->value));
}

/**
* Begin a string with a single instance of a given value.
*
Expand Down
11 changes: 11 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,17 @@ public function testSnake()
$this->assertSame('żółtałódka', Str::snake('ŻółtaŁódka'));
}

public function testSquish()
{
$this->assertSame('laravel php framework', Str::squish(' laravel php framework '));
$this->assertSame('laravel php framework', Str::squish("laravel\t\tphp\n\nframework"));
$this->assertSame('laravel php framework', Str::squish('
laravel
php
framework
'));
}

public function testStudly()
{
$this->assertSame('LaravelPHPFramework', Str::studly('laravel_p_h_p_framework'));
Expand Down
11 changes: 11 additions & 0 deletions tests/Support/SupportStringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,17 @@ public function testSlug()
$this->assertSame('', (string) $this->stringable('')->slug());
}

public function testSquish()
{
$this->assertSame('words with spaces', (string) $this->stringable(' words with spaces ')->squish());
$this->assertSame('words with spaces', (string) $this->stringable("words\t\twith\n\nspaces")->squish());
$this->assertSame('words with spaces', (string) $this->stringable('
words
with
spaces
')->squish());
}

public function testStart()
{
$this->assertSame('/test/string', (string) $this->stringable('test/string')->start('/'));
Expand Down

0 comments on commit 88663c1

Please sign in to comment.