Skip to content

Commit f719c20

Browse files
pxpminnocenzi
andauthored
feat(support): add start to StringHelper (#713)
Co-authored-by: Enzo Innocenzi <[email protected]>
1 parent f3054f9 commit f719c20

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/Tempest/Support/src/StringHelper.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,16 @@ public function finish(string $cap): self
182182
);
183183
}
184184

185+
/**
186+
* Prefixes the instance with the given string.
187+
*/
188+
public function start(string $prefix): self
189+
{
190+
return new self(
191+
$prefix.preg_replace('/^(?:'.preg_quote($prefix, '/').')+/u', replacement: '', subject: $this->string)
192+
);
193+
}
194+
185195
/**
186196
* Returns the remainder of the string after the first occurrence of the given value.
187197
*/

src/Tempest/Support/tests/StringHelperTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,4 +475,10 @@ public function test_unwrap(): void
475475
$this->assertSame('value', str('value]')->unwrap('[', ']', strict: false)->toString());
476476
$this->assertSame('Scott', str('Scott Kennedy')->unwrap(before: 'Leon ', after: ' Kennedy', strict: false)->toString());
477477
}
478+
479+
public function test_start(): void
480+
{
481+
$this->assertSame('Leon Scott Kennedy', str('Scott Kennedy')->start('Leon ')->toString());
482+
$this->assertSame('Leon Scott Kennedy', str('Leon Scott Kennedy')->start('Leon ')->toString());
483+
}
478484
}

0 commit comments

Comments
 (0)