Skip to content

Commit

Permalink
refactor: rename limit to truncate
Browse files Browse the repository at this point in the history
  • Loading branch information
innocenzi committed Nov 13, 2024
1 parent 04b027c commit aa5db6f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/Tempest/Support/src/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -657,20 +657,20 @@ public function excerpt(int $from, int $to, bool $asArray = false): self|ArrayHe
}

/**
* Limits the number of characters of the instance.
* Truncates the instance to the specified amount of characters.
*
* ### Example
* ```php
* str('Lorem ipsum')->limit(5, end: '...'); // Lorem...
* str('Lorem ipsum')->truncate(5, end: '...'); // Lorem...
* ```
*/
public function limit(int $characters, string $end = ''): self
public function truncate(int $characters, string $end = ''): self
{
if (mb_strwidth($this->string, 'UTF-8') <= $characters) {
return $this;
}

return new self(rtrim(mb_strimwidth($this->string, 0, $characters, '', 'UTF-8')) . $end);
return new self(rtrim(mb_strimwidth($this->string, 0, $characters, encoding: 'UTF-8')) . $end);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/Tempest/Support/tests/StringHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,12 +484,12 @@ public function test_start(): void

public function test_limit(): void
{
$this->assertSame('Lorem', str('Lorem ipsum')->limit(5)->toString());
$this->assertSame('Lorem...', str('Lorem ipsum')->limit(5, end: '...')->toString());
$this->assertSame('...', str('Lorem ipsum')->limit(0, end: '...')->toString());
$this->assertSame('L...', str('Lorem ipsum')->limit(1, end: '...')->toString());
$this->assertSame('Lorem ipsum', str('Lorem ipsum')->limit(100)->toString());
$this->assertSame('Lorem ipsum', str('Lorem ipsum')->limit(100, end: '...')->toString());
$this->assertSame('Lorem', str('Lorem ipsum')->truncate(5)->toString());
$this->assertSame('Lorem...', str('Lorem ipsum')->truncate(5, end: '...')->toString());
$this->assertSame('...', str('Lorem ipsum')->truncate(0, end: '...')->toString());
$this->assertSame('L...', str('Lorem ipsum')->truncate(1, end: '...')->toString());
$this->assertSame('Lorem ipsum', str('Lorem ipsum')->truncate(100)->toString());
$this->assertSame('Lorem ipsum', str('Lorem ipsum')->truncate(100, end: '...')->toString());
}

public function test_substr(): void
Expand Down

0 comments on commit aa5db6f

Please # to comment.