Skip to content
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

fix(ui-truncate): truncate should not split words appart #690

Merged

Conversation

aversini
Copy link
Collaborator

@aversini aversini commented Sep 24, 2024

Summary by CodeRabbit

  • New Features

    • Updated the truncation logic to focus on an 'ideal length' for string truncation, simplifying how text is displayed.
  • Bug Fixes

    • Adjusted test cases to reflect the new expected output for truncated strings, ensuring accuracy in displayed text.
  • Documentation

    • Enhanced clarity in the Truncate component's functionality by refining parameter names and logic.

Copy link

coderabbitai bot commented Sep 24, 2024

Walkthrough

The changes in this pull request involve a modification of the Truncate component and its associated utilities. The parameter for the truncation length has been updated from length to idealLength, indicating a shift in how truncation is defined. Additionally, test cases have been adjusted to reflect this new parameter and its expected behavior. The overall logic of the truncation process remains intact, but the implementation and testing have been refined for clarity and accuracy.

Changes

Files Change Summary
packages/ui-truncate/src/components/Truncate/Truncate.tsx Updated the truncate function call parameter from length to idealLength.
packages/ui-truncate/src/components/Truncate/__tests__/Truncate.test.tsx Modified the expectation in a test case to reflect a new expected truncated output when the children prop exceeds the specified length.
packages/ui-truncate/src/components/Truncate/__tests__/utilities.test.ts Updated test cases for the truncate function to use idealLength instead of length, adjusting expectations based on the new parameter.
packages/ui-truncate/src/components/Truncate/utilities.ts Revised the truncate function to replace length and omission properties with idealLength. Simplified the truncation logic to focus solely on idealLength, removing unnecessary parameters and calculations related to omission.

Possibly related PRs

Suggested labels

Enhancement


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    -- I pushed a fix in commit <commit_id>, please review it.
    -- Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    -- @coderabbitai generate unit testing code for this file.
    -- @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    -- @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    -- @coderabbitai read src/utils.ts and generate unit testing code.
    -- @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    -- @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

Bundle Size (components)

Status File Size (Gzip) Limits
style.css 7.34 KB 8 KB
index.js 10.89 KB (+13 B +0.12%) 12 KB
vendor.js 66.06 KB 67 KB

Overall bundle size: 84.29 KB (+13 B +0.02%)
Overall status: ✅

Bundle Size (fingerprint)

Status File Size (Gzip) Limits
index.js 3.94 KB (-1 B -0.02%) 4 KB
vendor.js 44.34 KB 45 KB

Overall bundle size: 48.28 KB (-1 B 0.00%)
Overall status: ✅

Bundle Size (form components)

Status File Size (Gzip) Limits
index.js 5.44 KB (+3 B +0.05%) 20 KB
vendor.js 44.46 KB 67 KB

Overall bundle size: 49.9 KB (+3 B +0.01%)
Overall status: ✅

Bundle Size (system)

Status File Size (Gzip) Limits
style.css 3.96 KB 8 KB
index.js 1.93 KB 3 KB
vendor.js 44.46 KB 46 KB

Overall bundle size: 50.35 KB
Overall status: ✅

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Outside diff range and nitpick comments (3)
packages/ui-truncate/src/components/Truncate/utilities.ts (1)

12-36: LGTM: Improved truncation logic

The new implementation of the truncate function aligns well with its description and the PR objectives. It correctly handles different scenarios, such as when the idealLength ends on a space or in the middle of a word, ensuring that words are not split apart during truncation.

The removal of the omission logic simplifies the function and makes it more focused on its primary task. The overall logic is clear and concise.

Consider renaming the nextSpace variable to nextSpaceIndex for better clarity:

-	const nextSpace = string.slice(idealLength).search(" ");
+	const nextSpaceIndex = string.slice(idealLength).search(" ");
 	return {
-		string: string.slice(0, idealLength + nextSpace),
+		string: string.slice(0, idealLength + nextSpaceIndex),
 		isTruncated: true,
 	};
packages/ui-truncate/src/components/Truncate/__tests__/utilities.test.ts (2)

13-19: Consider differentiating this test case from the previous one.

This test case has the same expected output as the previous one, despite having a different idealLength. To make the test suite more robust and clear:

  1. Consider modifying the input string or expected output to better demonstrate the "truncate at the next space found right after the ideal length" behavior.
  2. Update the test case name to more accurately reflect the specific scenario being tested.

Here's a suggested modification:

it("should truncate at the next space after the ideal length", () => {
  const result = truncate({
    string: "Hello there, World!",
    idealLength: 7,
  });
  expect(result).toEqual({ string: "Hello there,", isTruncated: true });
});

This change would better illustrate the difference between truncating at the ideal length and truncating at the next space after the ideal length.


21-27: Approve implementation, but suggest renaming the test case.

The test case correctly demonstrates the truncation behavior with the new idealLength parameter. However, to improve clarity and differentiate it from the previous test case:

Consider renaming this test case to better reflect its specific scenario. For example:

it("should truncate a longer string at the next space after the ideal length", () => {
  // ... (rest of the test case remains the same)
});

This name more accurately describes what this particular test case is verifying.

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 70ba5f7 and 90bc63a.

Files selected for processing (4)
  • packages/ui-truncate/src/components/Truncate/Truncate.tsx (1 hunks)
  • packages/ui-truncate/src/components/Truncate/tests/Truncate.test.tsx (1 hunks)
  • packages/ui-truncate/src/components/Truncate/tests/utilities.test.ts (1 hunks)
  • packages/ui-truncate/src/components/Truncate/utilities.ts (1 hunks)
Additional comments not posted (7)
packages/ui-truncate/src/components/Truncate/Truncate.tsx (1)

20-20: Approve the parameter name change and suggest utility function verification.

The change from length to idealLength: length aligns well with the PR objective of not splitting words apart during truncation. This semantic shift suggests a more flexible approach to determining the truncation point, which should lead to improved readability of the truncated text.

To ensure full compatibility, please verify that the truncate utility function in ./utilities.ts has been updated to handle this idealLength parameter correctly. Run the following script to check the implementation:

If the script doesn't return any results, it might indicate that the truncate function hasn't been updated to match this new parameter name. In that case, please update the utility function accordingly.

packages/ui-truncate/src/components/Truncate/utilities.ts (2)

5-5: LGTM: Improved type definition

The change from length to idealLength in the TruncateOptions type better reflects the function's behavior. Removing the omission property simplifies the API, which is a good improvement. Making idealLength optional allows for using a default value, providing flexibility to the users of this function.


12-36: Great job addressing the PR objective

The changes in the truncate function directly address the PR objective of ensuring that words are not split apart during truncation. The new implementation looks for the next space after the ideal length if truncation is necessary, which guarantees that whole words are preserved. This is a significant improvement that enhances the usability and readability of the truncated text.

packages/ui-truncate/src/components/Truncate/__tests__/utilities.test.ts (3)

5-11: LGTM: Test case correctly uses the new idealLength parameter.

The test case has been updated to use idealLength instead of length, which aligns with the changes made to the truncate function. The expected result is appropriate for the given input and ideal length.


29-35: LGTM: Edge case correctly handled.

This test case effectively verifies the behavior when the idealLength is greater than the string length. It correctly expects the original string to be returned unchanged with isTruncated set to false.


1-36: Overall, the test cases have been successfully updated to reflect the new truncation behavior.

The changes in this file align well with the PR objectives. All test cases now use idealLength instead of length, and they cover various scenarios including truncation at ideal length, truncation at the next space, and handling of edge cases where the ideal length exceeds the string length.

To further improve the test suite:

  1. Consider implementing the suggested changes to differentiate the second and third test cases more clearly.
  2. Ensure that the test cases cover all possible edge cases and scenarios for the updated truncation logic.

To ensure comprehensive test coverage, let's verify if there are any other test files for the truncate function:

This will help us confirm if we've reviewed all relevant test files for the truncate function.

packages/ui-truncate/src/components/Truncate/__tests__/Truncate.test.tsx (1)

21-21: Approved change, but clarification needed on truncation behavior

The updated test expectation aligns with the PR objective of not splitting words apart during truncation. This is a positive change that should improve the user experience.

However, I have a few points that need clarification:

  1. The test still uses the length prop with a value of 20, but the AI summary mentioned a change from length to idealLength. Can you confirm if this prop name change has been implemented in the component?

  2. The expected truncated text is now longer than the specified length of 20. Could you explain how the new truncation logic works? Is it now aiming for the nearest word boundary after the specified length?

  3. Has the component's behavior changed, or was this test updated to match the existing behavior?

To help clarify these points, could you run the following script to check the implementation of the Truncate component?

@aversini aversini merged commit 67b1686 into main Sep 24, 2024
6 checks passed
@aversini aversini deleted the fix(ui-truncate)-truncate-should-not-split-words-appart branch September 24, 2024 22:13
@aversini aversini mentioned this pull request Sep 24, 2024
aversini added a commit that referenced this pull request Sep 24, 2024
🚀 Automated Release
---


<details><summary>ui-anchor: 1.1.4</summary>

##
[1.1.4](ui-anchor-v1.1.3...ui-anchor-v1.1.4)
(2024-09-24)


### Bug Fixes

* bump non-breaking dependencies to latest
([#692](#692))
([2300b7c](2300b7c))


### Dependencies

* The following workspace dependencies were updated
  * dependencies
    * @versini/ui-button bumped to 1.1.4
  * devDependencies
    * @versini/ui-styles bumped to 1.10.1
</details>

<details><summary>ui-bubble: 1.0.4</summary>

##
[1.0.4](ui-bubble-v1.0.3...ui-bubble-v1.0.4)
(2024-09-24)


### Bug Fixes

* bump non-breaking dependencies to latest
([#692](#692))
([2300b7c](2300b7c))


### Dependencies

* The following workspace dependencies were updated
  * dependencies
    * @versini/ui-button bumped to 1.1.4
    * @versini/ui-icons bumped to 1.12.5
    * @versini/ui-private bumped to 1.4.13
  * devDependencies
    * @versini/ui-styles bumped to 1.10.1
</details>

<details><summary>ui-button: 1.1.4</summary>

##
[1.1.4](ui-button-v1.1.3...ui-button-v1.1.4)
(2024-09-24)


### Bug Fixes

* bump non-breaking dependencies to latest
([#692](#692))
([2300b7c](2300b7c))


### Dependencies

* The following workspace dependencies were updated
  * dependencies
    * @versini/ui-private bumped to 1.4.13
  * devDependencies
    * @versini/ui-icons bumped to 1.12.5
    * @versini/ui-styles bumped to 1.10.1
</details>

<details><summary>ui-card: 1.0.4</summary>

##
[1.0.4](ui-card-v1.0.3...ui-card-v1.0.4)
(2024-09-24)


### Bug Fixes

* bump non-breaking dependencies to latest
([#692](#692))
([2300b7c](2300b7c))


### Dependencies

* The following workspace dependencies were updated
  * dependencies
    * @versini/ui-private bumped to 1.4.13
  * devDependencies
    * @versini/ui-styles bumped to 1.10.1
</details>

<details><summary>ui-components: 5.31.5</summary>

##
[5.31.5](ui-components-v5.31.4...ui-components-v5.31.5)
(2024-09-24)


### Dependencies

* The following workspace dependencies were updated
  * dependencies
    * @versini/ui-anchor bumped to 1.1.4
    * @versini/ui-button bumped to 1.1.4
    * @versini/ui-bubble bumped to 1.0.4
    * @versini/ui-card bumped to 1.0.4
    * @versini/ui-footer bumped to 1.0.4
    * @versini/ui-header bumped to 1.0.4
    * @versini/ui-icons bumped to 1.12.5
    * @versini/ui-main bumped to 1.0.4
    * @versini/ui-menu bumped to 1.0.4
    * @versini/ui-panel bumped to 1.0.4
    * @versini/ui-pill bumped to 1.0.4
    * @versini/ui-private bumped to 1.4.13
    * @versini/ui-spinner bumped to 1.0.4
    * @versini/ui-table bumped to 1.0.4
  * devDependencies
    * @versini/ui-styles bumped to 1.10.1
</details>

<details><summary>ui-footer: 1.0.4</summary>

##
[1.0.4](ui-footer-v1.0.3...ui-footer-v1.0.4)
(2024-09-24)


### Bug Fixes

* bump non-breaking dependencies to latest
([#692](#692))
([2300b7c](2300b7c))


### Dependencies

* The following workspace dependencies were updated
  * dependencies
    * @versini/ui-private bumped to 1.4.13
  * devDependencies
    * @versini/ui-styles bumped to 1.10.1
</details>

<details><summary>ui-form: 1.6.4</summary>

##
[1.6.4](ui-form-v1.6.3...ui-form-v1.6.4)
(2024-09-24)


### Dependencies

* The following workspace dependencies were updated
  * dependencies
    * @versini/ui-textarea bumped to 1.0.3
    * @versini/ui-textinput bumped to 1.0.3
    * @versini/ui-toggle bumped to 1.0.3
    * @versini/ui-private bumped to 1.4.13
  * devDependencies
    * @versini/ui-button bumped to 1.1.4
    * @versini/ui-icons bumped to 1.12.5
    * @versini/ui-styles bumped to 1.10.1
</details>

<details><summary>ui-header: 1.0.4</summary>

##
[1.0.4](ui-header-v1.0.3...ui-header-v1.0.4)
(2024-09-24)


### Bug Fixes

* bump non-breaking dependencies to latest
([#692](#692))
([2300b7c](2300b7c))


### Dependencies

* The following workspace dependencies were updated
  * dependencies
    * @versini/ui-private bumped to 1.4.13
  * devDependencies
    * @versini/ui-styles bumped to 1.10.1
</details>

<details><summary>ui-icons: 1.12.5</summary>

##
[1.12.5](ui-icons-v1.12.4...ui-icons-v1.12.5)
(2024-09-24)


### Dependencies

* The following workspace dependencies were updated
  * dependencies
    * @versini/ui-private bumped to 1.4.13
  * devDependencies
    * @versini/ui-private bumped to 1.4.13
</details>

<details><summary>ui-main: 1.0.4</summary>

##
[1.0.4](ui-main-v1.0.3...ui-main-v1.0.4)
(2024-09-24)


### Bug Fixes

* bump non-breaking dependencies to latest
([#692](#692))
([2300b7c](2300b7c))


### Dependencies

* The following workspace dependencies were updated
  * dependencies
    * @versini/ui-private bumped to 1.4.13
  * devDependencies
    * @versini/ui-styles bumped to 1.10.1
</details>

<details><summary>ui-menu: 1.0.4</summary>

##
[1.0.4](ui-menu-v1.0.3...ui-menu-v1.0.4)
(2024-09-24)


### Bug Fixes

* bump non-breaking dependencies to latest
([#692](#692))
([2300b7c](2300b7c))


### Dependencies

* The following workspace dependencies were updated
  * devDependencies
    * @versini/ui-button bumped to 1.1.4
    * @versini/ui-icons bumped to 1.12.5
    * @versini/ui-styles bumped to 1.10.1
</details>

<details><summary>ui-panel: 1.0.4</summary>

##
[1.0.4](ui-panel-v1.0.3...ui-panel-v1.0.4)
(2024-09-24)


### Bug Fixes

* bump non-breaking dependencies to latest
([#692](#692))
([2300b7c](2300b7c))


### Dependencies

* The following workspace dependencies were updated
  * dependencies
    * @versini/ui-button bumped to 1.1.4
    * @versini/ui-icons bumped to 1.12.5
    * @versini/ui-private bumped to 1.4.13
  * devDependencies
    * @versini/ui-styles bumped to 1.10.1
</details>

<details><summary>ui-pill: 1.0.4</summary>

##
[1.0.4](ui-pill-v1.0.3...ui-pill-v1.0.4)
(2024-09-24)


### Bug Fixes

* bump non-breaking dependencies to latest
([#692](#692))
([2300b7c](2300b7c))


### Dependencies

* The following workspace dependencies were updated
  * dependencies
    * @versini/ui-private bumped to 1.4.13
  * devDependencies
    * @versini/ui-styles bumped to 1.10.1
</details>

<details><summary>ui-private: 1.4.13</summary>

##
[1.4.13](ui-private-v1.4.12...ui-private-v1.4.13)
(2024-09-24)


### Dependencies

* The following workspace dependencies were updated
  * devDependencies
    * @versini/ui-styles bumped to 1.10.1
</details>

<details><summary>ui-spinner: 1.0.4</summary>

##
[1.0.4](ui-spinner-v1.0.3...ui-spinner-v1.0.4)
(2024-09-24)


### Bug Fixes

* bump non-breaking dependencies to latest
([#692](#692))
([2300b7c](2300b7c))


### Dependencies

* The following workspace dependencies were updated
  * dependencies
    * @versini/ui-private bumped to 1.4.13
  * devDependencies
    * @versini/ui-styles bumped to 1.10.1
</details>

<details><summary>ui-styles: 1.10.1</summary>

##
[1.10.1](ui-styles-v1.10.0...ui-styles-v1.10.1)
(2024-09-24)


### Bug Fixes

* bump non-breaking dependencies to latest
([#692](#692))
([2300b7c](2300b7c))
</details>

<details><summary>ui-system: 1.4.14</summary>

##
[1.4.14](ui-system-v1.4.13...ui-system-v1.4.14)
(2024-09-24)


### Bug Fixes

* bump non-breaking dependencies to latest
([#692](#692))
([2300b7c](2300b7c))


### Dependencies

* The following workspace dependencies were updated
  * dependencies
    * @versini/ui-private bumped to 1.4.13
  * devDependencies
    * @versini/ui-button bumped to 1.1.4
    * @versini/ui-styles bumped to 1.10.1
</details>

<details><summary>ui-table: 1.0.4</summary>

##
[1.0.4](ui-table-v1.0.3...ui-table-v1.0.4)
(2024-09-24)


### Bug Fixes

* bump non-breaking dependencies to latest
([#692](#692))
([2300b7c](2300b7c))


### Dependencies

* The following workspace dependencies were updated
  * dependencies
    * @versini/ui-button bumped to 1.1.4
    * @versini/ui-icons bumped to 1.12.5
    * @versini/ui-private bumped to 1.4.13
  * devDependencies
    * @versini/ui-styles bumped to 1.10.1
</details>

<details><summary>ui-textarea: 1.0.3</summary>

##
[1.0.3](ui-textarea-v1.0.2...ui-textarea-v1.0.3)
(2024-09-24)


### Bug Fixes

* bump non-breaking dependencies to latest
([#692](#692))
([2300b7c](2300b7c))


### Dependencies

* The following workspace dependencies were updated
  * dependencies
    * @versini/ui-private bumped to 1.4.13
  * devDependencies
    * @versini/ui-styles bumped to 1.10.1
</details>

<details><summary>ui-textinput: 1.0.3</summary>

##
[1.0.3](ui-textinput-v1.0.2...ui-textinput-v1.0.3)
(2024-09-24)


### Bug Fixes

* bump non-breaking dependencies to latest
([#692](#692))
([2300b7c](2300b7c))


### Dependencies

* The following workspace dependencies were updated
  * dependencies
    * @versini/ui-private bumped to 1.4.13
  * devDependencies
    * @versini/ui-button bumped to 1.1.4
    * @versini/ui-icons bumped to 1.12.5
    * @versini/ui-styles bumped to 1.10.1
</details>

<details><summary>ui-toggle: 1.0.3</summary>

##
[1.0.3](ui-toggle-v1.0.2...ui-toggle-v1.0.3)
(2024-09-24)


### Bug Fixes

* bump non-breaking dependencies to latest
([#692](#692))
([2300b7c](2300b7c))


### Dependencies

* The following workspace dependencies were updated
  * dependencies
    * @versini/ui-private bumped to 1.4.13
  * devDependencies
    * @versini/ui-styles bumped to 1.10.1
</details>

<details><summary>ui-truncate: 1.0.1</summary>

##
[1.0.1](ui-truncate-v1.0.0...ui-truncate-v1.0.1)
(2024-09-24)


### Bug Fixes

* bump non-breaking dependencies to latest
([#692](#692))
([2300b7c](2300b7c))
* **ui-truncate:** truncate should not split words appart
([#690](#690))
([67b1686](67b1686))


### Dependencies

* The following workspace dependencies were updated
  * dependencies
    * @versini/ui-button bumped to 1.1.4
  * devDependencies
    * @versini/ui-styles bumped to 1.10.1
</details>

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

---------

Co-authored-by: aversini <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant