Skip to content

Commit

Permalink
feat(support): add start to StringHelper (#713)
Browse files Browse the repository at this point in the history
Co-authored-by: Enzo Innocenzi <enzo@innocenzi.dev>
  • Loading branch information
pxpm and innocenzi authored Nov 10, 2024
1 parent f3054f9 commit f719c20
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Tempest/Support/src/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
6 changes: 6 additions & 0 deletions src/Tempest/Support/tests/StringHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit f719c20

Please # to comment.