From f719c203619c9edb39b9b9ac08bccc63e3823219 Mon Sep 17 00:00:00 2001 From: Pedro Martins Date: Sun, 10 Nov 2024 15:29:03 +0000 Subject: [PATCH] feat(support): add `start` to `StringHelper` (#713) Co-authored-by: Enzo Innocenzi --- src/Tempest/Support/src/StringHelper.php | 10 ++++++++++ src/Tempest/Support/tests/StringHelperTest.php | 6 ++++++ 2 files changed, 16 insertions(+) diff --git a/src/Tempest/Support/src/StringHelper.php b/src/Tempest/Support/src/StringHelper.php index a3fc0af91..eea1b3ab7 100644 --- a/src/Tempest/Support/src/StringHelper.php +++ b/src/Tempest/Support/src/StringHelper.php @@ -182,6 +182,16 @@ public function finish(string $cap): self ); } + /** + * Prefixes the instance with the given string. + */ + public function start(string $prefix): self + { + return new self( + $prefix.preg_replace('/^(?:'.preg_quote($prefix, '/').')+/u', replacement: '', subject: $this->string) + ); + } + /** * Returns the remainder of the string after the first occurrence of the given value. */ diff --git a/src/Tempest/Support/tests/StringHelperTest.php b/src/Tempest/Support/tests/StringHelperTest.php index 4eba1a3bd..ed95ad89f 100644 --- a/src/Tempest/Support/tests/StringHelperTest.php +++ b/src/Tempest/Support/tests/StringHelperTest.php @@ -475,4 +475,10 @@ public function test_unwrap(): void $this->assertSame('value', str('value]')->unwrap('[', ']', strict: false)->toString()); $this->assertSame('Scott', str('Scott Kennedy')->unwrap(before: 'Leon ', after: ' Kennedy', strict: false)->toString()); } + + public function test_start(): void + { + $this->assertSame('Leon Scott Kennedy', str('Scott Kennedy')->start('Leon ')->toString()); + $this->assertSame('Leon Scott Kennedy', str('Leon Scott Kennedy')->start('Leon ')->toString()); + } }