Skip to content

Commit

Permalink
Merge pull request #33 from smeeckaert/bugfix/truncate-length-append
Browse files Browse the repository at this point in the history
Truncate check length of append
  • Loading branch information
Norbert Orzechowicz committed Oct 29, 2015
2 parents de617f8 + 2e657b5 commit 4f58615
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
12 changes: 12 additions & 0 deletions spec/Coduo/PHPHumanizer/StringSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,17 @@ function it_truncate_string_to_word_closest_to_a_certain_number_of_characters()
$this->truncate($text, 0)->shouldReturn("Lorem");
$this->truncate($text, 0, '...')->shouldReturn("Lorem...");
$this->truncate($text, -2)->shouldReturn($text);

$textShort = 'Short text';
$this->truncate($textShort, 1, '...')->shouldReturn("Short...");
$this->truncate($textShort, 2, '...')->shouldReturn("Short...");
$this->truncate($textShort, 3, '...')->shouldReturn("Short...");
$this->truncate($textShort, 4, '...')->shouldReturn("Short...");
$this->truncate($textShort, 5, '...')->shouldReturn("Short...");
$this->truncate($textShort, 6, '...')->shouldReturn("Short...");
$this->truncate($textShort, 7, '...')->shouldReturn("Short text");
$this->truncate($textShort, 8, '...')->shouldReturn("Short text");
$this->truncate($textShort, 9, '...')->shouldReturn("Short text");
$this->truncate($textShort, 10, '...')->shouldReturn("Short text");
}
}
2 changes: 1 addition & 1 deletion src/Coduo/PHPHumanizer/String/Truncate.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct($text, $charactersCount, $append = '')

public function __toString()
{
if ($this->charactersCount < 0 || strlen($this->text) <= $this->charactersCount) {
if ($this->charactersCount < 0 || strlen($this->text) <= ($this->charactersCount + mb_strlen($this->append))) {
return $this->text;
}

Expand Down

0 comments on commit 4f58615

Please sign in to comment.