Skip to content

Commit

Permalink
feature #35649 [String] Allow to keep the last word when truncating a…
Browse files Browse the repository at this point in the history
… text (franmomu)

This PR was merged into the 5.1-dev branch.

Discussion
----------

[String] Allow to keep the last word when truncating a text

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #35567
| License       | MIT
| Doc PR        | -

The [truncate filter from twig/extensions](https://github.com/twigphp/Twig-extensions/blob/master/src/TextExtension.php#L36) has a `preserve` parameter to preserve whole words.

Since `twig/extensions` is deprecated and its alternative for `truncate` filter is the use of `u` filter, this PR adds preverse word functionality.

Commits
-------

1cfaeec [String] Allow to keep the last word when truncating a text
  • Loading branch information
fabpot committed Feb 21, 2020
2 parents cde44fc + 1cfaeec commit d33a483
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/Symfony/Component/String/AbstractString.php
Expand Up @@ -620,7 +620,7 @@ abstract public function trimStart(string $chars = " \t\n\r\0\x0B\x0C\u{A0}\u{FE
/**
* @return static
*/
public function truncate(int $length, string $ellipsis = ''): self
public function truncate(int $length, string $ellipsis = '', bool $cut = true): self
{
$stringLength = $this->length();

Expand All @@ -634,6 +634,10 @@ public function truncate(int $length, string $ellipsis = ''): self
$ellipsisLength = 0;
}

if (!$cut) {
$length = $ellipsisLength + ($this->indexOf([' ', "\r", "\n", "\t"], ($length ?: 1) - 1) ?? $stringLength);
}

$str = $this->slice(0, $length - $ellipsisLength);

return $ellipsisLength ? $str->trimEnd()->append($ellipsis) : $str;
Expand Down
7 changes: 4 additions & 3 deletions src/Symfony/Component/String/CHANGELOG.md
Expand Up @@ -7,9 +7,10 @@ CHANGELOG
* added the `AbstractString::reverse()` method
* made `AbstractString::width()` follow POSIX.1-2001
* added `LazyString` which provides memoizing stringable objects
* The component is not marked as `@experimental` anymore.
* Added the `s()` helper method to get either an `UnicodeString` or `ByteString` instance,
depending of the input string UTF-8 compliancy.
* The component is not marked as `@experimental` anymore
* added the `s()` helper method to get either an `UnicodeString` or `ByteString` instance,
depending of the input string UTF-8 compliancy
* added `$cut` parameter to `Symfony\Component\String\AbstractString::truncate()`

5.0.0
-----
Expand Down
9 changes: 7 additions & 2 deletions src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php
Expand Up @@ -1405,9 +1405,9 @@ public static function providePadStart()
/**
* @dataProvider provideTruncate
*/
public function testTruncate(string $expected, string $origin, int $length, string $ellipsis)
public function testTruncate(string $expected, string $origin, int $length, string $ellipsis, bool $cut = true)
{
$instance = static::createFromString($origin)->truncate($length, $ellipsis);
$instance = static::createFromString($origin)->truncate($length, $ellipsis, $cut);

$this->assertEquals(static::createFromString($expected), $instance);
}
Expand All @@ -1417,12 +1417,17 @@ public static function provideTruncate()
return [
['', '', 3, ''],
['', 'foo', 0, '...'],
['foo', 'foo', 0, '...', false],
['fo', 'foobar', 2, ''],
['foobar', 'foobar', 10, ''],
['foobar', 'foobar', 10, '...', false],
['foo', 'foo', 3, '...'],
['fo', 'foobar', 2, '...'],
['...', 'foobar', 3, '...'],
['fo...', 'foobar', 5, '...'],
['foobar...', 'foobar foo', 6, '...', false],
['foobar...', 'foobar foo', 7, '...', false],
['foobar foo...', 'foobar foo a', 10, '...', false],
];
}

Expand Down

0 comments on commit d33a483

Please sign in to comment.