diff --git a/library/Toplevelview/Util/Str.php b/library/Toplevelview/Util/Str.php index cabadaa..0f37186 100644 --- a/library/Toplevelview/Util/Str.php +++ b/library/Toplevelview/Util/Str.php @@ -15,6 +15,10 @@ class Str */ public static function limit($str, $len = 25, $end = '...'): string { + if (empty($str)) { + return ''; + } + // If the string is smaller or equal to the limit we simply return it if (mb_strwidth($str, 'UTF-8') <= $len) { return $str; diff --git a/test/php/library/Toplevelview/Util/StrTest.php b/test/php/library/Toplevelview/Util/StrTest.php index f53beec..ff3bf08 100644 --- a/test/php/library/Toplevelview/Util/StrTest.php +++ b/test/php/library/Toplevelview/Util/StrTest.php @@ -9,6 +9,7 @@ final class StrTest extends TestCase { public function testLimitWithSmallerString() { + $this->assertSame('', Str::limit(null)); $this->assertSame('', Str::limit('')); $this->assertSame('noop', Str::limit('noop')); } @@ -19,6 +20,10 @@ public function testLimitWithLongerStringAndSpecificLimit() 'Lorem ipsu...', Str::limit('Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 10) ); + $this->assertSame( + '๐Ÿ”๐Ÿ”๐Ÿ”๐Ÿ”๐Ÿ”...', + Str::limit('๐Ÿ”๐Ÿ”๐Ÿ”๐Ÿ”๐Ÿ”๐Ÿ”๐Ÿ”๐Ÿ”', 10) + ); } public function testLimitWithLongerStringAndSpecificEnd() @@ -27,5 +32,13 @@ public function testLimitWithLongerStringAndSpecificEnd() 'L (...)', Str::limit('Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 1, ' (...)') ); + $this->assertSame( + 'ะ› (...)', + Str::limit('ะ›ัŽะฑะพะน, ะบั‚ะพ ัั‚ะพ ั‡ะธั‚ะฐะตั‚, ะดัƒั€ะฐะบ', 1, ' (...)') + ); + $this->assertSame( + 'ะ› (๐Ÿฆ”๐Ÿฆ”๐Ÿฆ”)', + Str::limit('ะ›ัŽะฑะพะน, ะบั‚ะพ ัั‚ะพ ั‡ะธั‚ะฐะตั‚, ะดัƒั€ะฐะบ', 1, ' (๐Ÿฆ”๐Ÿฆ”๐Ÿฆ”)') + ); } }