or run

npx @tessl/cli init
Log in

Version

Files

docs

index.md
tile.json

task.mdevals/scenario-3/

Text Preview Generator

Summary

Create a text preview system that displays truncated versions of content with customizable length, ending characters, and word boundary handling. The system should generate user-friendly previews suitable for listings, cards, and search results.

Dependencies { .dependencies }

lodash { .dependency }

Provides utility functions for string manipulation and data processing.

Requirements

Implement a generatePreview function that creates text previews with the following capabilities:

  1. Basic Truncation: Accept a text string and a maximum length, returning a shortened version when the text exceeds the limit.

  2. Custom Ending: Support customizable ending indicators (e.g., "...", "[more]", "→") that are appended to truncated text.

  3. Word Boundary Preservation: Provide an option to avoid cutting words in half by truncating at the nearest word boundary before the maximum length.

  4. Separator Configuration: Allow configuration of what constitutes a word separator (e.g., spaces, hyphens, punctuation).

The function signature should be:

function generatePreview(text, options)

Where options is an object that can contain:

  • length: Maximum length of the preview (default: 30)
  • omission: String to append when text is truncated (default: "...")
  • separator: Pattern to use for determining word boundaries (optional)

Test Cases

Basic truncation { .section @test }

Input:

generatePreview("The quick brown fox jumps over the lazy dog", { length: 20 })

Expected Output:

"The quick brown f..."

Test File: preview.test.js { .file }

Custom omission string { .section @test }

Input:

generatePreview("JavaScript is a versatile programming language", { length: 25, omission: " [Read more]" })

Expected Output:

"JavaScript is [Read more]"

Test File: preview.test.js { .file }

Word boundary preservation { .section @test }

Input:

generatePreview("The quick brown fox jumps over the lazy dog", { length: 20, separator: " " })

Expected Output:

"The quick brown..."

Test File: preview.test.js { .file }

Text shorter than limit { .section @test }

Input:

generatePreview("Short text", { length: 50 })

Expected Output:

"Short text"

Test File: preview.test.js { .file }

Notes

  • When text is shorter than or equal to the specified length, return it unchanged without adding the omission string
  • When using a separator, ensure the truncated text ends at a complete word boundary
  • The omission string counts toward the total length
  • Handle edge cases such as empty strings and very short length limits gracefully