Skip to content

[String] Add TruncateMode mode to truncate methods #57243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 5, 2024

Conversation

Korbeil
Copy link
Contributor

@Korbeil Korbeil commented May 30, 2024

Q A
Branch? 7.2
Bug fix? no
New feature? yes
Deprecations? no
Issues
License MIT

When using truncate we had two behaviors:

u("Lorem ipsum dolor sit amet")->truncate(14, cut: true); // outputs: Lorem ipsum do
u("Lorem ipsum dolor sit amet")->truncate(14, cut: false); // outputs: Lorem ipsum dolor

But sometimes, I want the truncate to fit within 14 chars while keeping all complete words, so I changed truncate method to have a new mode "WordBefore" and switch the cut parameter to use a new enum so we can choose which mode is better.

Now:

u("Lorem ipsum dolor sit amet")->truncate(14, cut: TruncateMode::Char); // outputs: Lorem ipsum do 
u("Lorem ipsum dolor sit amet")->truncate(14, cut: TruncateMode::WordAfter); // outputs: Lorem ipsum dolor
u("Lorem ipsum dolor sit amet")->truncate(14, cut: TruncateMode::WordBefore); // outputs: Lorem ipsum

@carsonbot carsonbot added this to the 7.2 milestone May 30, 2024
@Korbeil Korbeil force-pushed the feature/string-truncate branch 3 times, most recently from 0cbd9ea to da24ab8 Compare May 30, 2024 12:17
@Korbeil Korbeil force-pushed the feature/string-truncate branch 2 times, most recently from 2efd0c4 to 003ffd8 Compare May 30, 2024 12:34
@smnandre
Copy link
Member

This deprecation will deteriorate the DX when using String instances in twig 😐

Why not adding a second parameter that would dictate if the cut must happen before or after the word beeing cut in ?

Copy link
Member

@smnandre smnandre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per curent naming conventions ?

@Korbeil Korbeil force-pushed the feature/string-truncate branch 2 times, most recently from 31e0b07 to d1f2750 Compare June 23, 2024 07:40
@Korbeil
Copy link
Contributor Author

Korbeil commented Jun 23, 2024

This deprecation will deteriorate the DX when using String instances in twig 😐

Why not adding a second parameter that would dictate if the cut must happen before or after the word beeing cut in ?

I do not know how much it's being used within Twig, is it a very common function in there ?
Also for the proposal, it would be possible but consider:

public function truncate(int $length, string $ellipsis = '', bool $cut = true, bool $strict = true): static

true, true would mean CharStrict but true, false would mean Char, which is something that is kinda useless/not required here 😕 that's why I prefered a more "declarative" mode over 2 booleans.

@Korbeil Korbeil force-pushed the feature/string-truncate branch 3 times, most recently from 948e8e5 to a2ec874 Compare June 25, 2024 16:01
@smnandre
Copy link
Member

true, true would mean CharStrict but true, false would mean Char, which is something that is kinda useless/not required here 😕 that's why I prefered a more "declarative" mode over 2 booleans.

Yep absolutely right.

But with the bool not beeing deprecated my point is obselete anyway :) And if we want, we can add a |truncate filter with bool or named parameter in Twig, that use the new Enums 👍

@smnandre
Copy link
Member

Just some suggestion regarding the names and docblock..

To me it seems easier to understand the use cases / differences...

but feel free to ignore... as DX is always very personal :)

@Korbeil Korbeil force-pushed the feature/string-truncate branch from a2ec874 to e821bd5 Compare June 26, 2024 08:09
@Korbeil
Copy link
Contributor Author

Korbeil commented Jun 26, 2024

Updated with @smnandre recommendations ! Thanks ❤️

@Korbeil Korbeil changed the title [String] Add WORD_STRICT mode to truncate method [String] Add WordAfter mode to truncate method Jun 26, 2024
@OskarStark
Copy link
Contributor

OskarStark commented Jun 27, 2024

- TruncateCut
+ TruncateMode

?

Also the PR title needs some update

@Korbeil Korbeil force-pushed the feature/string-truncate branch from e821bd5 to 6ea2181 Compare June 28, 2024 07:32
@Korbeil Korbeil changed the title [String] Add WordAfter mode to truncate method [String] Add WordBefore mode to truncate method Jun 28, 2024
@Korbeil Korbeil force-pushed the feature/string-truncate branch from 6ea2181 to 76bf2c2 Compare June 28, 2024 07:54
@OskarStark OskarStark changed the title [String] Add WordBefore mode to truncate method [String] Add TruncateMode mode to truncate method Jun 28, 2024
@Korbeil Korbeil force-pushed the feature/string-truncate branch from 76bf2c2 to 1824b67 Compare July 1, 2024 07:33
@nicolas-grekas nicolas-grekas changed the title [String] Add TruncateMode mode to truncate method [String] Add TruncateMode mode to truncate methods Jul 1, 2024
@Korbeil Korbeil force-pushed the feature/string-truncate branch from 1824b67 to 446358d Compare July 1, 2024 08:06
@Korbeil Korbeil force-pushed the feature/string-truncate branch from 446358d to 2458aa5 Compare July 3, 2024 15:55
@Korbeil Korbeil force-pushed the feature/string-truncate branch from 2458aa5 to c3136a0 Compare July 3, 2024 15:56

* `truncate` method now also accept `TruncateMode` enum instead of a boolean:
* `TruncateMode::Char` is equivalent to `true` value ;
* `TruncateMode::WordAfter` is equivalent to `true` value ;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* `TruncateMode::WordAfter` is equivalent to `true` value ;
* `TruncateMode::WordAfter` is equivalent to `false` value ;

@fabpot
Copy link
Member

fabpot commented Jul 5, 2024

Thank you @Korbeil.

@fabpot fabpot merged commit 28ead3e into symfony:7.2 Jul 5, 2024
8 of 10 checks passed
@fabpot fabpot mentioned this pull request Oct 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants