[node-core-library] Fix Text.truncateWithEllipsis() exceeding maximumLength - #6106
Open
Alexandre Kohler (kwy404) wants to merge 1 commit into
Open
Alexandre Kohler (kwy404) wants to merge 1 commit into
Alexandre Kohler (kwy404) wants to merge 1 commit into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Text.truncateWithEllipsis()checkss.length <= 3where it should checkmaximumLength < 3before deciding to skip the ellipsis. So for any input longer than 3 characters withmaximumLengthof 0, 1 or 2, it returns'...', which is longer thanmaximumLength. For exampleText.truncateWithEllipsis('12345', 2)andText.truncateWithEllipsis('12345', 0)both return'...'.This PR changes the check to
maximumLength < 3.Details
With the old check, those inputs fall through to
s.substring(0, maximumLength - 3) + '...'. The negative end index is treated as 0, so the result is just'...'.Results do not change for any other input: an input of 3 characters or fewer only reaches this check when
maximumLengthis already below 3, andmaximumLengthof 3 or more still gets the ellipsis as before.rushxcan hit this when it truncates script bodies to the console width on a narrow terminal.How it was tested
Added a unit test in
src/test/Text.test.tsthat truncates'12345'to lengths 0 and 2. Without the fix it fails (Expected: "",Received: "..."). With the fix all 16Texttests pass. Also ran the node-core-library build with lint and API Extractor,rush prettierandrush change --verify.