From 3e30824d778e2de3ca16459fe67fae38d7f7707b Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Mon, 12 Aug 2024 12:59:42 +0800 Subject: [PATCH] Added `Str::deduplicate` (#700) Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com> --- output/Hyperf/Stringable/Str.php | 9 +++++++++ output/Hyperf/Stringable/Stringable.php | 9 +++++++++ src/StrMixin.php | 5 +++++ src/StringableMixin.php | 6 ++++++ 4 files changed, 29 insertions(+) diff --git a/output/Hyperf/Stringable/Str.php b/output/Hyperf/Stringable/Str.php index 90fd45f..05476b3 100644 --- a/output/Hyperf/Stringable/Str.php +++ b/output/Hyperf/Stringable/Str.php @@ -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. * diff --git a/output/Hyperf/Stringable/Stringable.php b/output/Hyperf/Stringable/Stringable.php index 102ab6e..d45bb29 100644 --- a/output/Hyperf/Stringable/Stringable.php +++ b/output/Hyperf/Stringable/Stringable.php @@ -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. * diff --git a/src/StrMixin.php b/src/StrMixin.php index ced0d7c..b19da37 100644 --- a/src/StrMixin.php +++ b/src/StrMixin.php @@ -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 = []) { diff --git a/src/StringableMixin.php b/src/StringableMixin.php index 8e78118..e698099 100644 --- a/src/StringableMixin.php +++ b/src/StringableMixin.php @@ -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 */