Skip to content

Commit

Permalink
Add dedup to strings (#52350)
Browse files Browse the repository at this point in the history
* Add `dedup` to strings

* formatting

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
jasonmccreary and taylorotwell authored Aug 1, 2024
1 parent 0ca8ad0 commit 4672516
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,18 @@ public static function convertCase(string $string, int $mode = MB_CASE_FOLD, ?st
return mb_convert_case($string, $mode, $encoding);
}

/**
* Replace consecutive instances of a given character with a single character in the given string.
*
* @param string $string
* @param string $character
* @return string
*/
public static function deduplicate(string $string, string $character = ' ')
{
return preg_replace('/'.preg_quote($character, '/').'+/u', $character, $string);
}

/**
* Determine if a given string ends with a given substring.
*
Expand Down
11 changes: 11 additions & 0 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,17 @@ public function convertCase(int $mode = MB_CASE_FOLD, ?string $encoding = 'UTF-8
return new static(Str::convertCase($this->value, $mode, $encoding));
}

/**
* Replace consecutive instances of a given character with a single character.
*
* @param string $character
* @return static
*/
public function deduplicate(string $character = ' ')
{
return new static(Str::deduplicate($this->value, $character));
}

/**
* Get the parent directory's path.
*
Expand Down
8 changes: 8 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,14 @@ public function testConvertCase()
Str::convertCase('Hello', -1);
}

public function testDedup()
{
$this->assertSame(' laravel php framework ', Str::deduplicate(' laravel php framework '));
$this->assertSame('what', Str::deduplicate('whaaat', 'a'));
$this->assertSame('/some/odd/path/', Str::deduplicate('/some//odd//path/', '/'));
$this->assertSame('ムだム', Str::deduplicate('ムだだム', ''));
}

public function testParseCallback()
{
$this->assertEquals(['Class', 'method'], Str::parseCallback('Class@method'));
Expand Down
8 changes: 8 additions & 0 deletions tests/Support/SupportStringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ public function testWhenContainsAll()
}));
}

public function testDedup()
{
$this->assertSame(' laravel php framework ', (string) $this->stringable(' laravel php framework ')->deduplicate());
$this->assertSame('what', (string) $this->stringable('whaaat')->deduplicate('a'));
$this->assertSame('/some/odd/path/', (string) $this->stringable('/some//odd//path/')->deduplicate('/'));
$this->assertSame('ムだム', (string) $this->stringable('ムだだム')->deduplicate(''));
}

public function testDirname()
{
$this->assertSame('/framework/tests', (string) $this->stringable('/framework/tests/Support')->dirname());
Expand Down

0 comments on commit 4672516

Please sign in to comment.