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
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add dedup to strings
  • Loading branch information
jasonmccreary committed Aug 1, 2024

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
commit cb5b7979d33b063465da12fa0a3f42ff632f256e
12 changes: 12 additions & 0 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
@@ -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 characters with character in the given string.
*
* @param string $string
* @param string $character
* @return string
*/
public static function dedup(string $string, string $character = ' ')
{
return preg_replace('/'.preg_quote($character, '/').'+/u', $character, $string);
}

/**
* Determine if a given string ends with a given substring.
*
11 changes: 11 additions & 0 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
@@ -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 characters with character in a string.
*
* @param string $character
* @return static
*/
public function dedup(string $character = ' ')
{
return new static(Str::dedup($this->value, $character));
}

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

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

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

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

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