docs
evals
scenario-1
scenario-10
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
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.
Provides utility functions for string manipulation and data processing.
Implement a generatePreview function that creates text previews with the following capabilities:
Basic Truncation: Accept a text string and a maximum length, returning a shortened version when the text exceeds the limit.
Custom Ending: Support customizable ending indicators (e.g., "...", "[more]", "→") that are appended to truncated text.
Word Boundary Preservation: Provide an option to avoid cutting words in half by truncating at the nearest word boundary before the maximum length.
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)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 }
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 }
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 }
Input:
generatePreview("Short text", { length: 50 })Expected Output:
"Short text"Test File: preview.test.js { .file }