Skip to content

Commit

Permalink
Added Str::deduplicate (#700)
Browse files Browse the repository at this point in the history
Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
  • Loading branch information
huangdijia and huangdijia authored Aug 12, 2024
1 parent 3a9bb6d commit 3e30824
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
9 changes: 9 additions & 0 deletions output/Hyperf/Stringable/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ public static function createUuidsNormally()
{
}

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

/**
* Converts GitHub flavored Markdown into HTML.
*
Expand Down
9 changes: 9 additions & 0 deletions output/Hyperf/Stringable/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@

class Stringable
{
/**
* Replace consecutive instances of a given character with a single character.
*
* @return static
*/
public function deduplicate(string $character = ' ')
{
}

/**
* Convert GitHub flavored Markdown into HTML.
*
Expand Down
5 changes: 5 additions & 0 deletions src/StrMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public function createUuidsUsing()
return fn (?callable $factory = null) => UuidContainer::$uuidFactory = $factory;
}

public static function deduplicate()
{
return fn (string $string, string $character = ' ') => preg_replace('/' . preg_quote($character, '/') . '+/u', $character, $string);
}

public function inlineMarkdown()
{
return function ($string, array $options = []) {
Expand Down
6 changes: 6 additions & 0 deletions src/StringableMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
*/
class StringableMixin
{
public function deduplicate()
{
/* @phpstan-ignore-next-line */
return fn (string $character = ' ') => new static(Str::deduplicate($this->value, $character));
}

public function inlineMarkdown()
{
/* @phpstan-ignore-next-line */
Expand Down

0 comments on commit 3e30824

Please sign in to comment.